
b709e29 Fix URL typo in release notes 01f203c Update release notes c49810a Update COPYRIGHT e327526 Add BSL version number to LICENSE.TXT 07e3a4e Remove superfluous COPURIGHT.md and LICENSE.md 54c3310 Replace Dynamic Data Routing Platform with Database Proxy 305d02f Remove *.cmake wildcard from .gitignore b0b5208 Cleanup of spaces aeca6d0 Extend maxscaled error messages 817d74c Document where the CDC users are stored 9a569db Update license ff8697a MXS-716: Fix table level privilege detection 2071a8c Only check replies of slaves that are in use f8dfa42 Fix possible hangs in CDC python scripts fa1d99e Removed "filestem option" from example 009b549 Removed "filestem option" from example 8d515c2 Add example Kafka producer script for Python 64e976b Fix sporadic SSL authentication failures 5a655dc MXS-814: Check service/monitor permissions on all servers 2a7f596 Add note about galeramon priority to Galera tutorials b90b5a6 Fixed SHOW SLAVE STATUS in binlog router e22fe39 Changed couln size for SHOW SLAVE STATUS ae97b18 Fix avrorouter build failure with older sqlite libraries 56ef8b7 Replace GPL license with BSL license in scripts and tests 552836f Initialize all fields when MySQL users are loaded from cache bf42947 Update all licensing related files b29db9d Remove optimize_wildcard parameter from use 5170844 Make readwritesplit diagnosting output more clear 262ffb1 Fix crash when a config parameter has no section 33ac9e6 Add note about LEAST_BEHIND_MASTER and server weights e13e860 Fix a memory leak when backend authentication fails 75d00c2 MXS-801: Set the default value of strip_db_esc to true bd5f2db MXS-800: Add a log message about the working directory 4b1dd8c Update MySQL Monitor documentation on detect_replication_lag 559bc99 Fix installation of license file b057587 Change LICENSE to LICENSE.TXT 223fa43 Remove null characters from log messages 36fd05b Fix fatal signal handler log message 053dc8a Fix typos in documentation 371dc87 Fix Galera text in Master-Slave tutorial 30b6265 Disable adding of new objects at runtime db92311 Update the documentation on configuration reloading 0923d40 Update Connector-C version c4738b5 Add define for avro-conversion.ini 196e6ac Update license from GPL to BSL. e612366 Correctly calculate the number of bytes read in dcb_read 93a2a03 Update rotate documentation in admin tutorial c5eb854 MXS-585: Fix authentication handling regression 6330070 Fix schemarouter memory leak aa5827e Fix CDC authentication memory leak a5af4ad Fix avro memory leaks 627d73f Fix Avro scripts 0ff7556 Add build instructions to avrorouter documentation 734a1c8 Fix doxygen mainpage e51ce09 Add licence text to avro scripts 4d27c14 Update Avro documentation and fix installation directories a58a330 Fix readconnroute error message about router_options 22b138c MXS-772: Fix postinstall script a9960b7 Fix function declaration in mysql_backend.c cbe1704 Add missing newline 09d76ee Fix avro documentation default values 1d3f8f3 Added refresh users on START SLAVE 880db34 Make router configuration errors fatal 3bad5ca Update documentation and add logging to avrorouter 10f3384 Disable SSLv3 ca8d902 Fix rwsplit error reporting when master is lost e816d65 Fix MaxScale Tutorial deca3e1 Update MaxScale man page f1735b7 Update release notes 9238714 qc: Change type of DEALLOCATE PREPARE 0b77c3b dbfwfilter: Do not require a trailing new line 1152ca9 Remove copyright message a038a85 Remove debug assertion on ERANGE error in galeramon 12ab235 Fix comparison error for connections limit. 5de1a8f qc_sqlite: Correct string recognition b63d754 Fix links in documentation contents 05d457e CDC protocol link fix 50676ef Fix monitor code formatting 218ba09 Remove MaxScale-and-SSL.md 0d6845b Add images to Avro documentation and tutorial 8dd2c9b Update MaxScale-2.0.0-Release-Notes.md 6d4b593 Change avrorouter default transaction grouping 4c629de Add notes about monitor changes to upgrading and release notes 267d0dc Update Binlogrouter.md c624781 Update Replication-Proxy-Binlog-Router-Tutorial.md f3261bc CDC users 1368797 Format authenticator and module headers ab01749 Format filters 8b05d32 Format core and include files f3974e5 Add GPL LICENSE to qc_mysqlembedded bfec36a astyle rabbitmq_consumer/consumer.c 54b960a Check that the Avro directory is writable 3d4cd2e Fix cdc_users using the wrong path for service users cache 1e738dd Add CDC client program documentation f6809fd Remove superfluous rabbitmw_consumer/LICENSE 6b5e667 Update license text in files 9bfcb46 Change CDC protocol documentation formatting 607f25c REQUEST-DATA formatting 8175ab4 CDC protocol update d5ca272 CDC protocol update 6c91764 Only check wsrep_local_index if node is joined f12e2c2 Do not use SSL for monitors and services 6d2cd99 Fix TestAdminUsers f4ae50d Apply astyle to server/core/test/*.c 7cc2824 Update build instructions cf8e2b5 Update release notes 03c7a6c Remove wrong function prototypes 5a11eed Revert "Remove duplicate functions" 80ed488 Remove duplicate functions bb0de8d Add info on SSL and throttling to release notes for 2.0. 0934aee Update MaxAdmin reference guide 2a3fe9b Update source URL in release notes e575cf0 Merge branch 'MXS-177-develop' into develop cc8c88d Change header for BSL ecde266 Change header for BSL 890b208 Log a message when a script is executed 9c365df Added information on server side SSL to config guide. aa3e002 Remove obsolete heading 79dd73a Make dprintAllSessions use dprintSession 1fc0db9 Align output of "show services" 1b9d301 Make monitorShowAll use monitorShow 983615e Adjust output of 'show modules' 436badd qc_sqlite: The module is now beta a7cbbe5 Update Upgrade document 71ac13f Remove obsolete user/password from example eb20ff8 Fix and clean up Avrorouter diagnostics code 31d4052 Change MaxScale to MariaDB MaxScale e6e4858 Fix `source` parameter not working with `router_options` d8de99a Update module version numbers eb81add Merge remote-tracking branch 'origin/develop' into MXS-177-develop daba563 Merge remote-tracking branch 'origin/MXS-651-develop-merge' into develop 678f417 Changes in response to reviews. 410fb81 Changes in response to reviews. 60135e5 Add initial release notes about Avrorouter 7400ecc qc_sqlite: Remove uninitialized read 536962c Update version number 018f044 Fix debug assertion in client command processing 51f0804 Prevent 'show monitor' from crashing with failed monitor 559347e Fix "Too many connections" message; add comments. 01d3929 Add printf format checking to dcb_printf fbd49a6 dbfwfilter: Require complete parsing only when needed 1885863 Add information to release notes about readwritesplit changes 73b56a2 Update MaxScale section in release notes. 0a2f56f MaxAdmin: Remove debug information from 'show users' 3cf3279 MaxAdmin: Report SSL information as well 29c2b66 Always use SSL if server configured with SSL 7d6b335 dprintAllServers should use dprintServer 02a5246 qc_sqlite: Correctly detect parsing context 469419b compare: Add strict mode 8c5b3d3 compare: Allow the comparison of a single statement 4691514 Add Updgrade to 2.0 document 38b3ecb Expand the checks done before a monitor is stopped 8e2cfb9 Add backend name to authentication error message 9600a07 Fix MaxInfo crash 91c58b0 Removed log message for duplicate entry while adding an user 40392fe Fixed log message priority 0ec35b8 maxadmin: Allow the last user to be removed 5a0ebed maxadmin: Change name of user file 87aa8f1 maxadmin: Always allow root to connect bf37751 Fix COM_QUIT packet detection 7c93ee4 Update avrorouter documentation and tutorial 95ce463 Fix wrong directory in avrorouter log message cfe54c7 Update ChangeLog d69562c Fix LOAD DATA LOCAL INFILE data size tracking 24e7cd6 MXS-584: added support for SET @@session.autocommit d6f6f76 Fixes, correct too many connections message efeb924 Update release notes for 2.0.0 8f71a87 qc_sqlite: Adjust error messages b967d60 Remove copy of enum enum_server_command 822b7e3 Update package license b58301a Update MaxScale License for overlooked files c09ee47 Update MaxScale License 49f46fa Tidy up. Comment out config items not yet supported. f5c3470 Updated and simplified the Building from Source document 98b98e2 Add note about master failure modes to documentation e036f2c Update Limitations document 62219a5 Merge remote-tracking branch 'origin/drain-writeq' into develop 5caf667 Invoke DCB_REASON_DRAINED more frequently. 77b107b qc_sqlite: Add support for LOAD DATA INFILE 8e70f26 compare: Optionally print out the parse result ad750e6 Merge remote-tracking branch 'origin/MXS-651-develop-merge' into develop ef85779 Merge remote-tracking branch 'origin/develop' into MXS-651-develop-merge ea9fdda MXS-477: Add LONGBLOB support for readconnroute eae6d42 qc_sqlite: Remove superfluous columnname definition 8fe2b21 Add binlog source to avrorouter b25cc37 qc_sqlite: Add missing destructors 8a749e7 qc_sqlite: Reduce number of keywords 5f4bb8b compare: Output query immediately 2456e52 dbfwfilter: Reject queries that cannot be parsed 5f1fbbd qc_sqlite: Extend SET grammar b8d8418 dbfwfilter: Remove 'allow' from firewall filter rule 0bd2a44 MXS-741 When no events are read from binlog file, ... a07c491 Remove duplicated function (merge error, probably) b237008 Save conflict resolution, missed last time. a0c0b40 Merge remote-tracking branch 'origin/develop' into MXS-651-develop 385d47d Change SSL logic, fix large read problem. b93b5e0 Remove false debug assertion b953b1f Turn off SSL read ahead. e0d46a5 Fix error messages and remove commented code 49b4655 MXS-739: Fix invalid JSON in Maxinfo 0c30692 qc_sqlite: Handle GROUP_CONCAT arguments 54e48a1 qc_sqlite: Consider \ as an escape character in strings 713a5d6 qc_sqlite: Add test cases 20d1b51 qc_sqlite: Handle qualified names in CREATE VIEW 1019313 qc_sqlite: Make QUERY_TYPE_WRITE default for SHOW 059c14e qc_sqlite: Accept qualified function names in SELECT db34989 qc_sqlite: Accept qualified function names b93e2f1 qc_sqlite: Add limited support for GRAND and REVOKE 678672d qc_sqlite: Cleanup copying of database and table names 9b744b9 qc_sqlite: Update table and database names at the same time db75e61 qc: Support getting the qualified table names 1f867f4 qc: Add join.test 9c7e02a qc_sqlite: Accept "...from NATURAL straight_join..." 93d1c31 qc_sqlite: Both " and ' can enclose a string literal 8055b21 qc_sqlite: Set more information based upon tokens 37e3663 qc_sqlite: Do not blindly add affected fields 50f1360 qc: Correctly collect affected fields 71c234e qc_sqlite: Recognize CREATE TABLE ... UNION 01803f1 qc_sqlite: Recognize {DEALLOCATE|DROP} PREPARE ... 6ecd4b3 qc_sqlite: Parse index hints 0bdab01 qc: Compare sets of tables b908c8f Fix double freeing of internal DCBs 8903556 qc_sqlite: Recognize LEFT, INSERT and REPLACE 266e6c0 qc: Log all problems by default (compare program) 7b54cac qc_sqlite: Fix logging bug 9566e9f qc_sqlite: Plug a leak b0a860d qc: Run compare a specified number of times 050d698 qc_sqlite: Simplified argument handling 97c56b8 qc: Allow arguments to be passed to the query classifier 09a46e0 qc_sqlite: Add argument log_unrecognized_statements fd98153 qc: Allow arguments to be provided to the query classifier 313aa7e Fix Problems SSL assertion; non SSL connect to SSL 1d721e6 Fix DEB packaging errors 96bdc39 Fix RPM packaging failures on CentOS 7 6ba900d qc_sqlite: Recognize more SHOW commands 2869d0b qc_sqlite: Exclude support for sqlite's PRAGMA 0be68a3 qc_sqlite: Enhance SELECT syntax 28f3e1a Merge branch 'develop' into MXS-729 e18bd41 qc: Expose the result of the parsing 5896085 Add BUILD_AVRO to the CMake cache daeb896 Remove changes to blr_master.c memory handling d523821 Add comments 4eb9a66 Empty admin users file is now handled 52b46c6 qc: Update create.test db09711 qc_sqlite: Ignore case when looking for test language keywords f042a1d qc_sqlite: Extend CREATE TABLE syntax 177d2de qc_sqlite: Extend CREATE TABLE syntax d3ca8e6 qc_sqlite: Add some support for HANDLER 86c6a96 qc_sqlite: Recognize RENAME TABLE 471594f qc_sqlite: Accept more table options at CREATE TABLE 3da6cde qc_sqlite: Remove unused keywords bd89662 Fix crash on corrupted passwd file b5d1764 MXS-733: Always print session states 043e2db Remove unused CMake variables 5604fe2 Restore missing line, fixes logic error. 66d15a5 Added log message warning for old users found 5be9fca Changes in response to review by Johan 899e0e2 Removed password parameter from admin_user_add and admin_remove_user a2d9302 Merge branch 'develop' into MXS-729 bcaf82f Code review update e61c716 Nagios plugin update with Maxadmin using UNIX socket only d7150a2 qc_sqlite: Extend column syntax 3b78df0 qc_sqlite: Accept VALUE in addition to VALUES 85a705b qc_sqlite: Accept CHARSET in addition to CHARACTER SET db9cec8 qc_sqlite: Accept qualified column names in CREATE TABLE a9cabb0 qc_sqlite: Extend SELECT syntax f5e9878 qc_sqlite: Add set type 675cb93 qc_sqlite: Allow BINARY to turn into an identifier b04a760 qc_sqlite: Accept DROP TABLES 1075d9c qc_sqlite: Allow qualified name with LIKE in CREATE 420ac56 qc_sqlite: Extend EXPLAIN grammar 727d626 Add missing error message to readwritesplit f4fd09e Change templates and testing configurations to use sockets 1ef2e06 Add configurable default admin user a723731 Remove wrong file 7c3b02b Maxadmin/maxscaled UNIX socket update eed78d4 qc_sqlite: Pick out more information from select when CREATEing 267f091 qc_sqlite: Recognise DROP TEMPORARY TABLE 54fc29f qc_sqlite: Accept $ as a valid character in identifiers afa2ec9 qc_sqlite: Allow keywords to be used in qualified name db0427d MXS-729 code review update a3b3000 Merge branch 'develop' into MXS-729 e73d66c qc_sqlite: Identify qualified identifiers 5bacade Trailing space fix 3bc9be3 MXS-729 socket=default support in maxscale.cnf 1a5c23c Code review update for MXS-729 d6665c7 qc_sqlite: Extend CREATE TABLE grammar 91725ce qc_sqlite: Dequote table and database names cd7a022 qc: Add create test 1aa4e6b qc: Update test files 762b0c2 qc_mysqlembedded: Do not return "*" as table name cd9968f qc_sqlite: Update delete.test f16703d qc_sqlite: Add support for CALL e3ca9b4 qc_mysqlembedded: Do not return an array of empty strings 5878a22 qc_sqlite: Support selects in DELETE 1cf0444 qc_sqlite: Fix bug in DELETE grammar 0bf39a1 qc_sqlite: Add support for CHECK TABLE 4a8feca qc_sqlite: Add helper for catenating SrcLists ab299b3 qc_sqlite: Extend DELETE syntax 5778856 qc_sqlite: Extract database name as well 99901f1 qc_sqlite: Extend DELETE syntax 63396f8 qc_sqlite: Match "autocommit" caseinsensitively e804dd3 qc_sqlite: Add support for LOCK/UNLOCK c23e442 qc_sqlite: Extend DELETE syntax 5460e31 qc: Add delete test ab392ad qc_sqlite: Free unused data 598c6f0 qc: Measure time of parsing 2fa3844 qc_sqlite: Put all changes being {%|#}ifdefs 1b43992 qc_sqlite: Modified update.test 1676ea4 qc_sqlite: LEFT|RIGHT are not required with JOIN 224ebd3 qc_sqlite: Extend UPDATE grammar dbecca9 qc_sqlite: Extend UPDATE grammar b6ca3b3 MaxAdmin security modification MXS-729 8fb47dd Remove copying of MariaDB embedded library files 22e1257 Normalize whitespace when canonicalizing queries 269cff2 MXS-697: Fix dbfwfilter logging for matched queries 6344f6f Ignore Apple .DS_Store files. d606977 Improve comments in response to code review. 619aa13 Add configuration check flag to MaxScale 27c860b Drain write queue improvements. 33d4154 Read only one configuration file d104c19 Format more core files 83fdead Format part of core source code 311d5de Format gateway.c and config.c with Astyle 8cbb48e Don't build maxavro library if BUILD_AVRO is not defined 32bb77a Merge branch 'MXS-483' into develop db72c5d Format CDC/Avro related files 3c26e27 qc_sqlite: Use SrcList instead of Expr f96ad6a Merge branch 'develop' into MXS-729 0728734 Fix query canonical form tests e68262d Merge remote-tracking branch 'gpl-maxscale/master' into develop 65460dc Fix missing symbols from MySQLAuth 791c821 MaxAdmin listens on UNIX socket only and maxadmin can connect 89afed6 MXS-66: All configuration errors are fatal errors d613053 Add more details to galeramon documentation 22f4f57 qc: Add support for multi UPDATE 0dba25a Added default port to blr_make_registration 9d8248c qc_sqlit: Plug leaks and access errors 057551a qc_sqlite: Fix to small an allocation 1f73820 qc_sqlite: Free memory allocated during parsing 93fefb9 qc: Enable compare to run the same test repeatedly e52c578 qc_sqlite: Handle last_insert_id() 929e02a qc_sqlite: Extend UPDATE grammar de3b9f7 qc_sqlite: Defines provided when running cmake and make 4d5c3b2 qc_sqlite: Add support for multiple-table DELETE FROM 36a4296 qc_mysqlembedded: Handle SQLCOM_DELETE_MULTI 41f613a Fix DCB and SESSION removal from free object pools 00f2ddd Move some common code used in only one protocol into protocol. 6fbd0b0 Format Go source with gofmt abfbbcb Fix build failures and internal test suite 31de74a Merge branch 'develop' into MXS-483 20d461d Remove uniqueness constrain on oneshot tasks 6c09288 Add missing error message to converter task 0c2c389 Merge branch 'develop' into MXS-483 fa0accc Set freed memory to NULL after authentication failure 63f24e4 Install cdc_schema.go 5123c21 Fix ALTER TABLE parsing 004acc2 Merge branch 'develop' into MXS-483 f69a671 Remove array foreach macro use a159cd9 qc_sqlite: Add support for SHOW DATABASES 31a2118 Make qc_mysqlembedded optional 27ef30e Changed the default query classifier 359010d Add -h flag as the alias for --host bebc086 Fix minor bugs c7ca253 qc_sqlite: Recognize START [TRANSACTION] 240f8bf qc_sqlite: Collect info from nested SELECTs 93ea393 qc_sqlite: Pass along the relative position of a token cc960af qc_sqlite: Fix incorrect assigment 22a6fef Fix `gtid` avro index table 4c38bef qc_sqlite: STATUS is not a registered word cace998 qc_sqlite: Include all fields of UPDATE 997b19c qc: Add update tests 7601b3f qc_sqlite: Parse "INSERT INTO t VALUES (), ();" correctly ca426f1 qc_sqlite: Handle CREATE TRIGGER f54f963 qc_sqlite: Allow INSERT without INTO e4a1b6d Remove foreign keys from Avro index e4501a2 Merge branch 'develop' into MXS-483 82b9585 Fix MMMon never assigning master status a45a709 qc_mysqlembedded: Find the leaf name 2f3ca8f qc_mysqlembedded: Recognize SQLCOM_REPLACE cc7ad83 qc_mysqlembedded: Pick up fields for INSERT SELECT as well 0e6b39e qc: Cleanup of select.test 9113f4f qc_sqlite: Pickup more fields from INSERT 4d58f98 Dummy query classifier dfe824f Document `query_classifier` option 4aa329b MXS-718: Collect fields of INSERT 53818f2 Modify packet number for SSL backend connection 346f973 qc_sqlite: Accept qualified column names 8a83616 Fix in-memory SQLite table structure 6f2c884 Further backend SSL development 4444e92 qc_sqlite: Extend INSERT grammar 2aebcab qc_sqlite: Add support for TRUNCATE 1a6742e qc_sqlite: Accept DEFAULT as value in INSERT 07dec05 qc_sqlite: Crude classification made based on seen keywords a90a579 Add missing function documentation 72bd0cf qc_sqlite: Change CREATE TABLE grammar 6e04bc8 qc: Add INSERT tests 3666bda qc_sqlite: Add SELECT test d27e173 Add server/mysql-test/t/select.test to query_classifier 562d004 qc_sqlite: Cleanup error logging. 819cacb Merge branch 'develop' into MXS-483 0d3a789 Add warnings and comments to Avro row event processing 2fab570 Added support for SET autocommit=1 1aa83cf Code review fix c999f0a Addition of SELECT USER() 8c723da Clean up avro_client.c and avro_file.c eb21ee8 Clean up avro.c 946a284 Added Avro schema to table metadata processing 72f90be qc_sqlite: Add support for CREATE {FUNCTION|PROCEDURE} ... 4a4ab49 qc: Update line number also when skipping a block ffddb2a qc_sqlite: Allow queries using INTERVAL b8b03bd qc_sqlite: Add support for SELECT * FROM tbl2 = tbl1; 77a261a qc_sqlite: Add support for GROUP BY ... WITH ROLLUP 0ead41e cdc_schema now generates lowercase JSON 66e327a Classifier has to be specified explicitly 9074b91 Updated Avrorouter documentation cf06c7a qc_sqlite: Some comments added. f579eff Added simple Go based Avro schema generator f448e90 MXS-419: Added ulimit calls to init scripts b4ad257 Added FindAvro.cmake 56cc9b9 Added the last transaction script to installations 2d52da2 Added temporary avro-alpha package name 6ada071 Fixed cdc_users script 61f0206 Renaming and minor fixes to CDC Python scripts 9d77c32 Moved GTID table tracking to an in-memory database 8ae7cb0 MXS-704: Fixed `which` usage in post-install scripts 195e118 Readwritesplit sessions aren't created if master is down 2be91da Added affected tables to avro diagnostics b185320 QUERY-LAST-TRANSACTION now returns proper table names 90860b5 Log stale master message only once 4859c60 Table name to GTID mapping f77bd23 First steps to backend SSL, not yet working. 68b5bf0 qc_sqlite: Don't treat TRUE and FALSE as identifiers fca8e59 qc_sqlite: Collect database names as well 6b0e04d qc_sqlite: Add support for SHOW CREATE VIEW 77f4b34 qc_mysqlembedded: Report more, rather than less a73e033 qc_sqlite: Extend builtin functions 9d9650e qc_sqlite: SQL_BUFFER_RESULT must decay to an id 83fe99d qc_sqlite: Support INSERT IGNORE 9d1c9ca Added avrorouter limitations and tutorial 8dd094d qc_sqlite: Recognize builtin functions 2edc3d6 Moved write operations of the maxavro library to a different file 1364e54 Added more comments to the Avro RBR handling code f711588 Added warnign about unsupported field types df0d250 Added SQLite3 based indexing to avrorouter 0c55706 Added GTID event flag check in AVRO processing bfe28dd qc_sqlite: Accept SET GLOBAL|SESSION ... a8d2068 qc_mysqlembedded: Exlude code that won't compile on 5.5.42 16ea0b3 qc_sqlite: Add support for DROP FUNCTION 1c0f1fc qc: Report stats after comparison 02345b2 qc_sqlite: Recognize builtin readonly functions c7a5e75 qc_sqlite: Recognize := 0aa849d qc: Make compare undestand the delimiter command fb0a877 qc_mysqlembedded: Examine Item::SUBSELECT_ITEMs 045cf8d qc: Add missing mtl commands e5c6f45 qc_sqlite: Relax qc_get_type comparison ac3b2df qc_sqlite: Add support for SHOW STATUS 73a34fb qc_sqlite: Add initial support for FLUSH 4ffbe79 qc_sqlite: Extend CREATE TABLE syntax 009af81 qc_sqlite: Add support for SHOW WARNINGS 001de97 qc: Ignore mysqltest constructs 128307d Merge branch 'release-1.4.3' into gpl-master 5e8a06a SET NAMES XXX added 3ca12ba MXS-685: MMMon clears server state before setting it dc4d2b0 Further steps to connection limit, non-working. ef70257 MXS-636: Master failures are no longer fatal errors 99f4c64 Updated QUERY-LAST-TRANSACTION format d1ff157 Changed QUERY-LAST-TRANSACTION format to JSON 8b2f1ac Fixed formatting of the avrorouter 61543df Added QUERY-LAST-TRANSACTION command c10d10b qc_sqlite: Add support for SHOW CREATE TABLE 106a38f qc_sqlite: Add support for DROP INDEX 2a85421 qc_sqlite: Extend what can be stated about a table 794cd1c qc_sqlite: Add support for MATCH ... AGAINST dd7b747 qc_sqlite: Accept FULLTEXT and SPATIAL in CREATE TABLE a13d6ce qc_sqlite: Add support for PREPARE and EXECUTE 0c5d29f qc_sqlite: Add support for ANALYZE a6cd32b qc_sqlite: Extend SET syntax 5d47704 qc_sqlite: Pick out fields from UPDATE t SET i = ... 0e05735 qc: Understand --error in server test files 8535975 qc_sqlite: Extend CREATE VIEW syntax b35e638 qc: Igore read type bit if write bit is on 818a814 qc_sqlite: Add support for SHOW VARIABLES 1aa877b qc_sqlite: Add initial support for DO e92913a qc_sqlite: Add support for CREATE VIEW d53a46d qc_sqlite: Recognize bit field literals b'1010' 1fb7977 Added GTID event timestmap into struct gtid_pos 8f95b10 Added new fields in AVRO diagnostics cb4db54 Added tests with large SQL packets to modutil tests e4dbd6b MXS-621: More fixes to log messages at startup 4f1e9ee qc: compare tester can now read server MySQL tests cd8154b qc_sqlite: Allow CHARACTER SET to be specified for column 6f8d053 Added MariaDB 10.1 check for new flags in GTID event 71c471b qc_mysqlembedded: Fix type bits setting 26b00a7 qc_sqlite: Extend ALTER grammar ea6057c qc_sqlite: Handle also pInto when dupping a struct select 2271559 qc_sqlite: Add support for SHOW TABLE STATUS 9caaf27 qc_sqlite: Add support for CREATE ... LIKE oldtable cd19932 Merge tag '1.4.2' into master 9e9e4d8 Merge branch 'develop' of https://github.com/mariadb-corporation/maxscale-bsl into develop 267cb60 qc_mysqlembedded: Look into parenthesized comma expressions 77c6ca9 qc_sqlite: Recognize token "<=>" 5ca9a9f qc_sqlite: Allow comma expressions in where clause b08e910 qc_sqlite: Add SELECT options d11e581 qc_sqlite: Some recursion cleanup d53d063 Add but don't invoke connection queue functionality. 6818104 Fix logic error in connections limiter 3c61605 qc_sqlite: Find more affected fields 9af8dfd Allow the classifiers to be specified on the command line 5d4a134 Activate call to protocol for max connections error message. 16638e7 Fix another mistake 234f9e6 Fix mistake 843a6fc Fix mistake. 2c6e9ad Fix errors in config.c; enable call to protocol on connection limit. fd27849 Introduce configuration items for Maximum and Queued Service connections 60d198d Implement very simple connection limit. 84d8f0f Merge remote-tracking branch 'origin/develop' into MXS-177 8a58e63 Merge remote-tracking branch 'origin/develop' into develop 08487cd Add assertion to enforce number of free DCBs not being negative. f73af2f Added MariaDB 10.1 check for new flags in GTID event 23898ec Fix wrong sprintf specifier, trailing white space. ea6cfa3 readwritesplit: Cleaned up routeQuery 3858df0 Cleaned up select_connect_backend_servers c38ee13 Added more buffer tests 48816df Added more modutils tests 537eac2 Added tests for modutil_get_complete_packets 22a6095 MXS-669: modutil_get_complete_packets no longer makes the buffer contiguous 51af97e qc_sqlite: Add support for CREATE INDEX e50c573 qc_sqlite: Dig out fields for IN f58c6df qc_sqlite: Dequote table name 319422b qc_sqlite: Accept ENUM as type for a column 5d6a45c qc_sqlite: Allow UNSIGNED to fallback to an id 16a5f20 qc_sqlite: Extend CREATE TABLE syntax d6268da qc_sqlite: Accept RIGHT and FULL OUTER joins 2207415 qc_sqlite: Allow STRAIGHT_JOIN in SELECT 6fee546 qc_sqlite: Pick upp more table names 9de5f84 Remove trailing white space. 758f84d Improve comments and messages in dcb.c and session.c re recycle memory. 1c2de21 Merge remote-tracking branch 'origin/develop' into dcb-optimise 6614944 DCB code tidying. Fix missing spinlock release; remove redundant variables ecd5e5c Remove extra code introduced by merge. 877127a Merge commit '3c0d3e5ab6ddde59da764ec904b517759074a31e' into develop 4275bbe Updated the Connector-C version to 2.2.3 c71042b Some tentative list management code; provide for counting service clients. ad0c8a6 qc_sqlite: Allow empty insert statement 72e75e5 qc_sqlite: Add support for SELECT ... INTO cc553fa qc_sqlite: MAXSCALE define can now be used everywhere 3305c6e qc_sqlite: Handle CASE in SELECT 702f62e qc_sqlite: Extend CREATE TABLE grammar 941c212 qc_sqlite: Add support for SHOW [INDEX|INDEXES|KEYS] 6a79136 qc_sqlite: Extend grammar for SHOW TABLES and SHOW COLUMNS f175d2d qc_sqlite: Add SHOW COLUMNS support 6e47951 qc_sqlite: Add support for SHOW TABLES bcfa0e7 qc_mysqlembedded: Return the actual name and not as-name 3e19f2e Fixed qlafilter build failure 810b24e MXS-675: Standardized qlafilter output be92173 qc_sqlite: Exclude alias names from affected fields 9479280 qc_sqlite: Add support for explain EXTENDED 13b0e10 qc_sqlite: Add support for DELETE a6ccfea qc_mysqlembedded: Look at all conditional items b428041 qc_sqlite: Extend SELECT options 83f829f query_classifier: Correctly calculate the length of a GWBUF 2ddb24c query_classifier: Ensure that -- comments are handled fa7a746 qc_sqlite: Allow STRAIGHT_JOIN in SELECTS 6f47819 FindLibUUID update 5ed897b Added FindLibUUID cmake file 16e02bb Added FindLibUUID cmake file aff63e0 MXS-680: qc_mysqlembedded does not look into functions 8a0eeb4 query_classifier: Improve output of compare 6f08185 Query classifier can now convert enums to strings 124e2b9 MXS-679: Correctly collect fields of WHERE 353c97c transaction_safety default is off 896e37b qc_sqlite: Invert stop logic and be more verbose 7a44d4d qc_sqlite: Extend what is accepted in CREATE TABLE 4dbf499 qc_sqlite: Accept FIRST in ALTER TABLE 3f655c0 qc_sqlite: Update table and affected fields for INSERT 8e1e275 qc_sqlite: Make AS optional in CREATE statement 5f2084b qc_sqlite: Add support for ENGINE when creating a table 242f183 qc_sqlite: CREATE paramters handled in the correct place 8ed2e25 qc_sqlite: Trace only when needed 63d4531 qc_sqlite: Update affected fields also from functions 118cdc3 qc_sqlite: Allow multiple index names in USE|IGNORE INDEX 912da76 qc_sqlite: Add initial support for ...IGNORE INDEX... 0aa7de6 qc_sqlite: Log detailed message on error 3e3bf1a qc_sqlite: Extend create syntax. c4a4572 qc_sqlite: Exclude quoted values 1621f49 Removed MYSQL_EMBEDDED_LIBRARIES d3e324c UUID generation now comes from libuuid e8fe678 qc_sqlite: Enable confitional compilation a9522ba qc_sqlite: Handle X.Y selects 9bc9770 qc_sqlite: Use same stream when outputting padding 366257a qc_sqlite: Add support for UNSIGNED and ZEROFILL d4d90ff qc_sqlite: Add support for DROP VIEW d0519bd qc_sqlite: Extend DROP TABLE syntax c1e4894 qc_sqlite: Add flag to compare for stopping at first error 9fd6344 MXS-674: Maxinfo generates invalid JSON 3c0d3e5 Fix stupid errors. 9d32b2d Include read queue in buffer provided by dcb_read; changes to match. b690797 Fix double spinlock release in random_jkiss. 6a4328f Fix problems of memory not being freed in some error cases. 2112e56 Change DCB and Session handling to recycle memory; fix bug in random_jkiss. 3912f72 MXS-631, MXS-632: Cleaned up default value CMake files 383ccb8 Fixed build failure on MariaDB 5.5 a60bca5 Merge branch '1.2.1-binlog_router_trx' into develop 3c2a062 Fix to crashes in embedded library with MariaDB 10.0 d3fe938 MXS-662: Service protocol check no longer ignores bind address c3da49b qc_sqlite: Update affected fields from everywhere 7a0fab8 qc_sqlite: Allow verbosity of compare test to be controlled 81d6822 qc_sqlite: Cleanup handling of select columns 13e5c59 qc_sqlite: Introduce custom allocation functions 026f27d qc_sqlite: Add support for "USE database" 99079df qc_sqlite: Ignore duplicates when comparing affected fields ca45cd6 qc_sqlite: Add initial support for qc_get_database_names 75970b6 qc_sqlite: Add support for DROP TABLE. b97e45d qc_sqlite: Move get_affected_fields() to other helpers cb0fa96 qc_sqlite: Collect table names of INSERT 3a7c513 qc_mysqlembedded: Only look for created name if CREATE 308b0a4 qc_sqlite: Add support for gc_get_created_table_name. 0dc4af2 qc_sqlite: Add qc_has_clause() handling to update e9f2d1d qc_sqlite: Update now also provides table names c3192e4 qc_sqlite: Add initial support for get_table_names c51eafd qc_sqlite: Add support for qc_has_clause f318fb2 qc_mysqlembedded: Work around embedded lib bug 4ba2e11 qc_sqlite: Add initial support for qc_get_affected_fields 080dea5 qc_sqlite: Support is_read_query 3f94df1 Fixed compare.cc build failure 868a712 Updated freeing of buffer chains in readwritesplit 9bf7fca Formatted readwritesplit source code de4da2b Add assertion to spinlock release to detect release of already released spinlock. d30955a qc_sqlite: Handle the default case of affected fields. 5d02b3f qc_sqlite: Set operation when creating table 94a334d Add test for comparing qc-output aa6f5d6 Allow a specific query classifier to be loaded explicitly c799d37 Test both qc_mysqlembedded and qc_sqlite f8d9aa1 qc_sqlite: Enable "set @user_var=@@system_var" f190bdc qc_sqlite: Recognize /*!-comments b694b55 Fixed binary Avro format streaming c95fa86 qc_sqlite: Report correctly the type of set autocommit 9cb236c qc_sqlite: Add test case 77b4e62 Ensure classify test checks all types 962039e Change return type of qc_get_type ae00df8 qc_sqlite: Add initial support for the SET statement. 88253c5 qc_sqlite: Rename functions fa48043 Rework of MySQL backend protocol clean up to fix fault. 3851064 qc_sqlite: Correct recognition of system variables (@@xyz). 9d86f7f qc_sqlite: Detect user and system variables. a683297 qc_sqlite: Recognize and accept system variables (@@xyz). a4f64dd qc_sqlite: Add initial support for CREATE [TEMPORARY] TABLE f834b4f MXS-661: Only COM_QUERY packets are parsed 30077c1 CMake policies set only for correct versions a166f34 Suppress warning about unknown CMake target 1412730 Added more variables to launchable monitor scripts 358c194 MXS-656: Galera nodes with index 0 can be master again 842aec5 qc_sqlite: Add support for BEGIN, COMMIT, ROLLBACK b9cad6d Add initial support for UPDATE. 95741cb Add initial support for insert. 3796158 Re-install sqlite whenever parse.y has changed 5bcd8cf Ensure that the query is the one passed cf05533 Add support for obtaining the type of a query 400d8b4 Always log the outcome 45cf632 Fixed resource leaks and minor bugs fa9e970 Printout the query when there is a mismatch. 263cb07 All classify to be used with any query classifier ea381b9 Further cleanup of classify.c 23f6f30 Merge pull request #107 from godmodelabs/typo-dpkg 8c2a64e Fixed classify build failure 0c3070b Fixed binlog to Avro conversion bugs b827ba9 MXS-653: Silence maxpasswd 30d981c MXS-654: Add test for checking maxpasswd 984039b Rearrange classify.c 837e46d Add log initialization 1cc7a6e Reformat query_classifier/test/classify.c 065a4e5 Merge branch 'develop' into develop-MXS-544-b-merge ca27f13 Fixed binlog build failure fb81be2 fixed typo dpgk <-> dpkg 1e88d5d Added python based CDC user creation script 040bbdd MXS-633: Monitor permission checks moved to modules cde7595 Master-Slave clusters are now robust by default 158c776 Cleaned up core test suite 94c6e66 Fixed bugs resulting from merge a491e11 Merge remote-tracking branch 'origin/MXS-544-b' into develop-MXS-544-b-merge 30f9f25 Cleaned up avro.c 6286f64 Merge branch 'release-1.4.1' into develop 00206ac MXS-194: Added support for more query types to dbfwfilter 267832b Fixed diagnostic output a64b694 Fixed bugs in avrorouter 8faaba1 Fixed a bug in GTID seeking a5fafb7 Fixed typos in avrorouter documentation 8080379 Added avrorouter documentation fa07d8a Fixed dbfwfilter rule parser build failure 744ce0d Constraints are ignored in DDL statement processing 50808c6 Cleaned up avrorouter 47f6032 Merge branch '1.2.1-binlog_router_trx_lint' into develop caa0956 Added missing dependencies to maxscale-core 92df61a Remove parallel make from travis coverity builds fa2b2b4 Added more error logging to Avro record reading 9a98e8b Support for GTID requests and data bursts c2a787b Small diagnostic fix c4cee7e Added format output requested by client 50483c7 Cleaning up of Avro code d485379 Added support for binary Avro protocol c22cdbb Converted Avro GTID from string to integer representation 5795ca9 Added coverity notification email to .travis.yml a06e44d Added coverity_scan to Travis 6b94384 Fixed memory leak in avro_schema.c a11096c Support for db.table request for Avrorouter 4e5cbbf Fixed bugs in Avro record reading a99e427 Fixed minor bugs in avrorouter 01db8ae Fixed errors with CREATE TABLE statements f5f3d7a Diagnostic routine update 209324f Added missing include for log_manager.h e62f764 Added sending of schemas and file rotation 8c8fcbb Added missing log_manager.h include b13942d Changed printf calls in maxavro library to MXS_ERROR 1168962 More lint inspired changes, mainly in blr_master.c and blr_slave.c ced8f2f Fixed directory checks in avrorouter a8ae6be Minor fix to string processing fbd2d95 Fixed typo in dbfwfilter's CMakeLists.txt 29c3cf4 Merge pull request #106 from mariadb-corporation/willfong-patch-1 854d4e9 Add password column name to example 2f956df Moved server state change logging to a common function 007121f Fixed truncated string values 782892b Fix lint errors and warnings for blr_file.c 4f99fc5 Added Avro testing script 2820980 Small fix to help clear lint problems in blr.c 3afeda4 Fixed errors and warnings located by lint ecfff82 Fix most lint problems in blr.c 223689c Added ALTER TABLE support 80bc935 Fix final lint problems with mysql_common protocol functions. e068310 Added preliminary alter table parsing 8c723f1 Lint monitor modules fdb5620 Fix lint issues in authenticators. 84f0e04 Added function documentation and renamed files 365d9f5 Tidy up, mainly for lint 2ff3005 Added update rows event processing and event types to avro records 2ae0371 Fixed failing regex and improved data streaming f19206a Renamed avrorouter header aa7174b Moved relpacement and storage of ddl statements to a separate function 0c10be8 Improved client notification and added Avro block size managemet 91405a7 Cleaned up instance creation dd97485 Removed useless vars af64e9e Added CDC authentication with a db file b73a118 Streamline and lint MySQL backend protocol. 65034ce Merge branch 'release-1.4.0' into develop 28f7e4e Added callback for AVRO client async data transmission 628c27a Added MAXAVRO_FILE struct to AVRO_CLIENT 32b3645 Fixed slavelag build failure 7b15542 Added default authentication method for CDC protocol 5f8e20f Renamed maxavro types and formatted files that use them 882cf84 Added more function documentation to maxavro library 9532f0b Fixed CDC protocol build failure 35a1d3a Added support for offsets in client requests 94577ac Fixed, formatted and refactored CDC protocol da9bcad Use the maxavro library to read data from Avro files 3ececee Added low level error checking to maxavro library 01b0b8b Tidy and lint mysql_client.c 943f0a7 Added handling of Avro boolean data types to maxavro library 4c781f7 Cleaned up maxavro library and avrorouter 6b2e85d Renamed functions more consistently and cleaned up code e07158a Moved query event handling to its own function df7d4c0 Added avro_ prefix to rbr.c fcbfceb Added seeking to a position in an Avro file 068243a CDC auth decoding 3584d54 Add checks to simplify downstream logic. 9b2c323 Removed useless fprintf bd5cd52 Added missing authfunc setup e4aff59 Added record value processing 5cc8615 Added value length functions 7921ecc Merge branch 'MXS-615' into MXS-483 4b09cca Added Travis status to readme.md cca3a48 Simplify interface to max admin authentication. 4739838 Authenticator API update 233505f Maxavrocheck now accepts multiple files 3fdd137 Improved the Avro file handling a6ba913 Merge from MXS-615 417d742 Added maxavrocheck 014f9cf Remove obsolete second parameter from authenticate function in authenticators. ece7ece MaxAdmin authentication converted to a module. Fix quirk in SSL setup. 7c8b37e Moved contents of avro_schema.h into mxs_avro.h d6660cf Improvements to type handling 71ed0cf Protocol API to have entry point for obtaining default authenticator name. 9d35de2 Fixed transaction tracking 5be02a2 Avrorouter internal state is now stored in the Avro file directory 9293464 Added new info to avro diagnostics 06e0e93 Protocol modules can still handle the authentication outside authenticator modules 6d7108b Added JSON output when Requesting an avro file 6188211 Added new CDC protocol state c8af27f CDC authentication uses its own authenticator 6590f94 Factor out qc_get_qtype_str b7880f1 Fix qc_sqlite CMakeLists.txt bd4ff43 Fixed connector-c being updated and built after every make invokation 0d9e57b Fixed non-MariaDB connectors being used in builds 3d3b779 FIX BUG IN CLIENT DCB SHUTDOWN THAT CAN CAUSE CRASHES e45ba33 Fixed Connector-C .cmake files c130189 Fixed connector-c being updated and built after every make invokation 7f3cdf3 Fixed errors on binlog rotation 9d3c83a Remove qc_sqlite 15e8ba5 CDC protocol is now compliant with new protocol structure 4460869 Merge branch 'release-1.4.0' into MXS-483 ea40812 Cleaned up the binlog processing loop cb646ca Add minimal select recognition to qc_sqlite ac1a9c5 Fixed binlogrouter test 85dd227 Re-route sqlite's sqlite3Select. 7a2e6f3 Update CMakeLists.txt for qc_sqlite 7a751c3 Added timestamps to records and fixed minor bugs f73bdde Avrorouter state storage to disk fcf0488 Fixed Connector-C .cmake files 48b8e4e Merge branch 'MXS-615' into MXS-615-binlog-merge 7c8e19f Add missing dependencies for qc_sqlite bb9b667 Improvements to type handling and binlog position tracking dc66b74 Client UUID added f12fce4 AVRO registration is now handled by avro router 575b809 Add skeleton sqlite-based query classifier. d09d5fc Build sqlite 146d1f9 Fixed BLOB type handling and refined error messages 6e9e521 Added client user to diagnostics 4538bb8 Merge pull request #104 from rasmushoj/develop 7e18d95 Avro router diagnostics routine update 01e3f75 reverted changes in CMakeLists.txt 52f7c78 reverted changes in postinst.in eaed577 Added sqlite 3110100 a58cdda Travis configuration for MaxScale. ... 38b452d MIGRATE FREE CLIENT DATA TO AUTH MODULE; BUG FIXES; TIDY UP 6e64506 Fixed minor bugs aff2411 Enabled CDC protocol f669100 Fixed NULL Avro value being assigned to a field which cannot be NULL 8f6b16a Added row event processing to avrorouter 2939fe0 Updated Avro schema management to use actual column names 9e3b0cb Removed use of RBR related functions in binlogrouter d674903 Formatted avro files fe028d1 DEVELOPMENT OF AUTHENTICATION AS MODULE - WILL NOT WORK YET 977aded Added authenticator modules to the build a2b384f MOVE MYSQL AUTH CODE INTO AUTHENTICATOR MODULES DIRECTORY a5d7484 PRELIMINARY CHANGES TO CREATE AUTHENTICATORS AS MODULES 66cf802 Merge remote-tracking branch 'origin/develop' into MXS-615 bca0a7d MINOR CHANGES TO SATISFY LINT 5a9e397 Added Avrorouter binlog file walking fbc737f Fixed binlogrouter test 3c7c9d7 Added avrorouter main event handling loop 07ad81b Moved common binlogrouter code to a separate file 8c605ed Fixed avrorouter build failures aa1ba05 Moved binlog definitions to a separate header and fixed build failures eee7c55 Added create table statement detection e52b27e Added AVRO_INSTANCE and AVRO_CLIENT 0830caa Change test for client DCB to use role being DCB_ROLE_CLIENT_HANDLER. ... 997bbca Change protocols to continue looping if an accept fails; ... 522e42d Make use of dcb_accept and dcb_listen in httpd and telnetd protocols. 4e692b0 Generalise dcb_listen to tailor log messages to different protocols. ... 52c431d Remove support for passing default port number when handling ... afe5abc Fix bug in creation of SSL listener structure; fix bugs in ... 0bd6c77 Merge remote-tracking branch 'origin/MXS-544' into MXS-544-a ... 7598597 Add dcb_listen function to make a given DCB into a listener, ... a275d89 Maxbinlogcheck avro version can detect proper end of file 9bb55a5 Moved row event and table map event handling to a separate file b7d9e09 Add/improve comments, fix mistake with premature return. c598770 First attempt at extracting general code into dcb_accept, ... f20f28f Testing with maxbinlogcheck b3c60b7 Added mysql_binlog files 0ff9971 Added MariaDB/MySQL binary data processing functions 124560c Merge branch '1.2.1-binlog_router_trx' into MXS-483 4deccff New router fro cdc client 2c11434 Fixed test compiler errors c1f7d24 Obliged to merge remote-tracking branch 'origin/develop' ... 1775599 Merge remote-tracking branch 'origin/MXS-544' into Test-dev-544-merge c5317da Small modifications in comments 11c0666 Code cleanup 64a5e9a Merge branch 'release-1.3.0' into MXS-483 2c11e89 First Implementation of CDC
4061 lines
127 KiB
C
4061 lines
127 KiB
C
/*
|
|
** 2014 August 30
|
|
**
|
|
** The author disclaims copyright to this source code. In place of
|
|
** a legal notice, here is a blessing:
|
|
**
|
|
** May you do good and not evil.
|
|
** May you find forgiveness for yourself and forgive others.
|
|
** May you share freely, never taking more than you give.
|
|
**
|
|
*************************************************************************
|
|
**
|
|
**
|
|
** OVERVIEW
|
|
**
|
|
** The RBU extension requires that the RBU update be packaged as an
|
|
** SQLite database. The tables it expects to find are described in
|
|
** sqlite3rbu.h. Essentially, for each table xyz in the target database
|
|
** that the user wishes to write to, a corresponding data_xyz table is
|
|
** created in the RBU database and populated with one row for each row to
|
|
** update, insert or delete from the target table.
|
|
**
|
|
** The update proceeds in three stages:
|
|
**
|
|
** 1) The database is updated. The modified database pages are written
|
|
** to a *-oal file. A *-oal file is just like a *-wal file, except
|
|
** that it is named "<database>-oal" instead of "<database>-wal".
|
|
** Because regular SQLite clients do not look for file named
|
|
** "<database>-oal", they go on using the original database in
|
|
** rollback mode while the *-oal file is being generated.
|
|
**
|
|
** During this stage RBU does not update the database by writing
|
|
** directly to the target tables. Instead it creates "imposter"
|
|
** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
|
|
** to update each b-tree individually. All updates required by each
|
|
** b-tree are completed before moving on to the next, and all
|
|
** updates are done in sorted key order.
|
|
**
|
|
** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
|
|
** location using a call to rename(2). Before doing this the RBU
|
|
** module takes an EXCLUSIVE lock on the database file, ensuring
|
|
** that there are no other active readers.
|
|
**
|
|
** Once the EXCLUSIVE lock is released, any other database readers
|
|
** detect the new *-wal file and read the database in wal mode. At
|
|
** this point they see the new version of the database - including
|
|
** the updates made as part of the RBU update.
|
|
**
|
|
** 3) The new *-wal file is checkpointed. This proceeds in the same way
|
|
** as a regular database checkpoint, except that a single frame is
|
|
** checkpointed each time sqlite3rbu_step() is called. If the RBU
|
|
** handle is closed before the entire *-wal file is checkpointed,
|
|
** the checkpoint progress is saved in the RBU database and the
|
|
** checkpoint can be resumed by another RBU client at some point in
|
|
** the future.
|
|
**
|
|
** POTENTIAL PROBLEMS
|
|
**
|
|
** The rename() call might not be portable. And RBU is not currently
|
|
** syncing the directory after renaming the file.
|
|
**
|
|
** When state is saved, any commit to the *-oal file and the commit to
|
|
** the RBU update database are not atomic. So if the power fails at the
|
|
** wrong moment they might get out of sync. As the main database will be
|
|
** committed before the RBU update database this will likely either just
|
|
** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
|
|
** constraint violations).
|
|
**
|
|
** If some client does modify the target database mid RBU update, or some
|
|
** other error occurs, the RBU extension will keep throwing errors. It's
|
|
** not really clear how to get out of this state. The system could just
|
|
** by delete the RBU update database and *-oal file and have the device
|
|
** download the update again and start over.
|
|
**
|
|
** At present, for an UPDATE, both the new.* and old.* records are
|
|
** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
|
|
** fields are collected. This means we're probably writing a lot more
|
|
** data to disk when saving the state of an ongoing update to the RBU
|
|
** update database than is strictly necessary.
|
|
**
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#include "sqlite3.h"
|
|
|
|
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
|
|
#include "sqlite3rbu.h"
|
|
|
|
#if defined(_WIN32_WCE)
|
|
#include "windows.h"
|
|
#endif
|
|
|
|
/* Maximum number of prepared UPDATE statements held by this module */
|
|
#define SQLITE_RBU_UPDATE_CACHESIZE 16
|
|
|
|
/*
|
|
** Swap two objects of type TYPE.
|
|
*/
|
|
#if !defined(SQLITE_AMALGAMATION)
|
|
# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
|
|
#endif
|
|
|
|
/*
|
|
** The rbu_state table is used to save the state of a partially applied
|
|
** update so that it can be resumed later. The table consists of integer
|
|
** keys mapped to values as follows:
|
|
**
|
|
** RBU_STATE_STAGE:
|
|
** May be set to integer values 1, 2, 4 or 5. As follows:
|
|
** 1: the *-rbu file is currently under construction.
|
|
** 2: the *-rbu file has been constructed, but not yet moved
|
|
** to the *-wal path.
|
|
** 4: the checkpoint is underway.
|
|
** 5: the rbu update has been checkpointed.
|
|
**
|
|
** RBU_STATE_TBL:
|
|
** Only valid if STAGE==1. The target database name of the table
|
|
** currently being written.
|
|
**
|
|
** RBU_STATE_IDX:
|
|
** Only valid if STAGE==1. The target database name of the index
|
|
** currently being written, or NULL if the main table is currently being
|
|
** updated.
|
|
**
|
|
** RBU_STATE_ROW:
|
|
** Only valid if STAGE==1. Number of rows already processed for the current
|
|
** table/index.
|
|
**
|
|
** RBU_STATE_PROGRESS:
|
|
** Trbul number of sqlite3rbu_step() calls made so far as part of this
|
|
** rbu update.
|
|
**
|
|
** RBU_STATE_CKPT:
|
|
** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
|
|
** header created by recovering the *-wal file. This is used to detect
|
|
** cases when another client appends frames to the *-wal file in the
|
|
** middle of an incremental checkpoint (an incremental checkpoint cannot
|
|
** be continued if this happens).
|
|
**
|
|
** RBU_STATE_COOKIE:
|
|
** Valid if STAGE==1. The current change-counter cookie value in the
|
|
** target db file.
|
|
**
|
|
** RBU_STATE_OALSZ:
|
|
** Valid if STAGE==1. The size in bytes of the *-oal file.
|
|
*/
|
|
#define RBU_STATE_STAGE 1
|
|
#define RBU_STATE_TBL 2
|
|
#define RBU_STATE_IDX 3
|
|
#define RBU_STATE_ROW 4
|
|
#define RBU_STATE_PROGRESS 5
|
|
#define RBU_STATE_CKPT 6
|
|
#define RBU_STATE_COOKIE 7
|
|
#define RBU_STATE_OALSZ 8
|
|
|
|
#define RBU_STAGE_OAL 1
|
|
#define RBU_STAGE_MOVE 2
|
|
#define RBU_STAGE_CAPTURE 3
|
|
#define RBU_STAGE_CKPT 4
|
|
#define RBU_STAGE_DONE 5
|
|
|
|
|
|
#define RBU_CREATE_STATE \
|
|
"CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
|
|
|
|
typedef struct RbuFrame RbuFrame;
|
|
typedef struct RbuObjIter RbuObjIter;
|
|
typedef struct RbuState RbuState;
|
|
typedef struct rbu_vfs rbu_vfs;
|
|
typedef struct rbu_file rbu_file;
|
|
typedef struct RbuUpdateStmt RbuUpdateStmt;
|
|
|
|
#if !defined(SQLITE_AMALGAMATION)
|
|
typedef unsigned int u32;
|
|
typedef unsigned char u8;
|
|
typedef sqlite3_int64 i64;
|
|
#endif
|
|
|
|
/*
|
|
** These values must match the values defined in wal.c for the equivalent
|
|
** locks. These are not magic numbers as they are part of the SQLite file
|
|
** format.
|
|
*/
|
|
#define WAL_LOCK_WRITE 0
|
|
#define WAL_LOCK_CKPT 1
|
|
#define WAL_LOCK_READ0 3
|
|
|
|
/*
|
|
** A structure to store values read from the rbu_state table in memory.
|
|
*/
|
|
struct RbuState {
|
|
int eStage;
|
|
char *zTbl;
|
|
char *zIdx;
|
|
i64 iWalCksum;
|
|
int nRow;
|
|
i64 nProgress;
|
|
u32 iCookie;
|
|
i64 iOalSz;
|
|
};
|
|
|
|
struct RbuUpdateStmt {
|
|
char *zMask; /* Copy of update mask used with pUpdate */
|
|
sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
|
|
RbuUpdateStmt *pNext;
|
|
};
|
|
|
|
/*
|
|
** An iterator of this type is used to iterate through all objects in
|
|
** the target database that require updating. For each such table, the
|
|
** iterator visits, in order:
|
|
**
|
|
** * the table itself,
|
|
** * each index of the table (zero or more points to visit), and
|
|
** * a special "cleanup table" state.
|
|
**
|
|
** abIndexed:
|
|
** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
|
|
** it points to an array of flags nTblCol elements in size. The flag is
|
|
** set for each column that is either a part of the PK or a part of an
|
|
** index. Or clear otherwise.
|
|
**
|
|
*/
|
|
struct RbuObjIter {
|
|
sqlite3_stmt *pTblIter; /* Iterate through tables */
|
|
sqlite3_stmt *pIdxIter; /* Index iterator */
|
|
int nTblCol; /* Size of azTblCol[] array */
|
|
char **azTblCol; /* Array of unquoted target column names */
|
|
char **azTblType; /* Array of target column types */
|
|
int *aiSrcOrder; /* src table col -> target table col */
|
|
u8 *abTblPk; /* Array of flags, set on target PK columns */
|
|
u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
|
|
u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
|
|
int eType; /* Table type - an RBU_PK_XXX value */
|
|
|
|
/* Output variables. zTbl==0 implies EOF. */
|
|
int bCleanup; /* True in "cleanup" state */
|
|
const char *zTbl; /* Name of target db table */
|
|
const char *zDataTbl; /* Name of rbu db table (or null) */
|
|
const char *zIdx; /* Name of target db index (or null) */
|
|
int iTnum; /* Root page of current object */
|
|
int iPkTnum; /* If eType==EXTERNAL, root of PK index */
|
|
int bUnique; /* Current index is unique */
|
|
|
|
/* Statements created by rbuObjIterPrepareAll() */
|
|
int nCol; /* Number of columns in current object */
|
|
sqlite3_stmt *pSelect; /* Source data */
|
|
sqlite3_stmt *pInsert; /* Statement for INSERT operations */
|
|
sqlite3_stmt *pDelete; /* Statement for DELETE ops */
|
|
sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
|
|
|
|
/* Last UPDATE used (for PK b-tree updates only), or NULL. */
|
|
RbuUpdateStmt *pRbuUpdate;
|
|
};
|
|
|
|
/*
|
|
** Values for RbuObjIter.eType
|
|
**
|
|
** 0: Table does not exist (error)
|
|
** 1: Table has an implicit rowid.
|
|
** 2: Table has an explicit IPK column.
|
|
** 3: Table has an external PK index.
|
|
** 4: Table is WITHOUT ROWID.
|
|
** 5: Table is a virtual table.
|
|
*/
|
|
#define RBU_PK_NOTABLE 0
|
|
#define RBU_PK_NONE 1
|
|
#define RBU_PK_IPK 2
|
|
#define RBU_PK_EXTERNAL 3
|
|
#define RBU_PK_WITHOUT_ROWID 4
|
|
#define RBU_PK_VTAB 5
|
|
|
|
|
|
/*
|
|
** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
|
|
** one of the following operations.
|
|
*/
|
|
#define RBU_INSERT 1 /* Insert on a main table b-tree */
|
|
#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
|
|
#define RBU_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
|
|
#define RBU_IDX_INSERT 4 /* Insert on an aux. index b-tree */
|
|
#define RBU_UPDATE 5 /* Update a row in a main table b-tree */
|
|
|
|
|
|
/*
|
|
** A single step of an incremental checkpoint - frame iWalFrame of the wal
|
|
** file should be copied to page iDbPage of the database file.
|
|
*/
|
|
struct RbuFrame {
|
|
u32 iDbPage;
|
|
u32 iWalFrame;
|
|
};
|
|
|
|
/*
|
|
** RBU handle.
|
|
*/
|
|
struct sqlite3rbu {
|
|
int eStage; /* Value of RBU_STATE_STAGE field */
|
|
sqlite3 *dbMain; /* target database handle */
|
|
sqlite3 *dbRbu; /* rbu database handle */
|
|
char *zTarget; /* Path to target db */
|
|
char *zRbu; /* Path to rbu db */
|
|
char *zState; /* Path to state db (or NULL if zRbu) */
|
|
char zStateDb[5]; /* Db name for state ("stat" or "main") */
|
|
int rc; /* Value returned by last rbu_step() call */
|
|
char *zErrmsg; /* Error message if rc!=SQLITE_OK */
|
|
int nStep; /* Rows processed for current object */
|
|
int nProgress; /* Rows processed for all objects */
|
|
RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
|
|
const char *zVfsName; /* Name of automatically created rbu vfs */
|
|
rbu_file *pTargetFd; /* File handle open on target db */
|
|
i64 iOalSz;
|
|
|
|
/* The following state variables are used as part of the incremental
|
|
** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
|
|
** function rbuSetupCheckpoint() for details. */
|
|
u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
|
|
u32 mLock;
|
|
int nFrame; /* Entries in aFrame[] array */
|
|
int nFrameAlloc; /* Allocated size of aFrame[] array */
|
|
RbuFrame *aFrame;
|
|
int pgsz;
|
|
u8 *aBuf;
|
|
i64 iWalCksum;
|
|
};
|
|
|
|
/*
|
|
** An rbu VFS is implemented using an instance of this structure.
|
|
*/
|
|
struct rbu_vfs {
|
|
sqlite3_vfs base; /* rbu VFS shim methods */
|
|
sqlite3_vfs *pRealVfs; /* Underlying VFS */
|
|
sqlite3_mutex *mutex; /* Mutex to protect pMain */
|
|
rbu_file *pMain; /* Linked list of main db files */
|
|
};
|
|
|
|
/*
|
|
** Each file opened by an rbu VFS is represented by an instance of
|
|
** the following structure.
|
|
*/
|
|
struct rbu_file {
|
|
sqlite3_file base; /* sqlite3_file methods */
|
|
sqlite3_file *pReal; /* Underlying file handle */
|
|
rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
|
|
sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
|
|
|
|
int openFlags; /* Flags this file was opened with */
|
|
u32 iCookie; /* Cookie value for main db files */
|
|
u8 iWriteVer; /* "write-version" value for main db files */
|
|
|
|
int nShm; /* Number of entries in apShm[] array */
|
|
char **apShm; /* Array of mmap'd *-shm regions */
|
|
char *zDel; /* Delete this when closing file */
|
|
|
|
const char *zWal; /* Wal filename for this main db file */
|
|
rbu_file *pWalFd; /* Wal file descriptor for this main db */
|
|
rbu_file *pMainNext; /* Next MAIN_DB file */
|
|
};
|
|
|
|
|
|
/*************************************************************************
|
|
** The following three functions, found below:
|
|
**
|
|
** rbuDeltaGetInt()
|
|
** rbuDeltaChecksum()
|
|
** rbuDeltaApply()
|
|
**
|
|
** are lifted from the fossil source code (http://fossil-scm.org). They
|
|
** are used to implement the scalar SQL function rbu_fossil_delta().
|
|
*/
|
|
|
|
/*
|
|
** Read bytes from *pz and convert them into a positive integer. When
|
|
** finished, leave *pz pointing to the first character past the end of
|
|
** the integer. The *pLen parameter holds the length of the string
|
|
** in *pz and is decremented once for each character in the integer.
|
|
*/
|
|
static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
|
|
static const signed char zValue[] = {
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
|
|
-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
|
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
|
|
-1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
|
|
};
|
|
unsigned int v = 0;
|
|
int c;
|
|
unsigned char *z = (unsigned char*)*pz;
|
|
unsigned char *zStart = z;
|
|
while( (c = zValue[0x7f&*(z++)])>=0 ){
|
|
v = (v<<6) + c;
|
|
}
|
|
z--;
|
|
*pLen -= z - zStart;
|
|
*pz = (char*)z;
|
|
return v;
|
|
}
|
|
|
|
/*
|
|
** Compute a 32-bit checksum on the N-byte buffer. Return the result.
|
|
*/
|
|
static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
|
|
const unsigned char *z = (const unsigned char *)zIn;
|
|
unsigned sum0 = 0;
|
|
unsigned sum1 = 0;
|
|
unsigned sum2 = 0;
|
|
unsigned sum3 = 0;
|
|
while(N >= 16){
|
|
sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
|
|
sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
|
|
sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
|
|
sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
|
|
z += 16;
|
|
N -= 16;
|
|
}
|
|
while(N >= 4){
|
|
sum0 += z[0];
|
|
sum1 += z[1];
|
|
sum2 += z[2];
|
|
sum3 += z[3];
|
|
z += 4;
|
|
N -= 4;
|
|
}
|
|
sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
|
|
switch(N){
|
|
case 3: sum3 += (z[2] << 8);
|
|
case 2: sum3 += (z[1] << 16);
|
|
case 1: sum3 += (z[0] << 24);
|
|
default: ;
|
|
}
|
|
return sum3;
|
|
}
|
|
|
|
/*
|
|
** Apply a delta.
|
|
**
|
|
** The output buffer should be big enough to hold the whole output
|
|
** file and a NUL terminator at the end. The delta_output_size()
|
|
** routine will determine this size for you.
|
|
**
|
|
** The delta string should be null-terminated. But the delta string
|
|
** may contain embedded NUL characters (if the input and output are
|
|
** binary files) so we also have to pass in the length of the delta in
|
|
** the lenDelta parameter.
|
|
**
|
|
** This function returns the size of the output file in bytes (excluding
|
|
** the final NUL terminator character). Except, if the delta string is
|
|
** malformed or intended for use with a source file other than zSrc,
|
|
** then this routine returns -1.
|
|
**
|
|
** Refer to the delta_create() documentation above for a description
|
|
** of the delta file format.
|
|
*/
|
|
static int rbuDeltaApply(
|
|
const char *zSrc, /* The source or pattern file */
|
|
int lenSrc, /* Length of the source file */
|
|
const char *zDelta, /* Delta to apply to the pattern */
|
|
int lenDelta, /* Length of the delta */
|
|
char *zOut /* Write the output into this preallocated buffer */
|
|
){
|
|
unsigned int limit;
|
|
unsigned int total = 0;
|
|
#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
|
|
char *zOrigOut = zOut;
|
|
#endif
|
|
|
|
limit = rbuDeltaGetInt(&zDelta, &lenDelta);
|
|
if( *zDelta!='\n' ){
|
|
/* ERROR: size integer not terminated by "\n" */
|
|
return -1;
|
|
}
|
|
zDelta++; lenDelta--;
|
|
while( *zDelta && lenDelta>0 ){
|
|
unsigned int cnt, ofst;
|
|
cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
|
|
switch( zDelta[0] ){
|
|
case '@': {
|
|
zDelta++; lenDelta--;
|
|
ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
|
|
if( lenDelta>0 && zDelta[0]!=',' ){
|
|
/* ERROR: copy command not terminated by ',' */
|
|
return -1;
|
|
}
|
|
zDelta++; lenDelta--;
|
|
total += cnt;
|
|
if( total>limit ){
|
|
/* ERROR: copy exceeds output file size */
|
|
return -1;
|
|
}
|
|
if( (int)(ofst+cnt) > lenSrc ){
|
|
/* ERROR: copy extends past end of input */
|
|
return -1;
|
|
}
|
|
memcpy(zOut, &zSrc[ofst], cnt);
|
|
zOut += cnt;
|
|
break;
|
|
}
|
|
case ':': {
|
|
zDelta++; lenDelta--;
|
|
total += cnt;
|
|
if( total>limit ){
|
|
/* ERROR: insert command gives an output larger than predicted */
|
|
return -1;
|
|
}
|
|
if( (int)cnt>lenDelta ){
|
|
/* ERROR: insert count exceeds size of delta */
|
|
return -1;
|
|
}
|
|
memcpy(zOut, zDelta, cnt);
|
|
zOut += cnt;
|
|
zDelta += cnt;
|
|
lenDelta -= cnt;
|
|
break;
|
|
}
|
|
case ';': {
|
|
zDelta++; lenDelta--;
|
|
zOut[0] = 0;
|
|
#ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
|
|
if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
|
|
/* ERROR: bad checksum */
|
|
return -1;
|
|
}
|
|
#endif
|
|
if( total!=limit ){
|
|
/* ERROR: generated size does not match predicted size */
|
|
return -1;
|
|
}
|
|
return total;
|
|
}
|
|
default: {
|
|
/* ERROR: unknown delta operator */
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
/* ERROR: unterminated delta */
|
|
return -1;
|
|
}
|
|
|
|
static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
|
|
int size;
|
|
size = rbuDeltaGetInt(&zDelta, &lenDelta);
|
|
if( *zDelta!='\n' ){
|
|
/* ERROR: size integer not terminated by "\n" */
|
|
return -1;
|
|
}
|
|
return size;
|
|
}
|
|
|
|
/*
|
|
** End of code taken from fossil.
|
|
*************************************************************************/
|
|
|
|
/*
|
|
** Implementation of SQL scalar function rbu_fossil_delta().
|
|
**
|
|
** This function applies a fossil delta patch to a blob. Exactly two
|
|
** arguments must be passed to this function. The first is the blob to
|
|
** patch and the second the patch to apply. If no error occurs, this
|
|
** function returns the patched blob.
|
|
*/
|
|
static void rbuFossilDeltaFunc(
|
|
sqlite3_context *context,
|
|
int argc,
|
|
sqlite3_value **argv
|
|
){
|
|
const char *aDelta;
|
|
int nDelta;
|
|
const char *aOrig;
|
|
int nOrig;
|
|
|
|
int nOut;
|
|
int nOut2;
|
|
char *aOut;
|
|
|
|
assert( argc==2 );
|
|
|
|
nOrig = sqlite3_value_bytes(argv[0]);
|
|
aOrig = (const char*)sqlite3_value_blob(argv[0]);
|
|
nDelta = sqlite3_value_bytes(argv[1]);
|
|
aDelta = (const char*)sqlite3_value_blob(argv[1]);
|
|
|
|
/* Figure out the size of the output */
|
|
nOut = rbuDeltaOutputSize(aDelta, nDelta);
|
|
if( nOut<0 ){
|
|
sqlite3_result_error(context, "corrupt fossil delta", -1);
|
|
return;
|
|
}
|
|
|
|
aOut = sqlite3_malloc(nOut+1);
|
|
if( aOut==0 ){
|
|
sqlite3_result_error_nomem(context);
|
|
}else{
|
|
nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
|
|
if( nOut2!=nOut ){
|
|
sqlite3_result_error(context, "corrupt fossil delta", -1);
|
|
}else{
|
|
sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
** Prepare the SQL statement in buffer zSql against database handle db.
|
|
** If successful, set *ppStmt to point to the new statement and return
|
|
** SQLITE_OK.
|
|
**
|
|
** Otherwise, if an error does occur, set *ppStmt to NULL and return
|
|
** an SQLite error code. Additionally, set output variable *pzErrmsg to
|
|
** point to a buffer containing an error message. It is the responsibility
|
|
** of the caller to (eventually) free this buffer using sqlite3_free().
|
|
*/
|
|
static int prepareAndCollectError(
|
|
sqlite3 *db,
|
|
sqlite3_stmt **ppStmt,
|
|
char **pzErrmsg,
|
|
const char *zSql
|
|
){
|
|
int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
|
|
if( rc!=SQLITE_OK ){
|
|
*pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
|
|
*ppStmt = 0;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Reset the SQL statement passed as the first argument. Return a copy
|
|
** of the value returned by sqlite3_reset().
|
|
**
|
|
** If an error has occurred, then set *pzErrmsg to point to a buffer
|
|
** containing an error message. It is the responsibility of the caller
|
|
** to eventually free this buffer using sqlite3_free().
|
|
*/
|
|
static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
|
|
int rc = sqlite3_reset(pStmt);
|
|
if( rc!=SQLITE_OK ){
|
|
*pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Unless it is NULL, argument zSql points to a buffer allocated using
|
|
** sqlite3_malloc containing an SQL statement. This function prepares the SQL
|
|
** statement against database db and frees the buffer. If statement
|
|
** compilation is successful, *ppStmt is set to point to the new statement
|
|
** handle and SQLITE_OK is returned.
|
|
**
|
|
** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
|
|
** returned. In this case, *pzErrmsg may also be set to point to an error
|
|
** message. It is the responsibility of the caller to free this error message
|
|
** buffer using sqlite3_free().
|
|
**
|
|
** If argument zSql is NULL, this function assumes that an OOM has occurred.
|
|
** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
|
|
*/
|
|
static int prepareFreeAndCollectError(
|
|
sqlite3 *db,
|
|
sqlite3_stmt **ppStmt,
|
|
char **pzErrmsg,
|
|
char *zSql
|
|
){
|
|
int rc;
|
|
assert( *pzErrmsg==0 );
|
|
if( zSql==0 ){
|
|
rc = SQLITE_NOMEM;
|
|
*ppStmt = 0;
|
|
}else{
|
|
rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
|
|
sqlite3_free(zSql);
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
|
|
** by an earlier call to rbuObjIterCacheTableInfo().
|
|
*/
|
|
static void rbuObjIterFreeCols(RbuObjIter *pIter){
|
|
int i;
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
sqlite3_free(pIter->azTblCol[i]);
|
|
sqlite3_free(pIter->azTblType[i]);
|
|
}
|
|
sqlite3_free(pIter->azTblCol);
|
|
pIter->azTblCol = 0;
|
|
pIter->azTblType = 0;
|
|
pIter->aiSrcOrder = 0;
|
|
pIter->abTblPk = 0;
|
|
pIter->abNotNull = 0;
|
|
pIter->nTblCol = 0;
|
|
pIter->eType = 0; /* Invalid value */
|
|
}
|
|
|
|
/*
|
|
** Finalize all statements and free all allocations that are specific to
|
|
** the current object (table/index pair).
|
|
*/
|
|
static void rbuObjIterClearStatements(RbuObjIter *pIter){
|
|
RbuUpdateStmt *pUp;
|
|
|
|
sqlite3_finalize(pIter->pSelect);
|
|
sqlite3_finalize(pIter->pInsert);
|
|
sqlite3_finalize(pIter->pDelete);
|
|
sqlite3_finalize(pIter->pTmpInsert);
|
|
pUp = pIter->pRbuUpdate;
|
|
while( pUp ){
|
|
RbuUpdateStmt *pTmp = pUp->pNext;
|
|
sqlite3_finalize(pUp->pUpdate);
|
|
sqlite3_free(pUp);
|
|
pUp = pTmp;
|
|
}
|
|
|
|
pIter->pSelect = 0;
|
|
pIter->pInsert = 0;
|
|
pIter->pDelete = 0;
|
|
pIter->pRbuUpdate = 0;
|
|
pIter->pTmpInsert = 0;
|
|
pIter->nCol = 0;
|
|
}
|
|
|
|
/*
|
|
** Clean up any resources allocated as part of the iterator object passed
|
|
** as the only argument.
|
|
*/
|
|
static void rbuObjIterFinalize(RbuObjIter *pIter){
|
|
rbuObjIterClearStatements(pIter);
|
|
sqlite3_finalize(pIter->pTblIter);
|
|
sqlite3_finalize(pIter->pIdxIter);
|
|
rbuObjIterFreeCols(pIter);
|
|
memset(pIter, 0, sizeof(RbuObjIter));
|
|
}
|
|
|
|
/*
|
|
** Advance the iterator to the next position.
|
|
**
|
|
** If no error occurs, SQLITE_OK is returned and the iterator is left
|
|
** pointing to the next entry. Otherwise, an error code and message is
|
|
** left in the RBU handle passed as the first argument. A copy of the
|
|
** error code is returned.
|
|
*/
|
|
static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
|
|
int rc = p->rc;
|
|
if( rc==SQLITE_OK ){
|
|
|
|
/* Free any SQLite statements used while processing the previous object */
|
|
rbuObjIterClearStatements(pIter);
|
|
if( pIter->zIdx==0 ){
|
|
rc = sqlite3_exec(p->dbMain,
|
|
"DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
|
|
"DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
|
|
"DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
|
|
"DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
|
|
, 0, 0, &p->zErrmsg
|
|
);
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
if( pIter->bCleanup ){
|
|
rbuObjIterFreeCols(pIter);
|
|
pIter->bCleanup = 0;
|
|
rc = sqlite3_step(pIter->pTblIter);
|
|
if( rc!=SQLITE_ROW ){
|
|
rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
|
|
pIter->zTbl = 0;
|
|
}else{
|
|
pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
|
|
pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
|
|
rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
|
|
}
|
|
}else{
|
|
if( pIter->zIdx==0 ){
|
|
sqlite3_stmt *pIdx = pIter->pIdxIter;
|
|
rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
|
|
}
|
|
if( rc==SQLITE_OK ){
|
|
rc = sqlite3_step(pIter->pIdxIter);
|
|
if( rc!=SQLITE_ROW ){
|
|
rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
|
|
pIter->bCleanup = 1;
|
|
pIter->zIdx = 0;
|
|
}else{
|
|
pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
|
|
pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
|
|
pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
|
|
rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
rbuObjIterFinalize(pIter);
|
|
p->rc = rc;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
|
|
/*
|
|
** The implementation of the rbu_target_name() SQL function. This function
|
|
** accepts one argument - the name of a table in the RBU database. If the
|
|
** table name matches the pattern:
|
|
**
|
|
** data[0-9]_<name>
|
|
**
|
|
** where <name> is any sequence of 1 or more characters, <name> is returned.
|
|
** Otherwise, if the only argument does not match the above pattern, an SQL
|
|
** NULL is returned.
|
|
**
|
|
** "data_t1" -> "t1"
|
|
** "data0123_t2" -> "t2"
|
|
** "dataAB_t3" -> NULL
|
|
*/
|
|
static void rbuTargetNameFunc(
|
|
sqlite3_context *context,
|
|
int argc,
|
|
sqlite3_value **argv
|
|
){
|
|
const char *zIn;
|
|
assert( argc==1 );
|
|
|
|
zIn = (const char*)sqlite3_value_text(argv[0]);
|
|
if( zIn && strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
|
|
int i;
|
|
for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
|
|
if( zIn[i]=='_' && zIn[i+1] ){
|
|
sqlite3_result_text(context, &zIn[i+1], -1, SQLITE_STATIC);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Initialize the iterator structure passed as the second argument.
|
|
**
|
|
** If no error occurs, SQLITE_OK is returned and the iterator is left
|
|
** pointing to the first entry. Otherwise, an error code and message is
|
|
** left in the RBU handle passed as the first argument. A copy of the
|
|
** error code is returned.
|
|
*/
|
|
static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
|
|
int rc;
|
|
memset(pIter, 0, sizeof(RbuObjIter));
|
|
|
|
rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
|
|
"SELECT rbu_target_name(name) AS target, name FROM sqlite_master "
|
|
"WHERE type IN ('table', 'view') AND target IS NOT NULL "
|
|
"ORDER BY name"
|
|
);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
|
|
"SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
|
|
" FROM main.sqlite_master "
|
|
" WHERE type='index' AND tbl_name = ?"
|
|
);
|
|
}
|
|
|
|
pIter->bCleanup = 1;
|
|
p->rc = rc;
|
|
return rbuObjIterNext(p, pIter);
|
|
}
|
|
|
|
/*
|
|
** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
|
|
** an error code is stored in the RBU handle passed as the first argument.
|
|
**
|
|
** If an error has already occurred (p->rc is already set to something other
|
|
** than SQLITE_OK), then this function returns NULL without modifying the
|
|
** stored error code. In this case it still calls sqlite3_free() on any
|
|
** printf() parameters associated with %z conversions.
|
|
*/
|
|
static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
|
|
char *zSql = 0;
|
|
va_list ap;
|
|
va_start(ap, zFmt);
|
|
zSql = sqlite3_vmprintf(zFmt, ap);
|
|
if( p->rc==SQLITE_OK ){
|
|
if( zSql==0 ) p->rc = SQLITE_NOMEM;
|
|
}else{
|
|
sqlite3_free(zSql);
|
|
zSql = 0;
|
|
}
|
|
va_end(ap);
|
|
return zSql;
|
|
}
|
|
|
|
/*
|
|
** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
|
|
** arguments are the usual subsitution values. This function performs
|
|
** the printf() style substitutions and executes the result as an SQL
|
|
** statement on the RBU handles database.
|
|
**
|
|
** If an error occurs, an error code and error message is stored in the
|
|
** RBU handle. If an error has already occurred when this function is
|
|
** called, it is a no-op.
|
|
*/
|
|
static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
|
|
va_list ap;
|
|
char *zSql;
|
|
va_start(ap, zFmt);
|
|
zSql = sqlite3_vmprintf(zFmt, ap);
|
|
if( p->rc==SQLITE_OK ){
|
|
if( zSql==0 ){
|
|
p->rc = SQLITE_NOMEM;
|
|
}else{
|
|
p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
|
|
}
|
|
}
|
|
sqlite3_free(zSql);
|
|
va_end(ap);
|
|
return p->rc;
|
|
}
|
|
|
|
/*
|
|
** Attempt to allocate and return a pointer to a zeroed block of nByte
|
|
** bytes.
|
|
**
|
|
** If an error (i.e. an OOM condition) occurs, return NULL and leave an
|
|
** error code in the rbu handle passed as the first argument. Or, if an
|
|
** error has already occurred when this function is called, return NULL
|
|
** immediately without attempting the allocation or modifying the stored
|
|
** error code.
|
|
*/
|
|
static void *rbuMalloc(sqlite3rbu *p, int nByte){
|
|
void *pRet = 0;
|
|
if( p->rc==SQLITE_OK ){
|
|
assert( nByte>0 );
|
|
pRet = sqlite3_malloc64(nByte);
|
|
if( pRet==0 ){
|
|
p->rc = SQLITE_NOMEM;
|
|
}else{
|
|
memset(pRet, 0, nByte);
|
|
}
|
|
}
|
|
return pRet;
|
|
}
|
|
|
|
|
|
/*
|
|
** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
|
|
** there is room for at least nCol elements. If an OOM occurs, store an
|
|
** error code in the RBU handle passed as the first argument.
|
|
*/
|
|
static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
|
|
int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
|
|
char **azNew;
|
|
|
|
azNew = (char**)rbuMalloc(p, nByte);
|
|
if( azNew ){
|
|
pIter->azTblCol = azNew;
|
|
pIter->azTblType = &azNew[nCol];
|
|
pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
|
|
pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
|
|
pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
|
|
pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
|
|
}
|
|
}
|
|
|
|
/*
|
|
** The first argument must be a nul-terminated string. This function
|
|
** returns a copy of the string in memory obtained from sqlite3_malloc().
|
|
** It is the responsibility of the caller to eventually free this memory
|
|
** using sqlite3_free().
|
|
**
|
|
** If an OOM condition is encountered when attempting to allocate memory,
|
|
** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
|
|
** if the allocation succeeds, (*pRc) is left unchanged.
|
|
*/
|
|
static char *rbuStrndup(const char *zStr, int *pRc){
|
|
char *zRet = 0;
|
|
|
|
assert( *pRc==SQLITE_OK );
|
|
if( zStr ){
|
|
size_t nCopy = strlen(zStr) + 1;
|
|
zRet = (char*)sqlite3_malloc64(nCopy);
|
|
if( zRet ){
|
|
memcpy(zRet, zStr, nCopy);
|
|
}else{
|
|
*pRc = SQLITE_NOMEM;
|
|
}
|
|
}
|
|
|
|
return zRet;
|
|
}
|
|
|
|
/*
|
|
** Finalize the statement passed as the second argument.
|
|
**
|
|
** If the sqlite3_finalize() call indicates that an error occurs, and the
|
|
** rbu handle error code is not already set, set the error code and error
|
|
** message accordingly.
|
|
*/
|
|
static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
|
|
sqlite3 *db = sqlite3_db_handle(pStmt);
|
|
int rc = sqlite3_finalize(pStmt);
|
|
if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
|
|
p->rc = rc;
|
|
p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
|
|
}
|
|
}
|
|
|
|
/* Determine the type of a table.
|
|
**
|
|
** peType is of type (int*), a pointer to an output parameter of type
|
|
** (int). This call sets the output parameter as follows, depending
|
|
** on the type of the table specified by parameters dbName and zTbl.
|
|
**
|
|
** RBU_PK_NOTABLE: No such table.
|
|
** RBU_PK_NONE: Table has an implicit rowid.
|
|
** RBU_PK_IPK: Table has an explicit IPK column.
|
|
** RBU_PK_EXTERNAL: Table has an external PK index.
|
|
** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
|
|
** RBU_PK_VTAB: Table is a virtual table.
|
|
**
|
|
** Argument *piPk is also of type (int*), and also points to an output
|
|
** parameter. Unless the table has an external primary key index
|
|
** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
|
|
** if the table does have an external primary key index, then *piPk
|
|
** is set to the root page number of the primary key index before
|
|
** returning.
|
|
**
|
|
** ALGORITHM:
|
|
**
|
|
** if( no entry exists in sqlite_master ){
|
|
** return RBU_PK_NOTABLE
|
|
** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
|
|
** return RBU_PK_VTAB
|
|
** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
|
|
** if( the index that is the pk exists in sqlite_master ){
|
|
** *piPK = rootpage of that index.
|
|
** return RBU_PK_EXTERNAL
|
|
** }else{
|
|
** return RBU_PK_WITHOUT_ROWID
|
|
** }
|
|
** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
|
|
** return RBU_PK_IPK
|
|
** }else{
|
|
** return RBU_PK_NONE
|
|
** }
|
|
*/
|
|
static void rbuTableType(
|
|
sqlite3rbu *p,
|
|
const char *zTab,
|
|
int *peType,
|
|
int *piTnum,
|
|
int *piPk
|
|
){
|
|
/*
|
|
** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
|
|
** 1) PRAGMA index_list = ?
|
|
** 2) SELECT count(*) FROM sqlite_master where name=%Q
|
|
** 3) PRAGMA table_info = ?
|
|
*/
|
|
sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
|
|
|
|
*peType = RBU_PK_NOTABLE;
|
|
*piPk = 0;
|
|
|
|
assert( p->rc==SQLITE_OK );
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
|
|
sqlite3_mprintf(
|
|
"SELECT (sql LIKE 'create virtual%%'), rootpage"
|
|
" FROM sqlite_master"
|
|
" WHERE name=%Q", zTab
|
|
));
|
|
if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
|
|
/* Either an error, or no such table. */
|
|
goto rbuTableType_end;
|
|
}
|
|
if( sqlite3_column_int(aStmt[0], 0) ){
|
|
*peType = RBU_PK_VTAB; /* virtual table */
|
|
goto rbuTableType_end;
|
|
}
|
|
*piTnum = sqlite3_column_int(aStmt[0], 1);
|
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
|
|
);
|
|
if( p->rc ) goto rbuTableType_end;
|
|
while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
|
|
const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
|
|
const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
|
|
if( zOrig && zIdx && zOrig[0]=='p' ){
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
|
|
sqlite3_mprintf(
|
|
"SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
|
|
));
|
|
if( p->rc==SQLITE_OK ){
|
|
if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
|
|
*piPk = sqlite3_column_int(aStmt[2], 0);
|
|
*peType = RBU_PK_EXTERNAL;
|
|
}else{
|
|
*peType = RBU_PK_WITHOUT_ROWID;
|
|
}
|
|
}
|
|
goto rbuTableType_end;
|
|
}
|
|
}
|
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
|
|
);
|
|
if( p->rc==SQLITE_OK ){
|
|
while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
|
|
if( sqlite3_column_int(aStmt[3],5)>0 ){
|
|
*peType = RBU_PK_IPK; /* explicit IPK column */
|
|
goto rbuTableType_end;
|
|
}
|
|
}
|
|
*peType = RBU_PK_NONE;
|
|
}
|
|
|
|
rbuTableType_end: {
|
|
unsigned int i;
|
|
for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
|
|
rbuFinalize(p, aStmt[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
** This is a helper function for rbuObjIterCacheTableInfo(). It populates
|
|
** the pIter->abIndexed[] array.
|
|
*/
|
|
static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
|
|
sqlite3_stmt *pList = 0;
|
|
int bIndex = 0;
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
|
|
);
|
|
}
|
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
|
|
const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
|
|
sqlite3_stmt *pXInfo = 0;
|
|
if( zIdx==0 ) break;
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
|
|
);
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
|
int iCid = sqlite3_column_int(pXInfo, 1);
|
|
if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
|
|
}
|
|
rbuFinalize(p, pXInfo);
|
|
bIndex = 1;
|
|
}
|
|
|
|
rbuFinalize(p, pList);
|
|
if( bIndex==0 ) pIter->abIndexed = 0;
|
|
}
|
|
|
|
|
|
/*
|
|
** If they are not already populated, populate the pIter->azTblCol[],
|
|
** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
|
|
** the table (not index) that the iterator currently points to.
|
|
**
|
|
** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
|
|
** an error does occur, an error code and error message are also left in
|
|
** the RBU handle.
|
|
*/
|
|
static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
|
|
if( pIter->azTblCol==0 ){
|
|
sqlite3_stmt *pStmt = 0;
|
|
int nCol = 0;
|
|
int i; /* for() loop iterator variable */
|
|
int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
|
|
int iOrder = 0;
|
|
int iTnum = 0;
|
|
|
|
/* Figure out the type of table this step will deal with. */
|
|
assert( pIter->eType==0 );
|
|
rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
|
|
if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
|
|
p->rc = SQLITE_ERROR;
|
|
p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
|
|
}
|
|
if( p->rc ) return p->rc;
|
|
if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
|
|
|
|
assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
|
|
|| pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
|
|
|| pIter->eType==RBU_PK_VTAB
|
|
);
|
|
|
|
/* Populate the azTblCol[] and nTblCol variables based on the columns
|
|
** of the input table. Ignore any input table columns that begin with
|
|
** "rbu_". */
|
|
p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
|
|
sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
|
|
);
|
|
if( p->rc==SQLITE_OK ){
|
|
nCol = sqlite3_column_count(pStmt);
|
|
rbuAllocateIterArrays(p, pIter, nCol);
|
|
}
|
|
for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
|
|
const char *zName = (const char*)sqlite3_column_name(pStmt, i);
|
|
if( sqlite3_strnicmp("rbu_", zName, 4) ){
|
|
char *zCopy = rbuStrndup(zName, &p->rc);
|
|
pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
|
|
pIter->azTblCol[pIter->nTblCol++] = zCopy;
|
|
}
|
|
else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
|
|
bRbuRowid = 1;
|
|
}
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
pStmt = 0;
|
|
|
|
if( p->rc==SQLITE_OK
|
|
&& bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
|
|
){
|
|
p->rc = SQLITE_ERROR;
|
|
p->zErrmsg = sqlite3_mprintf(
|
|
"table %q %s rbu_rowid column", pIter->zDataTbl,
|
|
(bRbuRowid ? "may not have" : "requires")
|
|
);
|
|
}
|
|
|
|
/* Check that all non-HIDDEN columns in the destination table are also
|
|
** present in the input table. Populate the abTblPk[], azTblType[] and
|
|
** aiTblOrder[] arrays at the same time. */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
|
|
);
|
|
}
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
|
|
if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
|
|
for(i=iOrder; i<pIter->nTblCol; i++){
|
|
if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
|
|
}
|
|
if( i==pIter->nTblCol ){
|
|
p->rc = SQLITE_ERROR;
|
|
p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
|
|
pIter->zDataTbl, zName
|
|
);
|
|
}else{
|
|
int iPk = sqlite3_column_int(pStmt, 5);
|
|
int bNotNull = sqlite3_column_int(pStmt, 3);
|
|
const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
|
|
|
|
if( i!=iOrder ){
|
|
SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
|
|
SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
|
|
}
|
|
|
|
pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
|
|
pIter->abTblPk[iOrder] = (iPk!=0);
|
|
pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
|
|
iOrder++;
|
|
}
|
|
}
|
|
|
|
rbuFinalize(p, pStmt);
|
|
rbuObjIterCacheIndexedCols(p, pIter);
|
|
assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
|
|
}
|
|
|
|
return p->rc;
|
|
}
|
|
|
|
/*
|
|
** This function constructs and returns a pointer to a nul-terminated
|
|
** string containing some SQL clause or list based on one or more of the
|
|
** column names currently stored in the pIter->azTblCol[] array.
|
|
*/
|
|
static char *rbuObjIterGetCollist(
|
|
sqlite3rbu *p, /* RBU object */
|
|
RbuObjIter *pIter /* Object iterator for column names */
|
|
){
|
|
char *zList = 0;
|
|
const char *zSep = "";
|
|
int i;
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
const char *z = pIter->azTblCol[i];
|
|
zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
|
|
zSep = ", ";
|
|
}
|
|
return zList;
|
|
}
|
|
|
|
/*
|
|
** This function is used to create a SELECT list (the list of SQL
|
|
** expressions that follows a SELECT keyword) for a SELECT statement
|
|
** used to read from an data_xxx or rbu_tmp_xxx table while updating the
|
|
** index object currently indicated by the iterator object passed as the
|
|
** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
|
|
** to obtain the required information.
|
|
**
|
|
** If the index is of the following form:
|
|
**
|
|
** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
|
|
**
|
|
** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
|
|
** "ipk", the returned string is:
|
|
**
|
|
** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
|
|
**
|
|
** As well as the returned string, three other malloc'd strings are
|
|
** returned via output parameters. As follows:
|
|
**
|
|
** pzImposterCols: ...
|
|
** pzImposterPk: ...
|
|
** pzWhere: ...
|
|
*/
|
|
static char *rbuObjIterGetIndexCols(
|
|
sqlite3rbu *p, /* RBU object */
|
|
RbuObjIter *pIter, /* Object iterator for column names */
|
|
char **pzImposterCols, /* OUT: Columns for imposter table */
|
|
char **pzImposterPk, /* OUT: Imposter PK clause */
|
|
char **pzWhere, /* OUT: WHERE clause */
|
|
int *pnBind /* OUT: Trbul number of columns */
|
|
){
|
|
int rc = p->rc; /* Error code */
|
|
int rc2; /* sqlite3_finalize() return code */
|
|
char *zRet = 0; /* String to return */
|
|
char *zImpCols = 0; /* String to return via *pzImposterCols */
|
|
char *zImpPK = 0; /* String to return via *pzImposterPK */
|
|
char *zWhere = 0; /* String to return via *pzWhere */
|
|
int nBind = 0; /* Value to return via *pnBind */
|
|
const char *zCom = ""; /* Set to ", " later on */
|
|
const char *zAnd = ""; /* Set to " AND " later on */
|
|
sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
|
|
|
|
if( rc==SQLITE_OK ){
|
|
assert( p->zErrmsg==0 );
|
|
rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
|
|
);
|
|
}
|
|
|
|
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
|
int iCid = sqlite3_column_int(pXInfo, 1);
|
|
int bDesc = sqlite3_column_int(pXInfo, 3);
|
|
const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
|
|
const char *zCol;
|
|
const char *zType;
|
|
|
|
if( iCid<0 ){
|
|
/* An integer primary key. If the table has an explicit IPK, use
|
|
** its name. Otherwise, use "rbu_rowid". */
|
|
if( pIter->eType==RBU_PK_IPK ){
|
|
int i;
|
|
for(i=0; pIter->abTblPk[i]==0; i++);
|
|
assert( i<pIter->nTblCol );
|
|
zCol = pIter->azTblCol[i];
|
|
}else{
|
|
zCol = "rbu_rowid";
|
|
}
|
|
zType = "INTEGER";
|
|
}else{
|
|
zCol = pIter->azTblCol[iCid];
|
|
zType = pIter->azTblType[iCid];
|
|
}
|
|
|
|
zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
|
|
if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
|
|
const char *zOrder = (bDesc ? " DESC" : "");
|
|
zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
|
|
zImpPK, zCom, nBind, zCol, zOrder
|
|
);
|
|
}
|
|
zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
|
|
zImpCols, zCom, nBind, zCol, zType, zCollate
|
|
);
|
|
zWhere = sqlite3_mprintf(
|
|
"%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
|
|
);
|
|
if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
|
|
zCom = ", ";
|
|
zAnd = " AND ";
|
|
nBind++;
|
|
}
|
|
|
|
rc2 = sqlite3_finalize(pXInfo);
|
|
if( rc==SQLITE_OK ) rc = rc2;
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
sqlite3_free(zRet);
|
|
sqlite3_free(zImpCols);
|
|
sqlite3_free(zImpPK);
|
|
sqlite3_free(zWhere);
|
|
zRet = 0;
|
|
zImpCols = 0;
|
|
zImpPK = 0;
|
|
zWhere = 0;
|
|
p->rc = rc;
|
|
}
|
|
|
|
*pzImposterCols = zImpCols;
|
|
*pzImposterPk = zImpPK;
|
|
*pzWhere = zWhere;
|
|
*pnBind = nBind;
|
|
return zRet;
|
|
}
|
|
|
|
/*
|
|
** Assuming the current table columns are "a", "b" and "c", and the zObj
|
|
** paramter is passed "old", return a string of the form:
|
|
**
|
|
** "old.a, old.b, old.b"
|
|
**
|
|
** With the column names escaped.
|
|
**
|
|
** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
|
|
** the text ", old._rowid_" to the returned value.
|
|
*/
|
|
static char *rbuObjIterGetOldlist(
|
|
sqlite3rbu *p,
|
|
RbuObjIter *pIter,
|
|
const char *zObj
|
|
){
|
|
char *zList = 0;
|
|
if( p->rc==SQLITE_OK && pIter->abIndexed ){
|
|
const char *zS = "";
|
|
int i;
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
if( pIter->abIndexed[i] ){
|
|
const char *zCol = pIter->azTblCol[i];
|
|
zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
|
|
}else{
|
|
zList = sqlite3_mprintf("%z%sNULL", zList, zS);
|
|
}
|
|
zS = ", ";
|
|
if( zList==0 ){
|
|
p->rc = SQLITE_NOMEM;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* For a table with implicit rowids, append "old._rowid_" to the list. */
|
|
if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
|
|
zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
|
|
}
|
|
}
|
|
return zList;
|
|
}
|
|
|
|
/*
|
|
** Return an expression that can be used in a WHERE clause to match the
|
|
** primary key of the current table. For example, if the table is:
|
|
**
|
|
** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
|
|
**
|
|
** Return the string:
|
|
**
|
|
** "b = ?1 AND c = ?2"
|
|
*/
|
|
static char *rbuObjIterGetWhere(
|
|
sqlite3rbu *p,
|
|
RbuObjIter *pIter
|
|
){
|
|
char *zList = 0;
|
|
if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
|
|
zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
|
|
}else if( pIter->eType==RBU_PK_EXTERNAL ){
|
|
const char *zSep = "";
|
|
int i;
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
if( pIter->abTblPk[i] ){
|
|
zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
|
|
zSep = " AND ";
|
|
}
|
|
}
|
|
zList = rbuMPrintf(p,
|
|
"_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
|
|
);
|
|
|
|
}else{
|
|
const char *zSep = "";
|
|
int i;
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
if( pIter->abTblPk[i] ){
|
|
const char *zCol = pIter->azTblCol[i];
|
|
zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
|
|
zSep = " AND ";
|
|
}
|
|
}
|
|
}
|
|
return zList;
|
|
}
|
|
|
|
/*
|
|
** The SELECT statement iterating through the keys for the current object
|
|
** (p->objiter.pSelect) currently points to a valid row. However, there
|
|
** is something wrong with the rbu_control value in the rbu_control value
|
|
** stored in the (p->nCol+1)'th column. Set the error code and error message
|
|
** of the RBU handle to something reflecting this.
|
|
*/
|
|
static void rbuBadControlError(sqlite3rbu *p){
|
|
p->rc = SQLITE_ERROR;
|
|
p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
|
|
}
|
|
|
|
|
|
/*
|
|
** Return a nul-terminated string containing the comma separated list of
|
|
** assignments that should be included following the "SET" keyword of
|
|
** an UPDATE statement used to update the table object that the iterator
|
|
** passed as the second argument currently points to if the rbu_control
|
|
** column of the data_xxx table entry is set to zMask.
|
|
**
|
|
** The memory for the returned string is obtained from sqlite3_malloc().
|
|
** It is the responsibility of the caller to eventually free it using
|
|
** sqlite3_free().
|
|
**
|
|
** If an OOM error is encountered when allocating space for the new
|
|
** string, an error code is left in the rbu handle passed as the first
|
|
** argument and NULL is returned. Or, if an error has already occurred
|
|
** when this function is called, NULL is returned immediately, without
|
|
** attempting the allocation or modifying the stored error code.
|
|
*/
|
|
static char *rbuObjIterGetSetlist(
|
|
sqlite3rbu *p,
|
|
RbuObjIter *pIter,
|
|
const char *zMask
|
|
){
|
|
char *zList = 0;
|
|
if( p->rc==SQLITE_OK ){
|
|
int i;
|
|
|
|
if( (int)strlen(zMask)!=pIter->nTblCol ){
|
|
rbuBadControlError(p);
|
|
}else{
|
|
const char *zSep = "";
|
|
for(i=0; i<pIter->nTblCol; i++){
|
|
char c = zMask[pIter->aiSrcOrder[i]];
|
|
if( c=='x' ){
|
|
zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
|
|
zList, zSep, pIter->azTblCol[i], i+1
|
|
);
|
|
zSep = ", ";
|
|
}
|
|
else if( c=='d' ){
|
|
zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
|
|
zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
|
|
);
|
|
zSep = ", ";
|
|
}
|
|
else if( c=='f' ){
|
|
zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
|
|
zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
|
|
);
|
|
zSep = ", ";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return zList;
|
|
}
|
|
|
|
/*
|
|
** Return a nul-terminated string consisting of nByte comma separated
|
|
** "?" expressions. For example, if nByte is 3, return a pointer to
|
|
** a buffer containing the string "?,?,?".
|
|
**
|
|
** The memory for the returned string is obtained from sqlite3_malloc().
|
|
** It is the responsibility of the caller to eventually free it using
|
|
** sqlite3_free().
|
|
**
|
|
** If an OOM error is encountered when allocating space for the new
|
|
** string, an error code is left in the rbu handle passed as the first
|
|
** argument and NULL is returned. Or, if an error has already occurred
|
|
** when this function is called, NULL is returned immediately, without
|
|
** attempting the allocation or modifying the stored error code.
|
|
*/
|
|
static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
|
|
char *zRet = 0;
|
|
int nByte = nBind*2 + 1;
|
|
|
|
zRet = (char*)rbuMalloc(p, nByte);
|
|
if( zRet ){
|
|
int i;
|
|
for(i=0; i<nBind; i++){
|
|
zRet[i*2] = '?';
|
|
zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
|
|
}
|
|
}
|
|
return zRet;
|
|
}
|
|
|
|
/*
|
|
** The iterator currently points to a table (not index) of type
|
|
** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
|
|
** declaration for the corresponding imposter table. For example,
|
|
** if the iterator points to a table created as:
|
|
**
|
|
** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
|
|
**
|
|
** this function returns:
|
|
**
|
|
** PRIMARY KEY("b", "a" DESC)
|
|
*/
|
|
static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
|
|
char *z = 0;
|
|
assert( pIter->zIdx==0 );
|
|
if( p->rc==SQLITE_OK ){
|
|
const char *zSep = "PRIMARY KEY(";
|
|
sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
|
|
sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
|
|
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
|
|
);
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
|
|
const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
|
|
if( zOrig && strcmp(zOrig, "pk")==0 ){
|
|
const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
|
|
if( zIdx ){
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
|
|
);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
rbuFinalize(p, pXList);
|
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
|
if( sqlite3_column_int(pXInfo, 5) ){
|
|
/* int iCid = sqlite3_column_int(pXInfo, 0); */
|
|
const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
|
|
const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
|
|
z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
|
|
zSep = ", ";
|
|
}
|
|
}
|
|
z = rbuMPrintf(p, "%z)", z);
|
|
rbuFinalize(p, pXInfo);
|
|
}
|
|
return z;
|
|
}
|
|
|
|
/*
|
|
** This function creates the second imposter table used when writing to
|
|
** a table b-tree where the table has an external primary key. If the
|
|
** iterator passed as the second argument does not currently point to
|
|
** a table (not index) with an external primary key, this function is a
|
|
** no-op.
|
|
**
|
|
** Assuming the iterator does point to a table with an external PK, this
|
|
** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
|
|
** used to access that PK index. For example, if the target table is
|
|
** declared as follows:
|
|
**
|
|
** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
|
|
**
|
|
** then the imposter table schema is:
|
|
**
|
|
** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
|
|
**
|
|
*/
|
|
static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
|
|
if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
|
|
int tnum = pIter->iPkTnum; /* Root page of PK index */
|
|
sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
|
|
const char *zIdx = 0; /* Name of PK index */
|
|
sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
|
|
const char *zComma = "";
|
|
char *zCols = 0; /* Used to build up list of table cols */
|
|
char *zPk = 0; /* Used to build up table PK declaration */
|
|
|
|
/* Figure out the name of the primary key index for the current table.
|
|
** This is needed for the argument to "PRAGMA index_xinfo". Set
|
|
** zIdx to point to a nul-terminated string containing this name. */
|
|
p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
|
|
"SELECT name FROM sqlite_master WHERE rootpage = ?"
|
|
);
|
|
if( p->rc==SQLITE_OK ){
|
|
sqlite3_bind_int(pQuery, 1, tnum);
|
|
if( SQLITE_ROW==sqlite3_step(pQuery) ){
|
|
zIdx = (const char*)sqlite3_column_text(pQuery, 0);
|
|
}
|
|
}
|
|
if( zIdx ){
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
|
|
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
|
|
);
|
|
}
|
|
rbuFinalize(p, pQuery);
|
|
|
|
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
|
|
int bKey = sqlite3_column_int(pXInfo, 5);
|
|
if( bKey ){
|
|
int iCid = sqlite3_column_int(pXInfo, 1);
|
|
int bDesc = sqlite3_column_int(pXInfo, 3);
|
|
const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
|
|
zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
|
|
iCid, pIter->azTblType[iCid], zCollate
|
|
);
|
|
zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
|
|
zComma = ", ";
|
|
}
|
|
}
|
|
zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
|
|
rbuFinalize(p, pXInfo);
|
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
|
|
rbuMPrintfExec(p, p->dbMain,
|
|
"CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
|
|
zCols, zPk
|
|
);
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
|
|
}
|
|
}
|
|
|
|
/*
|
|
** If an error has already occurred when this function is called, it
|
|
** immediately returns zero (without doing any work). Or, if an error
|
|
** occurs during the execution of this function, it sets the error code
|
|
** in the sqlite3rbu object indicated by the first argument and returns
|
|
** zero.
|
|
**
|
|
** The iterator passed as the second argument is guaranteed to point to
|
|
** a table (not an index) when this function is called. This function
|
|
** attempts to create any imposter table required to write to the main
|
|
** table b-tree of the table before returning. Non-zero is returned if
|
|
** an imposter table are created, or zero otherwise.
|
|
**
|
|
** An imposter table is required in all cases except RBU_PK_VTAB. Only
|
|
** virtual tables are written to directly. The imposter table has the
|
|
** same schema as the actual target table (less any UNIQUE constraints).
|
|
** More precisely, the "same schema" means the same columns, types,
|
|
** collation sequences. For tables that do not have an external PRIMARY
|
|
** KEY, it also means the same PRIMARY KEY declaration.
|
|
*/
|
|
static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
|
|
if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
|
|
int tnum = pIter->iTnum;
|
|
const char *zComma = "";
|
|
char *zSql = 0;
|
|
int iCol;
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
|
|
|
|
for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
|
|
const char *zPk = "";
|
|
const char *zCol = pIter->azTblCol[iCol];
|
|
const char *zColl = 0;
|
|
|
|
p->rc = sqlite3_table_column_metadata(
|
|
p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
|
|
);
|
|
|
|
if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
|
|
/* If the target table column is an "INTEGER PRIMARY KEY", add
|
|
** "PRIMARY KEY" to the imposter table column declaration. */
|
|
zPk = "PRIMARY KEY ";
|
|
}
|
|
zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
|
|
zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
|
|
(pIter->abNotNull[iCol] ? " NOT NULL" : "")
|
|
);
|
|
zComma = ", ";
|
|
}
|
|
|
|
if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
|
|
char *zPk = rbuWithoutRowidPK(p, pIter);
|
|
if( zPk ){
|
|
zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
|
|
}
|
|
}
|
|
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
|
|
rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
|
|
pIter->zTbl, zSql,
|
|
(pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
|
|
);
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
|
|
** Specifically a statement of the form:
|
|
**
|
|
** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
|
|
**
|
|
** The number of bound variables is equal to the number of columns in
|
|
** the target table, plus one (for the rbu_control column), plus one more
|
|
** (for the rbu_rowid column) if the target table is an implicit IPK or
|
|
** virtual table.
|
|
*/
|
|
static void rbuObjIterPrepareTmpInsert(
|
|
sqlite3rbu *p,
|
|
RbuObjIter *pIter,
|
|
const char *zCollist,
|
|
const char *zRbuRowid
|
|
){
|
|
int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
|
|
char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
|
|
if( zBind ){
|
|
assert( pIter->pTmpInsert==0 );
|
|
p->rc = prepareFreeAndCollectError(
|
|
p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
|
|
"INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
|
|
p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
|
|
));
|
|
}
|
|
}
|
|
|
|
static void rbuTmpInsertFunc(
|
|
sqlite3_context *pCtx,
|
|
int nVal,
|
|
sqlite3_value **apVal
|
|
){
|
|
sqlite3rbu *p = sqlite3_user_data(pCtx);
|
|
int rc = SQLITE_OK;
|
|
int i;
|
|
|
|
for(i=0; rc==SQLITE_OK && i<nVal; i++){
|
|
rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
|
|
}
|
|
if( rc==SQLITE_OK ){
|
|
sqlite3_step(p->objiter.pTmpInsert);
|
|
rc = sqlite3_reset(p->objiter.pTmpInsert);
|
|
}
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
sqlite3_result_error_code(pCtx, rc);
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Ensure that the SQLite statement handles required to update the
|
|
** target database object currently indicated by the iterator passed
|
|
** as the second argument are available.
|
|
*/
|
|
static int rbuObjIterPrepareAll(
|
|
sqlite3rbu *p,
|
|
RbuObjIter *pIter,
|
|
int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
|
|
){
|
|
assert( pIter->bCleanup==0 );
|
|
if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
|
|
const int tnum = pIter->iTnum;
|
|
char *zCollist = 0; /* List of indexed columns */
|
|
char **pz = &p->zErrmsg;
|
|
const char *zIdx = pIter->zIdx;
|
|
char *zLimit = 0;
|
|
|
|
if( nOffset ){
|
|
zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
|
|
if( !zLimit ) p->rc = SQLITE_NOMEM;
|
|
}
|
|
|
|
if( zIdx ){
|
|
const char *zTbl = pIter->zTbl;
|
|
char *zImposterCols = 0; /* Columns for imposter table */
|
|
char *zImposterPK = 0; /* Primary key declaration for imposter */
|
|
char *zWhere = 0; /* WHERE clause on PK columns */
|
|
char *zBind = 0;
|
|
int nBind = 0;
|
|
|
|
assert( pIter->eType!=RBU_PK_VTAB );
|
|
zCollist = rbuObjIterGetIndexCols(
|
|
p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
|
|
);
|
|
zBind = rbuObjIterGetBindlist(p, nBind);
|
|
|
|
/* Create the imposter table used to write to this index. */
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
|
|
rbuMPrintfExec(p, p->dbMain,
|
|
"CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
|
|
zTbl, zImposterCols, zImposterPK
|
|
);
|
|
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
|
|
|
|
/* Create the statement to insert index entries */
|
|
pIter->nCol = nBind;
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = prepareFreeAndCollectError(
|
|
p->dbMain, &pIter->pInsert, &p->zErrmsg,
|
|
sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
|
|
);
|
|
}
|
|
|
|
/* And to delete index entries */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = prepareFreeAndCollectError(
|
|
p->dbMain, &pIter->pDelete, &p->zErrmsg,
|
|
sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
|
|
);
|
|
}
|
|
|
|
/* Create the SELECT statement to read keys in sorted order */
|
|
if( p->rc==SQLITE_OK ){
|
|
char *zSql;
|
|
if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
|
|
zSql = sqlite3_mprintf(
|
|
"SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
|
|
zCollist, p->zStateDb, pIter->zDataTbl,
|
|
zCollist, zLimit
|
|
);
|
|
}else{
|
|
zSql = sqlite3_mprintf(
|
|
"SELECT %s, rbu_control FROM '%q' "
|
|
"WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
|
|
"UNION ALL "
|
|
"SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
|
|
"ORDER BY %s%s",
|
|
zCollist, pIter->zDataTbl,
|
|
zCollist, p->zStateDb, pIter->zDataTbl,
|
|
zCollist, zLimit
|
|
);
|
|
}
|
|
p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
|
|
}
|
|
|
|
sqlite3_free(zImposterCols);
|
|
sqlite3_free(zImposterPK);
|
|
sqlite3_free(zWhere);
|
|
sqlite3_free(zBind);
|
|
}else{
|
|
int bRbuRowid = (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE);
|
|
const char *zTbl = pIter->zTbl; /* Table this step applies to */
|
|
const char *zWrite; /* Imposter table name */
|
|
|
|
char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
|
|
char *zWhere = rbuObjIterGetWhere(p, pIter);
|
|
char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
|
|
char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
|
|
|
|
zCollist = rbuObjIterGetCollist(p, pIter);
|
|
pIter->nCol = pIter->nTblCol;
|
|
|
|
/* Create the imposter table or tables (if required). */
|
|
rbuCreateImposterTable(p, pIter);
|
|
rbuCreateImposterTable2(p, pIter);
|
|
zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
|
|
|
|
/* Create the INSERT statement to write to the target PK b-tree */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
|
|
sqlite3_mprintf(
|
|
"INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
|
|
zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
|
|
)
|
|
);
|
|
}
|
|
|
|
/* Create the DELETE statement to write to the target PK b-tree */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
|
|
sqlite3_mprintf(
|
|
"DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
|
|
)
|
|
);
|
|
}
|
|
|
|
if( pIter->abIndexed ){
|
|
const char *zRbuRowid = "";
|
|
if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
|
|
zRbuRowid = ", rbu_rowid";
|
|
}
|
|
|
|
/* Create the rbu_tmp_xxx table and the triggers to populate it. */
|
|
rbuMPrintfExec(p, p->dbRbu,
|
|
"CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
|
|
"SELECT *%s FROM '%q' WHERE 0;"
|
|
, p->zStateDb, pIter->zDataTbl
|
|
, (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
|
|
, pIter->zDataTbl
|
|
);
|
|
|
|
rbuMPrintfExec(p, p->dbMain,
|
|
"CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
|
|
"BEGIN "
|
|
" SELECT rbu_tmp_insert(2, %s);"
|
|
"END;"
|
|
|
|
"CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
|
|
"BEGIN "
|
|
" SELECT rbu_tmp_insert(2, %s);"
|
|
"END;"
|
|
|
|
"CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
|
|
"BEGIN "
|
|
" SELECT rbu_tmp_insert(3, %s);"
|
|
"END;",
|
|
zWrite, zTbl, zOldlist,
|
|
zWrite, zTbl, zOldlist,
|
|
zWrite, zTbl, zNewlist
|
|
);
|
|
|
|
if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
|
|
rbuMPrintfExec(p, p->dbMain,
|
|
"CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
|
|
"BEGIN "
|
|
" SELECT rbu_tmp_insert(0, %s);"
|
|
"END;",
|
|
zWrite, zTbl, zNewlist
|
|
);
|
|
}
|
|
|
|
rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
|
|
}
|
|
|
|
/* Create the SELECT statement to read keys from data_xxx */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
|
|
sqlite3_mprintf(
|
|
"SELECT %s, rbu_control%s FROM '%q'%s",
|
|
zCollist, (bRbuRowid ? ", rbu_rowid" : ""),
|
|
pIter->zDataTbl, zLimit
|
|
)
|
|
);
|
|
}
|
|
|
|
sqlite3_free(zWhere);
|
|
sqlite3_free(zOldlist);
|
|
sqlite3_free(zNewlist);
|
|
sqlite3_free(zBindings);
|
|
}
|
|
sqlite3_free(zCollist);
|
|
sqlite3_free(zLimit);
|
|
}
|
|
|
|
return p->rc;
|
|
}
|
|
|
|
/*
|
|
** Set output variable *ppStmt to point to an UPDATE statement that may
|
|
** be used to update the imposter table for the main table b-tree of the
|
|
** table object that pIter currently points to, assuming that the
|
|
** rbu_control column of the data_xyz table contains zMask.
|
|
**
|
|
** If the zMask string does not specify any columns to update, then this
|
|
** is not an error. Output variable *ppStmt is set to NULL in this case.
|
|
*/
|
|
static int rbuGetUpdateStmt(
|
|
sqlite3rbu *p, /* RBU handle */
|
|
RbuObjIter *pIter, /* Object iterator */
|
|
const char *zMask, /* rbu_control value ('x.x.') */
|
|
sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
|
|
){
|
|
RbuUpdateStmt **pp;
|
|
RbuUpdateStmt *pUp = 0;
|
|
int nUp = 0;
|
|
|
|
/* In case an error occurs */
|
|
*ppStmt = 0;
|
|
|
|
/* Search for an existing statement. If one is found, shift it to the front
|
|
** of the LRU queue and return immediately. Otherwise, leave nUp pointing
|
|
** to the number of statements currently in the cache and pUp to the
|
|
** last object in the list. */
|
|
for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
|
|
pUp = *pp;
|
|
if( strcmp(pUp->zMask, zMask)==0 ){
|
|
*pp = pUp->pNext;
|
|
pUp->pNext = pIter->pRbuUpdate;
|
|
pIter->pRbuUpdate = pUp;
|
|
*ppStmt = pUp->pUpdate;
|
|
return SQLITE_OK;
|
|
}
|
|
nUp++;
|
|
}
|
|
assert( pUp==0 || pUp->pNext==0 );
|
|
|
|
if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
|
|
for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
|
|
*pp = 0;
|
|
sqlite3_finalize(pUp->pUpdate);
|
|
pUp->pUpdate = 0;
|
|
}else{
|
|
pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
|
|
}
|
|
|
|
if( pUp ){
|
|
char *zWhere = rbuObjIterGetWhere(p, pIter);
|
|
char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
|
|
char *zUpdate = 0;
|
|
|
|
pUp->zMask = (char*)&pUp[1];
|
|
memcpy(pUp->zMask, zMask, pIter->nTblCol);
|
|
pUp->pNext = pIter->pRbuUpdate;
|
|
pIter->pRbuUpdate = pUp;
|
|
|
|
if( zSet ){
|
|
const char *zPrefix = "";
|
|
|
|
if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
|
|
zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
|
|
zPrefix, pIter->zTbl, zSet, zWhere
|
|
);
|
|
p->rc = prepareFreeAndCollectError(
|
|
p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
|
|
);
|
|
*ppStmt = pUp->pUpdate;
|
|
}
|
|
sqlite3_free(zWhere);
|
|
sqlite3_free(zSet);
|
|
}
|
|
|
|
return p->rc;
|
|
}
|
|
|
|
static sqlite3 *rbuOpenDbhandle(sqlite3rbu *p, const char *zName){
|
|
sqlite3 *db = 0;
|
|
if( p->rc==SQLITE_OK ){
|
|
const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
|
|
p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
|
|
if( p->rc ){
|
|
p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
|
|
sqlite3_close(db);
|
|
db = 0;
|
|
}
|
|
}
|
|
return db;
|
|
}
|
|
|
|
/*
|
|
** Open the database handle and attach the RBU database as "rbu". If an
|
|
** error occurs, leave an error code and message in the RBU handle.
|
|
*/
|
|
static void rbuOpenDatabase(sqlite3rbu *p){
|
|
assert( p->rc==SQLITE_OK );
|
|
assert( p->dbMain==0 && p->dbRbu==0 );
|
|
|
|
p->eStage = 0;
|
|
p->dbMain = rbuOpenDbhandle(p, p->zTarget);
|
|
p->dbRbu = rbuOpenDbhandle(p, p->zRbu);
|
|
|
|
/* If using separate RBU and state databases, attach the state database to
|
|
** the RBU db handle now. */
|
|
if( p->zState ){
|
|
rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
|
|
memcpy(p->zStateDb, "stat", 4);
|
|
}else{
|
|
memcpy(p->zStateDb, "main", 4);
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_create_function(p->dbMain,
|
|
"rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
|
|
);
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_create_function(p->dbMain,
|
|
"rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
|
|
);
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_create_function(p->dbRbu,
|
|
"rbu_target_name", 1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
|
|
);
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
|
|
}
|
|
rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
|
|
|
|
/* Mark the database file just opened as an RBU target database. If
|
|
** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
|
|
** This is an error. */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
|
|
}
|
|
|
|
if( p->rc==SQLITE_NOTFOUND ){
|
|
p->rc = SQLITE_ERROR;
|
|
p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
|
|
}
|
|
}
|
|
|
|
/*
|
|
** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
|
|
** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
|
|
**
|
|
** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
|
|
** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
|
|
** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
|
|
** three characters, then shorten the suffix on z[] to be the last three
|
|
** characters of the original suffix.
|
|
**
|
|
** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
|
|
** do the suffix shortening regardless of URI parameter.
|
|
**
|
|
** Examples:
|
|
**
|
|
** test.db-journal => test.nal
|
|
** test.db-wal => test.wal
|
|
** test.db-shm => test.shm
|
|
** test.db-mj7f3319fa => test.9fa
|
|
*/
|
|
static void rbuFileSuffix3(const char *zBase, char *z){
|
|
#ifdef SQLITE_ENABLE_8_3_NAMES
|
|
#if SQLITE_ENABLE_8_3_NAMES<2
|
|
if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
|
|
#endif
|
|
{
|
|
int i, sz;
|
|
sz = sqlite3Strlen30(z);
|
|
for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
|
|
if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
** Return the current wal-index header checksum for the target database
|
|
** as a 64-bit integer.
|
|
**
|
|
** The checksum is store in the first page of xShmMap memory as an 8-byte
|
|
** blob starting at byte offset 40.
|
|
*/
|
|
static i64 rbuShmChecksum(sqlite3rbu *p){
|
|
i64 iRet = 0;
|
|
if( p->rc==SQLITE_OK ){
|
|
sqlite3_file *pDb = p->pTargetFd->pReal;
|
|
u32 volatile *ptr;
|
|
p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
|
|
if( p->rc==SQLITE_OK ){
|
|
iRet = ((i64)ptr[10] << 32) + ptr[11];
|
|
}
|
|
}
|
|
return iRet;
|
|
}
|
|
|
|
/*
|
|
** This function is called as part of initializing or reinitializing an
|
|
** incremental checkpoint.
|
|
**
|
|
** It populates the sqlite3rbu.aFrame[] array with the set of
|
|
** (wal frame -> db page) copy operations required to checkpoint the
|
|
** current wal file, and obtains the set of shm locks required to safely
|
|
** perform the copy operations directly on the file-system.
|
|
**
|
|
** If argument pState is not NULL, then the incremental checkpoint is
|
|
** being resumed. In this case, if the checksum of the wal-index-header
|
|
** following recovery is not the same as the checksum saved in the RbuState
|
|
** object, then the rbu handle is set to DONE state. This occurs if some
|
|
** other client appends a transaction to the wal file in the middle of
|
|
** an incremental checkpoint.
|
|
*/
|
|
static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
|
|
|
|
/* If pState is NULL, then the wal file may not have been opened and
|
|
** recovered. Running a read-statement here to ensure that doing so
|
|
** does not interfere with the "capture" process below. */
|
|
if( pState==0 ){
|
|
p->eStage = 0;
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
|
|
}
|
|
}
|
|
|
|
/* Assuming no error has occurred, run a "restart" checkpoint with the
|
|
** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
|
|
** special behaviour in the rbu VFS:
|
|
**
|
|
** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
|
|
** the checkpoint fails with SQLITE_BUSY (normally SQLite would
|
|
** proceed with running a passive checkpoint instead of failing).
|
|
**
|
|
** * Attempts to read from the *-wal file or write to the database file
|
|
** do not perform any IO. Instead, the frame/page combinations that
|
|
** would be read/written are recorded in the sqlite3rbu.aFrame[]
|
|
** array.
|
|
**
|
|
** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
|
|
** READ0 and CHECKPOINT locks taken as part of the checkpoint are
|
|
** no-ops. These locks will not be released until the connection
|
|
** is closed.
|
|
**
|
|
** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
|
|
** error.
|
|
**
|
|
** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
|
|
** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
|
|
** array populated with a set of (frame -> page) mappings. Because the
|
|
** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
|
|
** data from the wal file into the database file according to the
|
|
** contents of aFrame[].
|
|
*/
|
|
if( p->rc==SQLITE_OK ){
|
|
int rc2;
|
|
p->eStage = RBU_STAGE_CAPTURE;
|
|
rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
|
|
if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
p->eStage = RBU_STAGE_CKPT;
|
|
p->nStep = (pState ? pState->nRow : 0);
|
|
p->aBuf = rbuMalloc(p, p->pgsz);
|
|
p->iWalCksum = rbuShmChecksum(p);
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
|
|
p->rc = SQLITE_DONE;
|
|
p->eStage = RBU_STAGE_DONE;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Called when iAmt bytes are read from offset iOff of the wal file while
|
|
** the rbu object is in capture mode. Record the frame number of the frame
|
|
** being read in the aFrame[] array.
|
|
*/
|
|
static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
|
|
const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
|
|
u32 iFrame;
|
|
|
|
if( pRbu->mLock!=mReq ){
|
|
pRbu->rc = SQLITE_BUSY;
|
|
return SQLITE_INTERNAL;
|
|
}
|
|
|
|
pRbu->pgsz = iAmt;
|
|
if( pRbu->nFrame==pRbu->nFrameAlloc ){
|
|
int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
|
|
RbuFrame *aNew;
|
|
aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
|
|
if( aNew==0 ) return SQLITE_NOMEM;
|
|
pRbu->aFrame = aNew;
|
|
pRbu->nFrameAlloc = nNew;
|
|
}
|
|
|
|
iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
|
|
if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
|
|
pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
|
|
pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
|
|
pRbu->nFrame++;
|
|
return SQLITE_OK;
|
|
}
|
|
|
|
/*
|
|
** Called when a page of data is written to offset iOff of the database
|
|
** file while the rbu handle is in capture mode. Record the page number
|
|
** of the page being written in the aFrame[] array.
|
|
*/
|
|
static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
|
|
pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
|
|
return SQLITE_OK;
|
|
}
|
|
|
|
/*
|
|
** This is called as part of an incremental checkpoint operation. Copy
|
|
** a single frame of data from the wal file into the database file, as
|
|
** indicated by the RbuFrame object.
|
|
*/
|
|
static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
|
|
sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
|
|
sqlite3_file *pDb = p->pTargetFd->pReal;
|
|
i64 iOff;
|
|
|
|
assert( p->rc==SQLITE_OK );
|
|
iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
|
|
p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
|
|
if( p->rc ) return;
|
|
|
|
iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
|
|
p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
|
|
}
|
|
|
|
|
|
/*
|
|
** Take an EXCLUSIVE lock on the database file.
|
|
*/
|
|
static void rbuLockDatabase(sqlite3rbu *p){
|
|
sqlite3_file *pReal = p->pTargetFd->pReal;
|
|
assert( p->rc==SQLITE_OK );
|
|
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
|
|
}
|
|
}
|
|
|
|
#if defined(_WIN32_WCE)
|
|
static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
|
|
int nChar;
|
|
LPWSTR zWideFilename;
|
|
|
|
nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
|
|
if( nChar==0 ){
|
|
return 0;
|
|
}
|
|
zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
|
|
if( zWideFilename==0 ){
|
|
return 0;
|
|
}
|
|
memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
|
|
nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
|
|
nChar);
|
|
if( nChar==0 ){
|
|
sqlite3_free(zWideFilename);
|
|
zWideFilename = 0;
|
|
}
|
|
return zWideFilename;
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
|
|
** on the database file. This proc moves the *-oal file to the *-wal path,
|
|
** then reopens the database file (this time in vanilla, non-oal, WAL mode).
|
|
** If an error occurs, leave an error code and error message in the rbu
|
|
** handle.
|
|
*/
|
|
static void rbuMoveOalFile(sqlite3rbu *p){
|
|
const char *zBase = sqlite3_db_filename(p->dbMain, "main");
|
|
|
|
char *zWal = sqlite3_mprintf("%s-wal", zBase);
|
|
char *zOal = sqlite3_mprintf("%s-oal", zBase);
|
|
|
|
assert( p->eStage==RBU_STAGE_MOVE );
|
|
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
|
|
if( zWal==0 || zOal==0 ){
|
|
p->rc = SQLITE_NOMEM;
|
|
}else{
|
|
/* Move the *-oal file to *-wal. At this point connection p->db is
|
|
** holding a SHARED lock on the target database file (because it is
|
|
** in WAL mode). So no other connection may be writing the db.
|
|
**
|
|
** In order to ensure that there are no database readers, an EXCLUSIVE
|
|
** lock is obtained here before the *-oal is moved to *-wal.
|
|
*/
|
|
rbuLockDatabase(p);
|
|
if( p->rc==SQLITE_OK ){
|
|
rbuFileSuffix3(zBase, zWal);
|
|
rbuFileSuffix3(zBase, zOal);
|
|
|
|
/* Re-open the databases. */
|
|
rbuObjIterFinalize(&p->objiter);
|
|
sqlite3_close(p->dbMain);
|
|
sqlite3_close(p->dbRbu);
|
|
p->dbMain = 0;
|
|
p->dbRbu = 0;
|
|
|
|
#if defined(_WIN32_WCE)
|
|
{
|
|
LPWSTR zWideOal;
|
|
LPWSTR zWideWal;
|
|
|
|
zWideOal = rbuWinUtf8ToUnicode(zOal);
|
|
if( zWideOal ){
|
|
zWideWal = rbuWinUtf8ToUnicode(zWal);
|
|
if( zWideWal ){
|
|
if( MoveFileW(zWideOal, zWideWal) ){
|
|
p->rc = SQLITE_OK;
|
|
}else{
|
|
p->rc = SQLITE_IOERR;
|
|
}
|
|
sqlite3_free(zWideWal);
|
|
}else{
|
|
p->rc = SQLITE_IOERR_NOMEM;
|
|
}
|
|
sqlite3_free(zWideOal);
|
|
}else{
|
|
p->rc = SQLITE_IOERR_NOMEM;
|
|
}
|
|
}
|
|
#else
|
|
p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
|
|
#endif
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
rbuOpenDatabase(p);
|
|
rbuSetupCheckpoint(p, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
sqlite3_free(zWal);
|
|
sqlite3_free(zOal);
|
|
}
|
|
|
|
/*
|
|
** The SELECT statement iterating through the keys for the current object
|
|
** (p->objiter.pSelect) currently points to a valid row. This function
|
|
** determines the type of operation requested by this row and returns
|
|
** one of the following values to indicate the result:
|
|
**
|
|
** * RBU_INSERT
|
|
** * RBU_DELETE
|
|
** * RBU_IDX_DELETE
|
|
** * RBU_UPDATE
|
|
**
|
|
** If RBU_UPDATE is returned, then output variable *pzMask is set to
|
|
** point to the text value indicating the columns to update.
|
|
**
|
|
** If the rbu_control field contains an invalid value, an error code and
|
|
** message are left in the RBU handle and zero returned.
|
|
*/
|
|
static int rbuStepType(sqlite3rbu *p, const char **pzMask){
|
|
int iCol = p->objiter.nCol; /* Index of rbu_control column */
|
|
int res = 0; /* Return value */
|
|
|
|
switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
|
|
case SQLITE_INTEGER: {
|
|
int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
|
|
if( iVal==0 ){
|
|
res = RBU_INSERT;
|
|
}else if( iVal==1 ){
|
|
res = RBU_DELETE;
|
|
}else if( iVal==2 ){
|
|
res = RBU_IDX_DELETE;
|
|
}else if( iVal==3 ){
|
|
res = RBU_IDX_INSERT;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SQLITE_TEXT: {
|
|
const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
|
|
if( z==0 ){
|
|
p->rc = SQLITE_NOMEM;
|
|
}else{
|
|
*pzMask = (const char*)z;
|
|
}
|
|
res = RBU_UPDATE;
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if( res==0 ){
|
|
rbuBadControlError(p);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
#ifdef SQLITE_DEBUG
|
|
/*
|
|
** Assert that column iCol of statement pStmt is named zName.
|
|
*/
|
|
static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
|
|
const char *zCol = sqlite3_column_name(pStmt, iCol);
|
|
assert( 0==sqlite3_stricmp(zName, zCol) );
|
|
}
|
|
#else
|
|
# define assertColumnName(x,y,z)
|
|
#endif
|
|
|
|
/*
|
|
** This function does the work for an sqlite3rbu_step() call.
|
|
**
|
|
** The object-iterator (p->objiter) currently points to a valid object,
|
|
** and the input cursor (p->objiter.pSelect) currently points to a valid
|
|
** input row. Perform whatever processing is required and return.
|
|
**
|
|
** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
|
|
** and message is left in the RBU handle and a copy of the error code
|
|
** returned.
|
|
*/
|
|
static int rbuStep(sqlite3rbu *p){
|
|
RbuObjIter *pIter = &p->objiter;
|
|
const char *zMask = 0;
|
|
int i;
|
|
int eType = rbuStepType(p, &zMask);
|
|
|
|
if( eType ){
|
|
assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
|
|
|
|
if( pIter->zIdx==0 && eType==RBU_IDX_DELETE ){
|
|
rbuBadControlError(p);
|
|
}
|
|
else if(
|
|
eType==RBU_INSERT
|
|
|| eType==RBU_DELETE
|
|
|| eType==RBU_IDX_DELETE
|
|
|| eType==RBU_IDX_INSERT
|
|
){
|
|
sqlite3_value *pVal;
|
|
sqlite3_stmt *pWriter;
|
|
|
|
assert( eType!=RBU_UPDATE );
|
|
assert( eType!=RBU_DELETE || pIter->zIdx==0 );
|
|
|
|
if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
|
|
pWriter = pIter->pDelete;
|
|
}else{
|
|
pWriter = pIter->pInsert;
|
|
}
|
|
|
|
for(i=0; i<pIter->nCol; i++){
|
|
/* If this is an INSERT into a table b-tree and the table has an
|
|
** explicit INTEGER PRIMARY KEY, check that this is not an attempt
|
|
** to write a NULL into the IPK column. That is not permitted. */
|
|
if( eType==RBU_INSERT
|
|
&& pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
|
|
&& sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
|
|
){
|
|
p->rc = SQLITE_MISMATCH;
|
|
p->zErrmsg = sqlite3_mprintf("datatype mismatch");
|
|
goto step_out;
|
|
}
|
|
|
|
if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
|
|
continue;
|
|
}
|
|
|
|
pVal = sqlite3_column_value(pIter->pSelect, i);
|
|
p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
|
|
if( p->rc ) goto step_out;
|
|
}
|
|
if( pIter->zIdx==0
|
|
&& (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
|
|
){
|
|
/* For a virtual table, or a table with no primary key, the
|
|
** SELECT statement is:
|
|
**
|
|
** SELECT <cols>, rbu_control, rbu_rowid FROM ....
|
|
**
|
|
** Hence column_value(pIter->nCol+1).
|
|
*/
|
|
assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
|
|
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
|
|
p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
|
|
}
|
|
if( p->rc==SQLITE_OK ){
|
|
sqlite3_step(pWriter);
|
|
p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
|
|
}
|
|
}else{
|
|
sqlite3_value *pVal;
|
|
sqlite3_stmt *pUpdate = 0;
|
|
assert( eType==RBU_UPDATE );
|
|
rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
|
|
if( pUpdate ){
|
|
for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
|
|
char c = zMask[pIter->aiSrcOrder[i]];
|
|
pVal = sqlite3_column_value(pIter->pSelect, i);
|
|
if( pIter->abTblPk[i] || c!='.' ){
|
|
p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
|
|
}
|
|
}
|
|
if( p->rc==SQLITE_OK
|
|
&& (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
|
|
){
|
|
/* Bind the rbu_rowid value to column _rowid_ */
|
|
assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
|
|
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
|
|
p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
|
|
}
|
|
if( p->rc==SQLITE_OK ){
|
|
sqlite3_step(pUpdate);
|
|
p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
step_out:
|
|
return p->rc;
|
|
}
|
|
|
|
/*
|
|
** Increment the schema cookie of the main database opened by p->dbMain.
|
|
*/
|
|
static void rbuIncrSchemaCookie(sqlite3rbu *p){
|
|
if( p->rc==SQLITE_OK ){
|
|
int iCookie = 1000000;
|
|
sqlite3_stmt *pStmt;
|
|
|
|
p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
|
|
"PRAGMA schema_version"
|
|
);
|
|
if( p->rc==SQLITE_OK ){
|
|
/* Coverage: it may be that this sqlite3_step() cannot fail. There
|
|
** is already a transaction open, so the prepared statement cannot
|
|
** throw an SQLITE_SCHEMA exception. The only database page the
|
|
** statement reads is page 1, which is guaranteed to be in the cache.
|
|
** And no memory allocations are required. */
|
|
if( SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
iCookie = sqlite3_column_int(pStmt, 0);
|
|
}
|
|
rbuFinalize(p, pStmt);
|
|
}
|
|
if( p->rc==SQLITE_OK ){
|
|
rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Update the contents of the rbu_state table within the rbu database. The
|
|
** value stored in the RBU_STATE_STAGE column is eStage. All other values
|
|
** are determined by inspecting the rbu handle passed as the first argument.
|
|
*/
|
|
static void rbuSaveState(sqlite3rbu *p, int eStage){
|
|
if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
|
|
sqlite3_stmt *pInsert = 0;
|
|
int rc;
|
|
|
|
assert( p->zErrmsg==0 );
|
|
rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
|
|
sqlite3_mprintf(
|
|
"INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
|
|
"(%d, %d), "
|
|
"(%d, %Q), "
|
|
"(%d, %Q), "
|
|
"(%d, %d), "
|
|
"(%d, %d), "
|
|
"(%d, %lld), "
|
|
"(%d, %lld), "
|
|
"(%d, %lld) ",
|
|
p->zStateDb,
|
|
RBU_STATE_STAGE, eStage,
|
|
RBU_STATE_TBL, p->objiter.zTbl,
|
|
RBU_STATE_IDX, p->objiter.zIdx,
|
|
RBU_STATE_ROW, p->nStep,
|
|
RBU_STATE_PROGRESS, p->nProgress,
|
|
RBU_STATE_CKPT, p->iWalCksum,
|
|
RBU_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
|
|
RBU_STATE_OALSZ, p->iOalSz
|
|
)
|
|
);
|
|
assert( pInsert==0 || rc==SQLITE_OK );
|
|
|
|
if( rc==SQLITE_OK ){
|
|
sqlite3_step(pInsert);
|
|
rc = sqlite3_finalize(pInsert);
|
|
}
|
|
if( rc!=SQLITE_OK ) p->rc = rc;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
** Step the RBU object.
|
|
*/
|
|
int sqlite3rbu_step(sqlite3rbu *p){
|
|
if( p ){
|
|
switch( p->eStage ){
|
|
case RBU_STAGE_OAL: {
|
|
RbuObjIter *pIter = &p->objiter;
|
|
while( p->rc==SQLITE_OK && pIter->zTbl ){
|
|
|
|
if( pIter->bCleanup ){
|
|
/* Clean up the rbu_tmp_xxx table for the previous table. It
|
|
** cannot be dropped as there are currently active SQL statements.
|
|
** But the contents can be deleted. */
|
|
if( pIter->abIndexed ){
|
|
rbuMPrintfExec(p, p->dbRbu,
|
|
"DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
|
|
);
|
|
}
|
|
}else{
|
|
rbuObjIterPrepareAll(p, pIter, 0);
|
|
|
|
/* Advance to the next row to process. */
|
|
if( p->rc==SQLITE_OK ){
|
|
int rc = sqlite3_step(pIter->pSelect);
|
|
if( rc==SQLITE_ROW ){
|
|
p->nProgress++;
|
|
p->nStep++;
|
|
return rbuStep(p);
|
|
}
|
|
p->rc = sqlite3_reset(pIter->pSelect);
|
|
p->nStep = 0;
|
|
}
|
|
}
|
|
|
|
rbuObjIterNext(p, pIter);
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
assert( pIter->zTbl==0 );
|
|
rbuSaveState(p, RBU_STAGE_MOVE);
|
|
rbuIncrSchemaCookie(p);
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
|
|
}
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
|
|
}
|
|
p->eStage = RBU_STAGE_MOVE;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case RBU_STAGE_MOVE: {
|
|
if( p->rc==SQLITE_OK ){
|
|
rbuMoveOalFile(p);
|
|
p->nProgress++;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case RBU_STAGE_CKPT: {
|
|
if( p->rc==SQLITE_OK ){
|
|
if( p->nStep>=p->nFrame ){
|
|
sqlite3_file *pDb = p->pTargetFd->pReal;
|
|
|
|
/* Sync the db file */
|
|
p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
|
|
|
|
/* Update nBackfill */
|
|
if( p->rc==SQLITE_OK ){
|
|
void volatile *ptr;
|
|
p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
|
|
if( p->rc==SQLITE_OK ){
|
|
((u32 volatile*)ptr)[24] = p->iMaxFrame;
|
|
}
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
p->eStage = RBU_STAGE_DONE;
|
|
p->rc = SQLITE_DONE;
|
|
}
|
|
}else{
|
|
RbuFrame *pFrame = &p->aFrame[p->nStep];
|
|
rbuCheckpointFrame(p, pFrame);
|
|
p->nStep++;
|
|
}
|
|
p->nProgress++;
|
|
}
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
return p->rc;
|
|
}else{
|
|
return SQLITE_NOMEM;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Free an RbuState object allocated by rbuLoadState().
|
|
*/
|
|
static void rbuFreeState(RbuState *p){
|
|
if( p ){
|
|
sqlite3_free(p->zTbl);
|
|
sqlite3_free(p->zIdx);
|
|
sqlite3_free(p);
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Allocate an RbuState object and load the contents of the rbu_state
|
|
** table into it. Return a pointer to the new object. It is the
|
|
** responsibility of the caller to eventually free the object using
|
|
** sqlite3_free().
|
|
**
|
|
** If an error occurs, leave an error code and message in the rbu handle
|
|
** and return NULL.
|
|
*/
|
|
static RbuState *rbuLoadState(sqlite3rbu *p){
|
|
RbuState *pRet = 0;
|
|
sqlite3_stmt *pStmt = 0;
|
|
int rc;
|
|
int rc2;
|
|
|
|
pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
|
|
if( pRet==0 ) return 0;
|
|
|
|
rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
|
|
sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
|
|
);
|
|
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
switch( sqlite3_column_int(pStmt, 0) ){
|
|
case RBU_STATE_STAGE:
|
|
pRet->eStage = sqlite3_column_int(pStmt, 1);
|
|
if( pRet->eStage!=RBU_STAGE_OAL
|
|
&& pRet->eStage!=RBU_STAGE_MOVE
|
|
&& pRet->eStage!=RBU_STAGE_CKPT
|
|
){
|
|
p->rc = SQLITE_CORRUPT;
|
|
}
|
|
break;
|
|
|
|
case RBU_STATE_TBL:
|
|
pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
|
|
break;
|
|
|
|
case RBU_STATE_IDX:
|
|
pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
|
|
break;
|
|
|
|
case RBU_STATE_ROW:
|
|
pRet->nRow = sqlite3_column_int(pStmt, 1);
|
|
break;
|
|
|
|
case RBU_STATE_PROGRESS:
|
|
pRet->nProgress = sqlite3_column_int64(pStmt, 1);
|
|
break;
|
|
|
|
case RBU_STATE_CKPT:
|
|
pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
|
|
break;
|
|
|
|
case RBU_STATE_COOKIE:
|
|
pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
|
|
break;
|
|
|
|
case RBU_STATE_OALSZ:
|
|
pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
|
|
break;
|
|
|
|
default:
|
|
rc = SQLITE_CORRUPT;
|
|
break;
|
|
}
|
|
}
|
|
rc2 = sqlite3_finalize(pStmt);
|
|
if( rc==SQLITE_OK ) rc = rc2;
|
|
|
|
p->rc = rc;
|
|
return pRet;
|
|
}
|
|
|
|
/*
|
|
** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
|
|
** otherwise. Either or both argument may be NULL. Two NULL values are
|
|
** considered equal, and NULL is considered distinct from all other values.
|
|
*/
|
|
static int rbuStrCompare(const char *z1, const char *z2){
|
|
if( z1==0 && z2==0 ) return 0;
|
|
if( z1==0 || z2==0 ) return 1;
|
|
return (sqlite3_stricmp(z1, z2)!=0);
|
|
}
|
|
|
|
/*
|
|
** This function is called as part of sqlite3rbu_open() when initializing
|
|
** an rbu handle in OAL stage. If the rbu update has not started (i.e.
|
|
** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
|
|
** things so that the next call to sqlite3rbu_step() continues on from
|
|
** where the previous rbu handle left off.
|
|
**
|
|
** If an error occurs, an error code and error message are left in the
|
|
** rbu handle passed as the first argument.
|
|
*/
|
|
static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
|
|
assert( p->rc==SQLITE_OK );
|
|
if( pState->zTbl ){
|
|
RbuObjIter *pIter = &p->objiter;
|
|
int rc = SQLITE_OK;
|
|
|
|
while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
|
|
|| rbuStrCompare(pIter->zIdx, pState->zIdx)
|
|
|| rbuStrCompare(pIter->zTbl, pState->zTbl)
|
|
)){
|
|
rc = rbuObjIterNext(p, pIter);
|
|
}
|
|
|
|
if( rc==SQLITE_OK && !pIter->zTbl ){
|
|
rc = SQLITE_ERROR;
|
|
p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
p->nStep = pState->nRow;
|
|
rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
|
|
}
|
|
|
|
p->rc = rc;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** If there is a "*-oal" file in the file-system corresponding to the
|
|
** target database in the file-system, delete it. If an error occurs,
|
|
** leave an error code and error message in the rbu handle.
|
|
*/
|
|
static void rbuDeleteOalFile(sqlite3rbu *p){
|
|
char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
|
|
if( zOal ){
|
|
sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
|
|
assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
|
|
pVfs->xDelete(pVfs, zOal, 0);
|
|
sqlite3_free(zOal);
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Allocate a private rbu VFS for the rbu handle passed as the only
|
|
** argument. This VFS will be used unless the call to sqlite3rbu_open()
|
|
** specified a URI with a vfs=? option in place of a target database
|
|
** file name.
|
|
*/
|
|
static void rbuCreateVfs(sqlite3rbu *p){
|
|
int rnd;
|
|
char zRnd[64];
|
|
|
|
assert( p->rc==SQLITE_OK );
|
|
sqlite3_randomness(sizeof(int), (void*)&rnd);
|
|
sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
|
|
p->rc = sqlite3rbu_create_vfs(zRnd, 0);
|
|
if( p->rc==SQLITE_OK ){
|
|
sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
|
|
assert( pVfs );
|
|
p->zVfsName = pVfs->zName;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Destroy the private VFS created for the rbu handle passed as the only
|
|
** argument by an earlier call to rbuCreateVfs().
|
|
*/
|
|
static void rbuDeleteVfs(sqlite3rbu *p){
|
|
if( p->zVfsName ){
|
|
sqlite3rbu_destroy_vfs(p->zVfsName);
|
|
p->zVfsName = 0;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Open and return a new RBU handle.
|
|
*/
|
|
sqlite3rbu *sqlite3rbu_open(
|
|
const char *zTarget,
|
|
const char *zRbu,
|
|
const char *zState
|
|
){
|
|
sqlite3rbu *p;
|
|
size_t nTarget = strlen(zTarget);
|
|
size_t nRbu = strlen(zRbu);
|
|
size_t nState = zState ? strlen(zState) : 0;
|
|
size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1+ nState+1;
|
|
|
|
p = (sqlite3rbu*)sqlite3_malloc64(nByte);
|
|
if( p ){
|
|
RbuState *pState = 0;
|
|
|
|
/* Create the custom VFS. */
|
|
memset(p, 0, sizeof(sqlite3rbu));
|
|
rbuCreateVfs(p);
|
|
|
|
/* Open the target database */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->zTarget = (char*)&p[1];
|
|
memcpy(p->zTarget, zTarget, nTarget+1);
|
|
p->zRbu = &p->zTarget[nTarget+1];
|
|
memcpy(p->zRbu, zRbu, nRbu+1);
|
|
if( zState ){
|
|
p->zState = &p->zRbu[nRbu+1];
|
|
memcpy(p->zState, zState, nState+1);
|
|
}
|
|
rbuOpenDatabase(p);
|
|
}
|
|
|
|
/* If it has not already been created, create the rbu_state table */
|
|
rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
pState = rbuLoadState(p);
|
|
assert( pState || p->rc!=SQLITE_OK );
|
|
if( p->rc==SQLITE_OK ){
|
|
|
|
if( pState->eStage==0 ){
|
|
rbuDeleteOalFile(p);
|
|
p->eStage = RBU_STAGE_OAL;
|
|
}else{
|
|
p->eStage = pState->eStage;
|
|
}
|
|
p->nProgress = pState->nProgress;
|
|
p->iOalSz = pState->iOalSz;
|
|
}
|
|
}
|
|
assert( p->rc!=SQLITE_OK || p->eStage!=0 );
|
|
|
|
if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
|
|
if( p->eStage==RBU_STAGE_OAL ){
|
|
p->rc = SQLITE_ERROR;
|
|
p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
|
|
}else if( p->eStage==RBU_STAGE_MOVE ){
|
|
p->eStage = RBU_STAGE_CKPT;
|
|
p->nStep = 0;
|
|
}
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK
|
|
&& (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
|
|
&& pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
|
|
){
|
|
/* At this point (pTargetFd->iCookie) contains the value of the
|
|
** change-counter cookie (the thing that gets incremented when a
|
|
** transaction is committed in rollback mode) currently stored on
|
|
** page 1 of the database file. */
|
|
p->rc = SQLITE_BUSY;
|
|
p->zErrmsg = sqlite3_mprintf("database modified during rbu update");
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
if( p->eStage==RBU_STAGE_OAL ){
|
|
sqlite3 *db = p->dbMain;
|
|
|
|
/* Open transactions both databases. The *-oal file is opened or
|
|
** created at this point. */
|
|
p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
|
|
}
|
|
|
|
/* Check if the main database is a zipvfs db. If it is, set the upper
|
|
** level pager to use "journal_mode=off". This prevents it from
|
|
** generating a large journal using a temp file. */
|
|
if( p->rc==SQLITE_OK ){
|
|
int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
|
|
if( frc==SQLITE_OK ){
|
|
p->rc = sqlite3_exec(db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
|
|
}
|
|
}
|
|
|
|
/* Point the object iterator at the first object */
|
|
if( p->rc==SQLITE_OK ){
|
|
p->rc = rbuObjIterFirst(p, &p->objiter);
|
|
}
|
|
|
|
/* If the RBU database contains no data_xxx tables, declare the RBU
|
|
** update finished. */
|
|
if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
|
|
p->rc = SQLITE_DONE;
|
|
}
|
|
|
|
if( p->rc==SQLITE_OK ){
|
|
rbuSetupOal(p, pState);
|
|
}
|
|
|
|
}else if( p->eStage==RBU_STAGE_MOVE ){
|
|
/* no-op */
|
|
}else if( p->eStage==RBU_STAGE_CKPT ){
|
|
rbuSetupCheckpoint(p, pState);
|
|
}else if( p->eStage==RBU_STAGE_DONE ){
|
|
p->rc = SQLITE_DONE;
|
|
}else{
|
|
p->rc = SQLITE_CORRUPT;
|
|
}
|
|
}
|
|
|
|
rbuFreeState(pState);
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
|
|
/*
|
|
** Return the database handle used by pRbu.
|
|
*/
|
|
sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
|
|
sqlite3 *db = 0;
|
|
if( pRbu ){
|
|
db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
|
|
}
|
|
return db;
|
|
}
|
|
|
|
|
|
/*
|
|
** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
|
|
** then edit any error message string so as to remove all occurrences of
|
|
** the pattern "rbu_imp_[0-9]*".
|
|
*/
|
|
static void rbuEditErrmsg(sqlite3rbu *p){
|
|
if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
|
|
int i;
|
|
size_t nErrmsg = strlen(p->zErrmsg);
|
|
for(i=0; i<(nErrmsg-8); i++){
|
|
if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
|
|
int nDel = 8;
|
|
while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
|
|
memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
|
|
nErrmsg -= nDel;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Close the RBU handle.
|
|
*/
|
|
int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
|
|
int rc;
|
|
if( p ){
|
|
|
|
/* Commit the transaction to the *-oal file. */
|
|
if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
|
|
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
|
|
}
|
|
|
|
rbuSaveState(p, p->eStage);
|
|
|
|
if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
|
|
p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
|
|
}
|
|
|
|
/* Close any open statement handles. */
|
|
rbuObjIterFinalize(&p->objiter);
|
|
|
|
/* Close the open database handle and VFS object. */
|
|
sqlite3_close(p->dbMain);
|
|
sqlite3_close(p->dbRbu);
|
|
rbuDeleteVfs(p);
|
|
sqlite3_free(p->aBuf);
|
|
sqlite3_free(p->aFrame);
|
|
|
|
rbuEditErrmsg(p);
|
|
rc = p->rc;
|
|
*pzErrmsg = p->zErrmsg;
|
|
sqlite3_free(p);
|
|
}else{
|
|
rc = SQLITE_NOMEM;
|
|
*pzErrmsg = 0;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Return the total number of key-value operations (inserts, deletes or
|
|
** updates) that have been performed on the target database since the
|
|
** current RBU update was started.
|
|
*/
|
|
sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
|
|
return pRbu->nProgress;
|
|
}
|
|
|
|
int sqlite3rbu_savestate(sqlite3rbu *p){
|
|
int rc = p->rc;
|
|
|
|
if( rc==SQLITE_DONE ) return SQLITE_OK;
|
|
|
|
assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
|
|
if( p->eStage==RBU_STAGE_OAL ){
|
|
assert( rc!=SQLITE_DONE );
|
|
if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
|
|
}
|
|
|
|
p->rc = rc;
|
|
rbuSaveState(p, p->eStage);
|
|
rc = p->rc;
|
|
|
|
if( p->eStage==RBU_STAGE_OAL ){
|
|
assert( rc!=SQLITE_DONE );
|
|
if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
|
|
if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, 0);
|
|
if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
|
|
}
|
|
|
|
p->rc = rc;
|
|
return rc;
|
|
}
|
|
|
|
/**************************************************************************
|
|
** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
|
|
** of a standard VFS in the following ways:
|
|
**
|
|
** 1. Whenever the first page of a main database file is read or
|
|
** written, the value of the change-counter cookie is stored in
|
|
** rbu_file.iCookie. Similarly, the value of the "write-version"
|
|
** database header field is stored in rbu_file.iWriteVer. This ensures
|
|
** that the values are always trustworthy within an open transaction.
|
|
**
|
|
** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
|
|
** member variable of the associated database file descriptor is set
|
|
** to point to the new file. A mutex protected linked list of all main
|
|
** db fds opened using a particular RBU VFS is maintained at
|
|
** rbu_vfs.pMain to facilitate this.
|
|
**
|
|
** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
|
|
** object can be marked as the target database of an RBU update. This
|
|
** turns on the following extra special behaviour:
|
|
**
|
|
** 3a. If xAccess() is called to check if there exists a *-wal file
|
|
** associated with an RBU target database currently in RBU_STAGE_OAL
|
|
** stage (preparing the *-oal file), the following special handling
|
|
** applies:
|
|
**
|
|
** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
|
|
** target database may not be in wal mode already.
|
|
**
|
|
** * if the *-wal file does not exist, set the output parameter to
|
|
** non-zero (to tell SQLite that it does exist) anyway.
|
|
**
|
|
** Then, when xOpen() is called to open the *-wal file associated with
|
|
** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
|
|
** file, the rbu vfs opens the corresponding *-oal file instead.
|
|
**
|
|
** 3b. The *-shm pages returned by xShmMap() for a target db file in
|
|
** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
|
|
** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
|
|
** are no-ops on target database files in RBU_STAGE_OAL mode. This is
|
|
** because assert() statements in some VFS implementations fail if
|
|
** xShmLock() is called before xShmMap().
|
|
**
|
|
** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
|
|
** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
|
|
** fails with an SQLITE_BUSY error. This is to stop RBU connections
|
|
** from automatically checkpointing a *-wal (or *-oal) file from within
|
|
** sqlite3_close().
|
|
**
|
|
** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
|
|
** all xWrite() calls on the target database file perform no IO.
|
|
** Instead the frame and page numbers that would be read and written
|
|
** are recorded. Additionally, successful attempts to obtain exclusive
|
|
** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
|
|
** database file are recorded. xShmLock() calls to unlock the same
|
|
** locks are no-ops (so that once obtained, these locks are never
|
|
** relinquished). Finally, calls to xSync() on the target database
|
|
** file fail with SQLITE_INTERNAL errors.
|
|
*/
|
|
|
|
static void rbuUnlockShm(rbu_file *p){
|
|
if( p->pRbu ){
|
|
int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
|
|
int i;
|
|
for(i=0; i<SQLITE_SHM_NLOCK;i++){
|
|
if( (1<<i) & p->pRbu->mLock ){
|
|
xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
|
|
}
|
|
}
|
|
p->pRbu->mLock = 0;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Close an rbu file.
|
|
*/
|
|
static int rbuVfsClose(sqlite3_file *pFile){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
int rc;
|
|
int i;
|
|
|
|
/* Free the contents of the apShm[] array. And the array itself. */
|
|
for(i=0; i<p->nShm; i++){
|
|
sqlite3_free(p->apShm[i]);
|
|
}
|
|
sqlite3_free(p->apShm);
|
|
p->apShm = 0;
|
|
sqlite3_free(p->zDel);
|
|
|
|
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
|
|
rbu_file **pp;
|
|
sqlite3_mutex_enter(p->pRbuVfs->mutex);
|
|
for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
|
|
*pp = p->pMainNext;
|
|
sqlite3_mutex_leave(p->pRbuVfs->mutex);
|
|
rbuUnlockShm(p);
|
|
p->pReal->pMethods->xShmUnmap(p->pReal, 0);
|
|
}
|
|
|
|
/* Close the underlying file handle */
|
|
rc = p->pReal->pMethods->xClose(p->pReal);
|
|
return rc;
|
|
}
|
|
|
|
|
|
/*
|
|
** Read and return an unsigned 32-bit big-endian integer from the buffer
|
|
** passed as the only argument.
|
|
*/
|
|
static u32 rbuGetU32(u8 *aBuf){
|
|
return ((u32)aBuf[0] << 24)
|
|
+ ((u32)aBuf[1] << 16)
|
|
+ ((u32)aBuf[2] << 8)
|
|
+ ((u32)aBuf[3]);
|
|
}
|
|
|
|
/*
|
|
** Read data from an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsRead(
|
|
sqlite3_file *pFile,
|
|
void *zBuf,
|
|
int iAmt,
|
|
sqlite_int64 iOfst
|
|
){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
sqlite3rbu *pRbu = p->pRbu;
|
|
int rc;
|
|
|
|
if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
|
|
assert( p->openFlags & SQLITE_OPEN_WAL );
|
|
rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
|
|
}else{
|
|
if( pRbu && pRbu->eStage==RBU_STAGE_OAL
|
|
&& (p->openFlags & SQLITE_OPEN_WAL)
|
|
&& iOfst>=pRbu->iOalSz
|
|
){
|
|
rc = SQLITE_OK;
|
|
memset(zBuf, 0, iAmt);
|
|
}else{
|
|
rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
|
|
}
|
|
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
|
|
/* These look like magic numbers. But they are stable, as they are part
|
|
** of the definition of the SQLite file format, which may not change. */
|
|
u8 *pBuf = (u8*)zBuf;
|
|
p->iCookie = rbuGetU32(&pBuf[24]);
|
|
p->iWriteVer = pBuf[19];
|
|
}
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Write data to an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsWrite(
|
|
sqlite3_file *pFile,
|
|
const void *zBuf,
|
|
int iAmt,
|
|
sqlite_int64 iOfst
|
|
){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
sqlite3rbu *pRbu = p->pRbu;
|
|
int rc;
|
|
|
|
if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
|
|
assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
|
|
rc = rbuCaptureDbWrite(p->pRbu, iOfst);
|
|
}else{
|
|
if( pRbu && pRbu->eStage==RBU_STAGE_OAL
|
|
&& (p->openFlags & SQLITE_OPEN_WAL)
|
|
&& iOfst>=pRbu->iOalSz
|
|
){
|
|
pRbu->iOalSz = iAmt + iOfst;
|
|
}
|
|
rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
|
|
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
|
|
/* These look like magic numbers. But they are stable, as they are part
|
|
** of the definition of the SQLite file format, which may not change. */
|
|
u8 *pBuf = (u8*)zBuf;
|
|
p->iCookie = rbuGetU32(&pBuf[24]);
|
|
p->iWriteVer = pBuf[19];
|
|
}
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Truncate an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
return p->pReal->pMethods->xTruncate(p->pReal, size);
|
|
}
|
|
|
|
/*
|
|
** Sync an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsSync(sqlite3_file *pFile, int flags){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
|
|
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
|
|
return SQLITE_INTERNAL;
|
|
}
|
|
return SQLITE_OK;
|
|
}
|
|
return p->pReal->pMethods->xSync(p->pReal, flags);
|
|
}
|
|
|
|
/*
|
|
** Return the current file-size of an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
return p->pReal->pMethods->xFileSize(p->pReal, pSize);
|
|
}
|
|
|
|
/*
|
|
** Lock an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsLock(sqlite3_file *pFile, int eLock){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
sqlite3rbu *pRbu = p->pRbu;
|
|
int rc = SQLITE_OK;
|
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
|
if( pRbu && eLock==SQLITE_LOCK_EXCLUSIVE && pRbu->eStage!=RBU_STAGE_DONE ){
|
|
/* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
|
|
** prevents it from checkpointing the database from sqlite3_close(). */
|
|
rc = SQLITE_BUSY;
|
|
}else{
|
|
rc = p->pReal->pMethods->xLock(p->pReal, eLock);
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Unlock an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
return p->pReal->pMethods->xUnlock(p->pReal, eLock);
|
|
}
|
|
|
|
/*
|
|
** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
|
|
}
|
|
|
|
/*
|
|
** File control method. For custom operations on an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
|
|
int rc;
|
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
|
|
|| p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
|
|
);
|
|
if( op==SQLITE_FCNTL_RBU ){
|
|
sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
|
|
|
|
/* First try to find another RBU vfs lower down in the vfs stack. If
|
|
** one is found, this vfs will operate in pass-through mode. The lower
|
|
** level vfs will do the special RBU handling. */
|
|
rc = xControl(p->pReal, op, pArg);
|
|
|
|
if( rc==SQLITE_NOTFOUND ){
|
|
/* Now search for a zipvfs instance lower down in the VFS stack. If
|
|
** one is found, this is an error. */
|
|
void *dummy = 0;
|
|
rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
|
|
if( rc==SQLITE_OK ){
|
|
rc = SQLITE_ERROR;
|
|
pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
|
|
}else if( rc==SQLITE_NOTFOUND ){
|
|
pRbu->pTargetFd = p;
|
|
p->pRbu = pRbu;
|
|
if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
|
|
rc = SQLITE_OK;
|
|
}
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
rc = xControl(p->pReal, op, pArg);
|
|
if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
|
|
rbu_vfs *pRbuVfs = p->pRbuVfs;
|
|
char *zIn = *(char**)pArg;
|
|
char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
|
|
*(char**)pArg = zOut;
|
|
if( zOut==0 ) rc = SQLITE_NOMEM;
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Return the sector-size in bytes for an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsSectorSize(sqlite3_file *pFile){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
return p->pReal->pMethods->xSectorSize(p->pReal);
|
|
}
|
|
|
|
/*
|
|
** Return the device characteristic flags supported by an rbuVfs-file.
|
|
*/
|
|
static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
|
|
}
|
|
|
|
/*
|
|
** Take or release a shared-memory lock.
|
|
*/
|
|
static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
sqlite3rbu *pRbu = p->pRbu;
|
|
int rc = SQLITE_OK;
|
|
|
|
#ifdef SQLITE_AMALGAMATION
|
|
assert( WAL_CKPT_LOCK==1 );
|
|
#endif
|
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
|
if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
|
|
/* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
|
|
** taking this lock also prevents any checkpoints from occurring.
|
|
** todo: really, it's not clear why this might occur, as
|
|
** wal_autocheckpoint ought to be turned off. */
|
|
if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
|
|
}else{
|
|
int bCapture = 0;
|
|
if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
|
|
&& pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
|
|
&& (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
|
|
){
|
|
bCapture = 1;
|
|
}
|
|
|
|
if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
|
|
rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
|
|
if( bCapture && rc==SQLITE_OK ){
|
|
pRbu->mLock |= (1 << ofst);
|
|
}
|
|
}
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
|
|
*/
|
|
static int rbuVfsShmMap(
|
|
sqlite3_file *pFile,
|
|
int iRegion,
|
|
int szRegion,
|
|
int isWrite,
|
|
void volatile **pp
|
|
){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
int rc = SQLITE_OK;
|
|
int eStage = (p->pRbu ? p->pRbu->eStage : 0);
|
|
|
|
/* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
|
|
** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
|
|
** instead of a file on disk. */
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
|
if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
|
|
if( iRegion<=p->nShm ){
|
|
int nByte = (iRegion+1) * sizeof(char*);
|
|
char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
|
|
if( apNew==0 ){
|
|
rc = SQLITE_NOMEM;
|
|
}else{
|
|
memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
|
|
p->apShm = apNew;
|
|
p->nShm = iRegion+1;
|
|
}
|
|
}
|
|
|
|
if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
|
|
char *pNew = (char*)sqlite3_malloc64(szRegion);
|
|
if( pNew==0 ){
|
|
rc = SQLITE_NOMEM;
|
|
}else{
|
|
memset(pNew, 0, szRegion);
|
|
p->apShm[iRegion] = pNew;
|
|
}
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
*pp = p->apShm[iRegion];
|
|
}else{
|
|
*pp = 0;
|
|
}
|
|
}else{
|
|
assert( p->apShm==0 );
|
|
rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Memory barrier.
|
|
*/
|
|
static void rbuVfsShmBarrier(sqlite3_file *pFile){
|
|
rbu_file *p = (rbu_file *)pFile;
|
|
p->pReal->pMethods->xShmBarrier(p->pReal);
|
|
}
|
|
|
|
/*
|
|
** The xShmUnmap method.
|
|
*/
|
|
static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
|
|
rbu_file *p = (rbu_file*)pFile;
|
|
int rc = SQLITE_OK;
|
|
int eStage = (p->pRbu ? p->pRbu->eStage : 0);
|
|
|
|
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
|
|
if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
|
|
/* no-op */
|
|
}else{
|
|
/* Release the checkpointer and writer locks */
|
|
rbuUnlockShm(p);
|
|
rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Given that zWal points to a buffer containing a wal file name passed to
|
|
** either the xOpen() or xAccess() VFS method, return a pointer to the
|
|
** file-handle opened by the same database connection on the corresponding
|
|
** database file.
|
|
*/
|
|
static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
|
|
rbu_file *pDb;
|
|
sqlite3_mutex_enter(pRbuVfs->mutex);
|
|
for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
|
|
sqlite3_mutex_leave(pRbuVfs->mutex);
|
|
return pDb;
|
|
}
|
|
|
|
/*
|
|
** Open an rbu file handle.
|
|
*/
|
|
static int rbuVfsOpen(
|
|
sqlite3_vfs *pVfs,
|
|
const char *zName,
|
|
sqlite3_file *pFile,
|
|
int flags,
|
|
int *pOutFlags
|
|
){
|
|
static sqlite3_io_methods rbuvfs_io_methods = {
|
|
2, /* iVersion */
|
|
rbuVfsClose, /* xClose */
|
|
rbuVfsRead, /* xRead */
|
|
rbuVfsWrite, /* xWrite */
|
|
rbuVfsTruncate, /* xTruncate */
|
|
rbuVfsSync, /* xSync */
|
|
rbuVfsFileSize, /* xFileSize */
|
|
rbuVfsLock, /* xLock */
|
|
rbuVfsUnlock, /* xUnlock */
|
|
rbuVfsCheckReservedLock, /* xCheckReservedLock */
|
|
rbuVfsFileControl, /* xFileControl */
|
|
rbuVfsSectorSize, /* xSectorSize */
|
|
rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
|
|
rbuVfsShmMap, /* xShmMap */
|
|
rbuVfsShmLock, /* xShmLock */
|
|
rbuVfsShmBarrier, /* xShmBarrier */
|
|
rbuVfsShmUnmap, /* xShmUnmap */
|
|
0, 0 /* xFetch, xUnfetch */
|
|
};
|
|
rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
|
|
sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
|
|
rbu_file *pFd = (rbu_file *)pFile;
|
|
int rc = SQLITE_OK;
|
|
const char *zOpen = zName;
|
|
|
|
memset(pFd, 0, sizeof(rbu_file));
|
|
pFd->pReal = (sqlite3_file*)&pFd[1];
|
|
pFd->pRbuVfs = pRbuVfs;
|
|
pFd->openFlags = flags;
|
|
if( zName ){
|
|
if( flags & SQLITE_OPEN_MAIN_DB ){
|
|
/* A main database has just been opened. The following block sets
|
|
** (pFd->zWal) to point to a buffer owned by SQLite that contains
|
|
** the name of the *-wal file this db connection will use. SQLite
|
|
** happens to pass a pointer to this buffer when using xAccess()
|
|
** or xOpen() to operate on the *-wal file. */
|
|
int n = (int)strlen(zName);
|
|
const char *z = &zName[n];
|
|
if( flags & SQLITE_OPEN_URI ){
|
|
int odd = 0;
|
|
while( 1 ){
|
|
if( z[0]==0 ){
|
|
odd = 1 - odd;
|
|
if( odd && z[1]==0 ) break;
|
|
}
|
|
z++;
|
|
}
|
|
z += 2;
|
|
}else{
|
|
while( *z==0 ) z++;
|
|
}
|
|
z += (n + 8 + 1);
|
|
pFd->zWal = z;
|
|
}
|
|
else if( flags & SQLITE_OPEN_WAL ){
|
|
rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
|
|
if( pDb ){
|
|
if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
|
|
/* This call is to open a *-wal file. Intead, open the *-oal. This
|
|
** code ensures that the string passed to xOpen() is terminated by a
|
|
** pair of '\0' bytes in case the VFS attempts to extract a URI
|
|
** parameter from it. */
|
|
size_t nCopy = strlen(zName);
|
|
char *zCopy = sqlite3_malloc64(nCopy+2);
|
|
if( zCopy ){
|
|
memcpy(zCopy, zName, nCopy);
|
|
zCopy[nCopy-3] = 'o';
|
|
zCopy[nCopy] = '\0';
|
|
zCopy[nCopy+1] = '\0';
|
|
zOpen = (const char*)(pFd->zDel = zCopy);
|
|
}else{
|
|
rc = SQLITE_NOMEM;
|
|
}
|
|
pFd->pRbu = pDb->pRbu;
|
|
}
|
|
pDb->pWalFd = pFd;
|
|
}
|
|
}
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, flags, pOutFlags);
|
|
}
|
|
if( pFd->pReal->pMethods ){
|
|
/* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
|
|
** pointer and, if the file is a main database file, link it into the
|
|
** mutex protected linked list of all such files. */
|
|
pFile->pMethods = &rbuvfs_io_methods;
|
|
if( flags & SQLITE_OPEN_MAIN_DB ){
|
|
sqlite3_mutex_enter(pRbuVfs->mutex);
|
|
pFd->pMainNext = pRbuVfs->pMain;
|
|
pRbuVfs->pMain = pFd;
|
|
sqlite3_mutex_leave(pRbuVfs->mutex);
|
|
}
|
|
}else{
|
|
sqlite3_free(pFd->zDel);
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Delete the file located at zPath.
|
|
*/
|
|
static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
|
|
}
|
|
|
|
/*
|
|
** Test for access permissions. Return true if the requested permission
|
|
** is available, or false otherwise.
|
|
*/
|
|
static int rbuVfsAccess(
|
|
sqlite3_vfs *pVfs,
|
|
const char *zPath,
|
|
int flags,
|
|
int *pResOut
|
|
){
|
|
rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
|
|
sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
|
|
int rc;
|
|
|
|
rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
|
|
|
|
/* If this call is to check if a *-wal file associated with an RBU target
|
|
** database connection exists, and the RBU update is in RBU_STAGE_OAL,
|
|
** the following special handling is activated:
|
|
**
|
|
** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
|
|
** ensures that the RBU extension never tries to update a database
|
|
** in wal mode, even if the first page of the database file has
|
|
** been damaged.
|
|
**
|
|
** b) if the *-wal file does not exist, claim that it does anyway,
|
|
** causing SQLite to call xOpen() to open it. This call will also
|
|
** be intercepted (see the rbuVfsOpen() function) and the *-oal
|
|
** file opened instead.
|
|
*/
|
|
if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
|
|
rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
|
|
if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
|
|
if( *pResOut ){
|
|
rc = SQLITE_CANTOPEN;
|
|
}else{
|
|
*pResOut = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Populate buffer zOut with the full canonical pathname corresponding
|
|
** to the pathname in zPath. zOut is guaranteed to point to a buffer
|
|
** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
|
|
*/
|
|
static int rbuVfsFullPathname(
|
|
sqlite3_vfs *pVfs,
|
|
const char *zPath,
|
|
int nOut,
|
|
char *zOut
|
|
){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
|
|
}
|
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
|
/*
|
|
** Open the dynamic library located at zPath and return a handle.
|
|
*/
|
|
static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
return pRealVfs->xDlOpen(pRealVfs, zPath);
|
|
}
|
|
|
|
/*
|
|
** Populate the buffer zErrMsg (size nByte bytes) with a human readable
|
|
** utf-8 string describing the most recent error encountered associated
|
|
** with dynamic libraries.
|
|
*/
|
|
static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
|
|
}
|
|
|
|
/*
|
|
** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
|
|
*/
|
|
static void (*rbuVfsDlSym(
|
|
sqlite3_vfs *pVfs,
|
|
void *pArg,
|
|
const char *zSym
|
|
))(void){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
|
|
}
|
|
|
|
/*
|
|
** Close the dynamic library handle pHandle.
|
|
*/
|
|
static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
pRealVfs->xDlClose(pRealVfs, pHandle);
|
|
}
|
|
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
|
|
|
|
/*
|
|
** Populate the buffer pointed to by zBufOut with nByte bytes of
|
|
** random data.
|
|
*/
|
|
static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
|
|
}
|
|
|
|
/*
|
|
** Sleep for nMicro microseconds. Return the number of microseconds
|
|
** actually slept.
|
|
*/
|
|
static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
return pRealVfs->xSleep(pRealVfs, nMicro);
|
|
}
|
|
|
|
/*
|
|
** Return the current time as a Julian Day number in *pTimeOut.
|
|
*/
|
|
static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
|
|
sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
|
|
return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
|
|
}
|
|
|
|
/*
|
|
** No-op.
|
|
*/
|
|
static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
** Deregister and destroy an RBU vfs created by an earlier call to
|
|
** sqlite3rbu_create_vfs().
|
|
*/
|
|
void sqlite3rbu_destroy_vfs(const char *zName){
|
|
sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
|
|
if( pVfs && pVfs->xOpen==rbuVfsOpen ){
|
|
sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
|
|
sqlite3_vfs_unregister(pVfs);
|
|
sqlite3_free(pVfs);
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Create an RBU VFS named zName that accesses the underlying file-system
|
|
** via existing VFS zParent. The new object is registered as a non-default
|
|
** VFS with SQLite before returning.
|
|
*/
|
|
int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
|
|
|
|
/* Template for VFS */
|
|
static sqlite3_vfs vfs_template = {
|
|
1, /* iVersion */
|
|
0, /* szOsFile */
|
|
0, /* mxPathname */
|
|
0, /* pNext */
|
|
0, /* zName */
|
|
0, /* pAppData */
|
|
rbuVfsOpen, /* xOpen */
|
|
rbuVfsDelete, /* xDelete */
|
|
rbuVfsAccess, /* xAccess */
|
|
rbuVfsFullPathname, /* xFullPathname */
|
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
|
rbuVfsDlOpen, /* xDlOpen */
|
|
rbuVfsDlError, /* xDlError */
|
|
rbuVfsDlSym, /* xDlSym */
|
|
rbuVfsDlClose, /* xDlClose */
|
|
#else
|
|
0, 0, 0, 0,
|
|
#endif
|
|
|
|
rbuVfsRandomness, /* xRandomness */
|
|
rbuVfsSleep, /* xSleep */
|
|
rbuVfsCurrentTime, /* xCurrentTime */
|
|
rbuVfsGetLastError, /* xGetLastError */
|
|
0, /* xCurrentTimeInt64 (version 2) */
|
|
0, 0, 0 /* Unimplemented version 3 methods */
|
|
};
|
|
|
|
rbu_vfs *pNew = 0; /* Newly allocated VFS */
|
|
int rc = SQLITE_OK;
|
|
size_t nName;
|
|
size_t nByte;
|
|
|
|
nName = strlen(zName);
|
|
nByte = sizeof(rbu_vfs) + nName + 1;
|
|
pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
|
|
if( pNew==0 ){
|
|
rc = SQLITE_NOMEM;
|
|
}else{
|
|
sqlite3_vfs *pParent; /* Parent VFS */
|
|
memset(pNew, 0, nByte);
|
|
pParent = sqlite3_vfs_find(zParent);
|
|
if( pParent==0 ){
|
|
rc = SQLITE_NOTFOUND;
|
|
}else{
|
|
char *zSpace;
|
|
memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
|
|
pNew->base.mxPathname = pParent->mxPathname;
|
|
pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
|
|
pNew->pRealVfs = pParent;
|
|
pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
|
|
memcpy(zSpace, zName, nName);
|
|
|
|
/* Allocate the mutex and register the new VFS (not as the default) */
|
|
pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
|
|
if( pNew->mutex==0 ){
|
|
rc = SQLITE_NOMEM;
|
|
}else{
|
|
rc = sqlite3_vfs_register(&pNew->base, 0);
|
|
}
|
|
}
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
sqlite3_mutex_free(pNew->mutex);
|
|
sqlite3_free(pNew);
|
|
}
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
|