From 2d3e7ce4cc17ccd383bc1781a129e779ce5e8056 Mon Sep 17 00:00:00 2001 From: Tyler Karaszewski Date: Thu, 31 Aug 2023 16:04:01 -0700 Subject: [PATCH] Add flag to allow running 'prepare' with no writes --- BedrockCommand.cpp | 5 +++++ BedrockCommand.h | 7 +++++++ BedrockCore.cpp | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/BedrockCommand.cpp b/BedrockCommand.cpp index 6bcde4a69..931874684 100644 --- a/BedrockCommand.cpp +++ b/BedrockCommand.cpp @@ -20,6 +20,7 @@ BedrockCommand::BedrockCommand(SQLiteCommand&& baseCommand, BedrockPlugin* plugi socket(nullptr), scheduledTime(request.isSet("commandExecuteTime") ? request.calc64("commandExecuteTime") : STimeNow()), _plugin(plugin), + _commitEmptyTransactions(false), _inProgressTiming(INVALID, 0, 0), _timeout(_getTimeout(request, scheduledTime)) { @@ -298,3 +299,7 @@ void BedrockCommand::setTimeout(uint64_t timeoutDurationMS) { timeoutDurationMS += STimeNow(); _timeout = timeoutDurationMS; } + +bool BedrockCommand::shouldCommitEmptyTransactions() const { + return _commitEmptyTransactions; +} diff --git a/BedrockCommand.h b/BedrockCommand.h index 602191d4d..d385de33d 100644 --- a/BedrockCommand.h +++ b/BedrockCommand.h @@ -207,10 +207,17 @@ class BedrockCommand : public SQLiteCommand { // Time at which this command was initially scheduled (typically the time of creation). const uint64_t scheduledTime; + // Returns _commitEmptyTransactions. + bool shouldCommitEmptyTransactions() const; + protected: // The plugin that owns this command. BedrockPlugin* _plugin; + // Commands can set this flag to indicate they want the commit process to be run even though it doesn't appear that any writes have occurred. + // The main use of this is to cause commands that use SQLite::onPrepareHandler to do additional writing to run these final writes. + bool _commitEmptyTransactions; + private: // Set certain initial state on construction. Common functionality to several constructors. void _init(); diff --git a/BedrockCore.cpp b/BedrockCore.cpp index 9cfec2805..c7c1015f6 100644 --- a/BedrockCore.cpp +++ b/BedrockCore.cpp @@ -278,7 +278,7 @@ BedrockCore::RESULT BedrockCore::processCommand(unique_ptr& comm } // If we have no uncommitted query, just rollback the empty transaction. Otherwise, we need to commit. - if (_db.getUncommittedQuery().empty()) { + if (_db.getUncommittedQuery().empty() && !command->shouldCommitEmptyTransactions()) { _db.rollback(); } else { needsCommit = true;