Skip to content

Commit

Permalink
Fix nested transaction count issue (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Dec 13, 2023
1 parent b7d61b0 commit 8cbfd8c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 57 deletions.
25 changes: 12 additions & 13 deletions src/Adapter/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ public function beginTransaction(?int $flags = null, ?string $name = null): Mysq
}

$this->isTransaction = true;
$this->transactionDepth++;
}

$this->transactionDepth++;

return $this;
}

Expand Down Expand Up @@ -172,20 +173,18 @@ public function commit(?int $flags = null, ?string $name = null): Mysql
*/
public function rollback(?int $flags = null, ?string $name = null): Mysql
{
$this->transactionDepth--;
$this->transactionDepth = 0;

if ($this->transactionDepth == 0) {
if (($flags !== null) && ($name !== null)) {
$this->connection->rollback($flags, $name);
} else if ($flags !== null) {
$this->connection->rollback($flags);
} else {
$this->connection->rollback();
}

$this->isTransaction = false;
if (($flags !== null) && ($name !== null)) {
$this->connection->rollback($flags, $name);
} else if ($flags !== null) {
$this->connection->rollback($flags);
} else {
$this->connection->rollback();
}

$this->isTransaction = false;

return $this;
}

Expand Down Expand Up @@ -493,4 +492,4 @@ public function getTables(): array
return $tables;
}

}
}
15 changes: 7 additions & 8 deletions src/Adapter/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ public function beginTransaction(): Pdo
if ($this->transactionDepth == 0) {
$this->connection->beginTransaction();
$this->isTransaction = true;
$this->transactionDepth++;
}

$this->transactionDepth++;

return $this;
}

Expand Down Expand Up @@ -214,12 +216,9 @@ public function commit(): Pdo
*/
public function rollback(): Pdo
{
$this->transactionDepth--;

if ($this->transactionDepth == 0) {
$this->connection->rollBack();
$this->isTransaction = false;
}
$this->transactionDepth = 0;
$this->connection->rollBack();
$this->isTransaction = false;

return $this;
}
Expand Down Expand Up @@ -742,4 +741,4 @@ public function exec(mixed $sql): Pdo
return $this;
}

}
}
22 changes: 8 additions & 14 deletions src/Adapter/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public function beginTransaction(): Pgsql
if ($this->transactionDepth == 0) {
$this->query('BEGIN TRANSACTION');
$this->isTransaction = true;
$this->transactionDepth++;
}
$this->transactionDepth++;

return $this;
}
Expand All @@ -169,12 +169,9 @@ public function beginTransaction(): Pgsql
*/
public function commit(): Pgsql
{
$this->transactionDepth--;

if ($this->transactionDepth == 0) {
$this->query('COMMIT');
$this->isTransaction = false;
}
$this->transactionDepth = 0;
$this->query('COMMIT');
$this->isTransaction = false;

return $this;
}
Expand All @@ -186,12 +183,9 @@ public function commit(): Pgsql
*/
public function rollback(): Pgsql
{
$this->transactionDepth--;

if ($this->transactionDepth == 0) {
$this->query('ROLLBACK');
$this->isTransaction = false;
}
$this->transactionDepth = 0;
$this->query('ROLLBACK');
$this->isTransaction = false;

return $this;
}
Expand Down Expand Up @@ -433,4 +427,4 @@ public function getTables(): array
return $tables;
}

}
}
14 changes: 6 additions & 8 deletions src/Adapter/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ public function beginTransaction(): Sqlite
if ($this->transactionDepth == 0) {
$this->query('BEGIN TRANSACTION');
$this->isTransaction = true;
$this->transactionDepth++;
}

$this->transactionDepth++;

return $this;
}

Expand Down Expand Up @@ -167,12 +168,9 @@ public function commit(): Sqlite
*/
public function rollback(): Sqlite
{
$this->transactionDepth--;

if ($this->transactionDepth == 0) {
$this->query('ROLLBACK');
$this->isTransaction = false;
}
$this->transactionDepth = 0;
$this->query('ROLLBACK');
$this->isTransaction = false;

return $this;
}
Expand Down Expand Up @@ -476,4 +474,4 @@ public function getTables(): array
return $tables;
}

}
}
14 changes: 6 additions & 8 deletions src/Adapter/Sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ public function beginTransaction(): Sqlsrv
if ($this->transactionDepth == 0) {
sqlsrv_begin_transaction($this->connection);
$this->isTransaction = true;
$this->transactionDepth++;
}

$this->transactionDepth++;

return $this;
}

Expand Down Expand Up @@ -170,12 +171,9 @@ public function commit(): Sqlsrv
*/
public function rollback(): Sqlsrv
{
$this->transactionDepth--;

if ($this->transactionDepth == 0) {
sqlsrv_rollback($this->connection);
$this->isTransaction = false;
}
$this->transactionDepth = 0;
sqlsrv_rollback($this->connection);
$this->isTransaction = false;

return $this;
}
Expand Down Expand Up @@ -481,4 +479,4 @@ public function getTables(): array
return $tables;
}

}
}
6 changes: 2 additions & 4 deletions src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ public static function start(): static|null
$args = func_get_args();
$class = get_called_class();
if (Db::hasDb($class)) {
if (Db::db($class)->getTransactionDepth() == 0) {
Db::db($class)->beginTransaction();
}
Db::db($class)->beginTransaction();
}

if ($class !== 'Pop\Db\Record') {
Expand Down Expand Up @@ -1064,4 +1062,4 @@ public static function __callStatic(string $name, array $arguments): Collection|
return ($columns !== null) ? static::findBy($columns, $options, $asArray) : null;
}

}
}
4 changes: 2 additions & 2 deletions src/Record/AbstractRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function setPrimaryKeys(array $keys): AbstractRecord
public function startTransaction(): AbstractRecord
{
$class = get_called_class();
if ((Db::hasDb($class)) && (!Db::db($class)->isTransaction())) {
if (Db::hasDb($class)) {
Db::db($class)->beginTransaction();
}
$this->isTransaction = true;
Expand Down Expand Up @@ -727,4 +727,4 @@ public function offsetUnset(mixed $offset): void
$this->__unset($offset);
}

}
}

0 comments on commit 8cbfd8c

Please sign in to comment.