diff --git a/src/cooldogedev/BedrockEconomy/database/mysql/InsertionQuery.php b/src/cooldogedev/BedrockEconomy/database/mysql/InsertionQuery.php index 7b16a81..efdb6a9 100644 --- a/src/cooldogedev/BedrockEconomy/database/mysql/InsertionQuery.php +++ b/src/cooldogedev/BedrockEconomy/database/mysql/InsertionQuery.php @@ -60,6 +60,7 @@ public function onRun(mysqli $connection): void $checkQuery->close(); if ($checkResult->num_rows > 0) { + $connection->rollback(); throw new RecordAlreadyExistsException( _message: "Account already exists for xuid " . $this->xuid . " or username " . $this->username ); @@ -85,6 +86,6 @@ public function onRun(mysqli $connection): void $checkResult->free(); $insertionResult->free(); - $this->setResult(true); + $this->setResult($connection->affected_rows > 0); } } diff --git a/src/cooldogedev/BedrockEconomy/database/mysql/TransferQuery.php b/src/cooldogedev/BedrockEconomy/database/mysql/TransferQuery.php index 886d90f..98b62eb 100644 --- a/src/cooldogedev/BedrockEconomy/database/mysql/TransferQuery.php +++ b/src/cooldogedev/BedrockEconomy/database/mysql/TransferQuery.php @@ -119,6 +119,6 @@ public function onRun(mysqli $connection): void $targetUpdateResult->free(); $sourceUpdateResult->free(); - $this->setResult(true); + $this->setResult($connection->affected_rows > 0 ); } } diff --git a/src/cooldogedev/BedrockEconomy/database/mysql/UpdateQuery.php b/src/cooldogedev/BedrockEconomy/database/mysql/UpdateQuery.php index 99d30ac..d42590c 100644 --- a/src/cooldogedev/BedrockEconomy/database/mysql/UpdateQuery.php +++ b/src/cooldogedev/BedrockEconomy/database/mysql/UpdateQuery.php @@ -63,6 +63,7 @@ public function onRun(mysqli $connection): void $checkQuery->close(); if ($checkResult->num_rows === 0) { + $connection->rollback(); throw new RecordNotFoundException( _message: "Account not found for xuid " . $this->xuid . " or username " . $this->username ); @@ -93,22 +94,24 @@ public function onRun(mysqli $connection): void $updateQuery->close(); if ($updateResult->num_rows === 0 && $this->mode === UpdateMode::SUBTRACT) { + $connection->rollback(); throw new InsufficientFundsException( _message: "Insufficient funds for xuid " . $this->xuid . " or username " . $this->username ); } if ($updateResult->num_rows === 0) { + $connection->rollback(); throw new RecordNotFoundException( _message: "Account not found for xuid " . $this->xuid . " or username " . $this->username ); } - $this->setResult($connection->affected_rows > 0); + $connection->commit(); $checkResult->free(); $updateResult->free(); - $connection->commit(); + $this->setResult($connection->affected_rows > 0); } } diff --git a/src/cooldogedev/BedrockEconomy/database/sqlite/InsertionQuery.php b/src/cooldogedev/BedrockEconomy/database/sqlite/InsertionQuery.php index 134adeb..5630aa2 100644 --- a/src/cooldogedev/BedrockEconomy/database/sqlite/InsertionQuery.php +++ b/src/cooldogedev/BedrockEconomy/database/sqlite/InsertionQuery.php @@ -57,6 +57,7 @@ public function onRun(SQLite3 $connection): void $checkResult = $checkQuery->execute(); if ($checkResult->fetchArray(SQLITE3_ASSOC) !== false) { + $connection->exec("ROLLBACK"); throw new RecordAlreadyExistsException( _message: "Account already exists for xuid " . $this->xuid . " or username " . $this->username ); @@ -70,6 +71,7 @@ public function onRun(SQLite3 $connection): void $insertionQuery->execute(); if ($connection->changes() === 0) { + $connection->exec("ROLLBACK"); throw new RecordAlreadyExistsException( _message: "Account already exists for xuid " . $this->xuid . " or username " . $this->username ); @@ -82,6 +84,6 @@ public function onRun(SQLite3 $connection): void $checkQuery->close(); $insertionQuery->close(); - $this->setResult(true); + $this->setResult($connection->changes() > 0); } } diff --git a/src/cooldogedev/BedrockEconomy/database/sqlite/TransferQuery.php b/src/cooldogedev/BedrockEconomy/database/sqlite/TransferQuery.php index 6a36a22..6310950 100644 --- a/src/cooldogedev/BedrockEconomy/database/sqlite/TransferQuery.php +++ b/src/cooldogedev/BedrockEconomy/database/sqlite/TransferQuery.php @@ -116,8 +116,6 @@ public function onRun(SQLite3 $connection): void $connection->exec("COMMIT"); - $this->setResult($connection->changes() > 0); - $targetResult->finalize(); $sourceResult->finalize(); $targetUpdateResult->finalize(); @@ -127,5 +125,7 @@ public function onRun(SQLite3 $connection): void $targetQuery->close(); $sourceUpdateQuery->close(); $targetUpdateQuery->close(); + + $this->setResult($connection->changes() > 0); } } diff --git a/src/cooldogedev/BedrockEconomy/database/sqlite/UpdateQuery.php b/src/cooldogedev/BedrockEconomy/database/sqlite/UpdateQuery.php index 30faad2..f219485 100644 --- a/src/cooldogedev/BedrockEconomy/database/sqlite/UpdateQuery.php +++ b/src/cooldogedev/BedrockEconomy/database/sqlite/UpdateQuery.php @@ -52,6 +52,9 @@ public function __construct(protected int $mode, protected int $amount, protecte */ public function onRun(SQLite3 $connection): void { + $connection->exec("BEGIN TRANSACTION"); + + // check if account exists $checkQuery = $connection->prepare("SELECT * FROM " . $this->table . " WHERE xuid = ? OR username = ?"); $checkQuery->bindValue(1, $this->xuid); $checkQuery->bindValue(2, $this->username); @@ -59,6 +62,7 @@ public function onRun(SQLite3 $connection): void $checkResult = $checkQuery->execute(); if ($checkResult->fetchArray(SQLITE3_ASSOC) === false) { + $connection->exec("ROLLBACK"); throw new RecordNotFoundException( _message: "Account not found for xuid " . $this->xuid . " or username " . $this->username ); @@ -72,6 +76,7 @@ public function onRun(SQLite3 $connection): void default => throw new InvalidArgumentException("Invalid mode " . $this->mode) }; + // update account $updateQuery = $connection->prepare($updateQuery); $updateQuery->bindValue(1, $this->amount, SQLITE3_INTEGER); $updateQuery->bindValue(2, $this->decimals, SQLITE3_INTEGER); @@ -85,24 +90,28 @@ public function onRun(SQLite3 $connection): void $updateResult = $updateQuery->execute(); - if ($connection->changes() === 0) { - if ($this->mode === UpdateMode::SUBTRACT) { - throw new InsufficientFundsException( - _message: "Insufficient funds for xuid " . $this->xuid . " or username " . $this->username - ); - } + if ($connection->changes() === 0 && $this->mode === UpdateMode::SUBTRACT) { + $connection->exec("ROLLBACK"); + throw new InsufficientFundsException( + _message: "Insufficient funds for xuid " . $this->xuid . " or username " . $this->username + ); + } + if ($connection->changes() === 0) { + $connection->exec("ROLLBACK"); throw new RecordNotFoundException( _message: "Account not found for xuid " . $this->xuid . " or username " . $this->username ); } - $this->setResult($connection->changes() > 0); + $connection->exec("COMMIT"); $checkResult->finalize(); $updateResult->finalize(); $checkQuery->close(); $updateQuery->close(); + + $this->setResult($connection->changes() > 0); } }