Skip to content

Commit b1cb7e3

Browse files
committed
Add transaction method (unfinished)
1 parent 22b47f9 commit b1cb7e3

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/PeachySql.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ public function __construct(PDO $connection, ?Options $options = null)
2929
$this->options = $options;
3030
}
3131

32+
/**
33+
* Automatically begins a transaction, runs the callback function, and commits
34+
* the transaction on success, or rolls it back if an error occurs.
35+
*/
36+
public function transaction(\Closure $callback): void
37+
{
38+
$this->begin();
39+
try {
40+
$callback();
41+
$this->commit();
42+
} catch (\Throwable $e) {
43+
$this->rollback();
44+
throw $e;
45+
}
46+
}
47+
3248
/**
3349
* Begins a transaction
3450
* @throws SqlException if an error occurs

0 commit comments

Comments
 (0)