Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit 58ac942

Browse files
authored
Merge pull request #8 from freezemage0/feature/support_transaction_api
Implemented support for transaction API.
2 parents d77cd96 + ad7441d commit 58ac942

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

src/Exceptions/DriverException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
namespace Tnapf\Driver\Exceptions;
44

55
use Exception;
6+
use PDOException;
67

78
class DriverException extends Exception
89
{
10+
public static function createFromPdo(PDOException $e): DriverException
11+
{
12+
return new DriverException($e->getMessage(), $e->getCode());
13+
}
914
}

src/Interfaces/DriverInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tnapf\Driver\Interfaces;
44

5+
use Tnapf\Driver\Exceptions\DriverException;
6+
57
interface DriverInterface
68
{
79
/**
@@ -34,4 +36,32 @@ public function connect(): void;
3436
* Disconnect the driver from the database.
3537
*/
3638
public function disconnect(): void;
39+
40+
/**
41+
* Begins a transactions.
42+
*
43+
* Some DBMS may create implicit COMMIT on DML queries.
44+
*
45+
* @throws DriverException on failure.
46+
*/
47+
public function begin(): void;
48+
49+
/**
50+
* Rolls back a transaction.
51+
*
52+
* @throws DriverException on failure.
53+
*/
54+
public function rollback(): void;
55+
56+
/**
57+
* Commits a transaction.
58+
*
59+
* @throws DriverException on failure.
60+
*/
61+
public function commit(): void;
62+
63+
/**
64+
* Returns whether a transaction is active.
65+
*/
66+
public function inTransaction(): bool;
3767
}

src/Interfaces/PreparedQueryInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
namespace Tnapf\Driver\Interfaces;
44

5+
use Tnapf\Driver\Exceptions\QueryException;
6+
57
interface PreparedQueryInterface extends QueryInterface
68
{
79
/**
810
* Bind a value to a named parameter in the prepared statement.
911
*
1012
* @param string $name The name of the parameter.
1113
* @param mixed $value The value to bind to the parameter.
14+
*
15+
* @throws QueryException on failure.
1216
*/
1317
public function bindValue(string $name, mixed $value): void;
1418

1519
/**
1620
* Bind an array of values to their corresponding named parameters in the prepared statement.
1721
*
1822
* @param array $values An associative array of parameter names and their corresponding values.
23+
*
24+
* @throws QueryException on failure.
1925
*/
2026
public function bindValues(array $values): void;
2127
}

src/Interfaces/QueryInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Tnapf\Driver\Interfaces;
44

5+
use Tnapf\Driver\Exceptions\QueryException;
6+
57
interface QueryInterface
68
{
79
/**
810
* QueryInterface constructor.
911
*
10-
* @param string $query The SQL query to execute or prepare.
12+
* @param string $query The SQL query to execute or prepare.
1113
* @param DriverInterface $driver The driver instance to use for this query.
1214
*/
1315
public function __construct(string $query, DriverInterface $driver);
@@ -16,6 +18,8 @@ public function __construct(string $query, DriverInterface $driver);
1618
* Execute the query and return a QueryResponseInterface instance.
1719
*
1820
* @return QueryResponseInterface The response object containing the results of the query execution.
21+
*
22+
* @throws QueryException on failure.
1923
*/
2024
public function execute(): QueryResponseInterface;
2125
}

src/PDODriver.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
class PDODriver implements DriverInterface
1313
{
1414
public readonly PDO $pdo;
15-
protected static self $instance;
1615

1716
/**
1817
* @throws DriverException
@@ -49,7 +48,7 @@ public function connect(): void
4948
try {
5049
$this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options);
5150
} catch (PDOException $e) {
52-
throw new DriverException($e->getMessage(), $e->getCode());
51+
throw DriverException::createFromPdo($e);
5352
}
5453
}
5554

@@ -62,4 +61,51 @@ public function isConnected(): bool
6261
{
6362
return isset($this->pdo);
6463
}
64+
65+
/**
66+
* @throws DriverException
67+
*/
68+
public function begin(): void
69+
{
70+
try {
71+
if (!$this->pdo->beginTransaction()) {
72+
throw new DriverException('Failed to begin transaction.');
73+
}
74+
} catch (PDOException $e) {
75+
throw DriverException::createFromPdo($e);
76+
}
77+
}
78+
79+
/**
80+
* @throws DriverException
81+
*/
82+
public function commit(): void
83+
{
84+
try {
85+
if (!$this->pdo->commit()) {
86+
throw new DriverException('Failed to commit transaction.');
87+
}
88+
} catch (PDOException $e) {
89+
throw DriverException::createFromPdo($e);
90+
}
91+
}
92+
93+
/**
94+
* @throws DriverException
95+
*/
96+
public function rollback(): void
97+
{
98+
try {
99+
if (!$this->pdo->rollBack()) {
100+
throw new DriverException('Failed to rollback transaction.');
101+
}
102+
} catch (PDOException $e) {
103+
throw DriverException::createFromPdo($e);
104+
}
105+
}
106+
107+
public function inTransaction(): bool
108+
{
109+
return $this->pdo->inTransaction();
110+
}
65111
}

0 commit comments

Comments
 (0)