8000 finish transaction support · SourceCode/pixie@fe2a498 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe2a498

Browse files
committed
finish transaction support
closes usmanhalalit#17 - add support to manually end transactions without an exception being generated - add transaction usage info to readme
1 parent 28606d6 commit fe2a498

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Library on [Packagist](https://packagist.org/packages/usmanhalalit/pixie).
113113
- [Insert with ON DUPLICATE KEY statement](#insert-with-on-duplicate-key-statement)
114114
- [**Update**](#update)
115115
- [**Delete**](#delete)
116+
- [Transactions](#transactions)
116117
- [Get Built Query](#get-built-query)
117118
- [Sub Queries and Nested Queries](#sub-queries-and-nested-queries)
118119
- [Get PDO Instance](#get-pdo-instance)
@@ -469,6 +470,45 @@ QB::table('my_table')->where('id', '>', 5)->delete();
469470
```
470471
Will delete all the rows where id is greater than 5.
471472

473+
### Transactions
474+
475+
Pixie has the ability to run database "transactions", in which all database
476+
changes are not saved until committed. That way, if something goes wrong or
477+
differently then you intend, the database changes are not saved and no changes
478+
are made.
479+
480+
Here's a basic transaction:
481+
482+
```PHP
483+
QB::transaction(function ($qb) {
484+
$qb->table('my_table')->insert(array(
485+
'name' => 'Test',
486+
'url' => 'example.com'
487+
));
488+
489+
$qb->table('my_table')->insert(array(
490+
'name' => 'Test2',
491+
'url' => 'example.com'
492+
));
493+
});
494+
```
495+
496+
If this were to cause any errors (such as a duplicate name or some other such
497+
error), neither data set would show up in the database. If not, the changes would
498+
be successfully saved.
499+
500+
If you wish to manually commit or rollback your changes, you can use the
501+
`commit()` and `rollback()` methods accordingly:
502+
503+
```PHP
504+
QB::transaction(function (qb) {
505+
$qb->table('my_table')->insert(array(/* data... */));
506+
507+
$qb->commit(); // to commit the changes (data would be saved)
508+
$qb->rollback(); // to rollback the changes (data would be rejected)
509+
});
510+
```
511+
472512
### Get Built Query
473513
Sometimes you may need to get the query string, its possible.
474514
```PHP

src/Pixie/QueryBuilder/QueryBuilderHandler.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,18 +789,37 @@ public function join($table, $key, $operator = null, $value = null, $type = 'inn
789789
return $this;
790790
}
791791

792+
/**
793+
* Runs a transaction
794+
*
795+
* @param $callback
796+
*
797+
* @return $this
798+
*/
792799
public function transaction(\Closure $callback)
793800
{
794801
try {
802+
// Begin the PDO transaction
795803
$this->pdo->beginTransaction();
796804

797-
$callback($this);
805+
// Get the Transaction class
806+
$transaction = $this->container->build('\\Pixie\\QueryBuilder\\Transaction', array($this->connection));
807+
808+
// Call closure
809+
$callback($transaction);
810+
811+
// If no errors have been thrown or the transaction wasn't completed within
812+
// the closure, commit the changes
798813
$this->pdo->commit();
799814

800-
return true;
815+
return $this;
816+
} catch (TransactionHaltException $e) {
817+
// Commit or rollback behavior has been handled in the closure, so exit
818+
return $this;
801819
} catch (\Exception $e) {
820+
// something happened, rollback changes
802821
$this->pdo->rollBack();
803-
return false;
822+
return $this;
804823
}
805824
}
806825

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Pixie\QueryBuilder;
4+
5+
class Transaction extends QueryBuilderHandler {
6+
7+
/**
8+
* Commit the database changes
9+
*/
10+
public function commit() {
11+
$this->pdo->commit();
12+
throw new TransactionHaltException();
13+
}
14+
15+
/**
16+
* Rollback the database changes
17+
*/
18+
public function rollback() {
19+
$this->pdo->rollBack();
20+
throw new TransactionHaltException();
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Pixie\QueryBuilder;
4+
5+
class TransactionHaltException extends \Exception {}

0 commit comments

Comments
 (0)
0