Skip to content

Commit 01b5358

Browse files
committed
[ENH] Add support to query transactions
1 parent 9e58b52 commit 01b5358

6 files changed

Lines changed: 77 additions & 13 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=7.4"
19+
"php": ">=7.4",
20+
"ext-pdo": "*"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "^9.5"

src/DB.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ class DB
2020
private \PDO $pdo;
2121
private string $_action="";
2222
private int $_count;
23-
private array $option=[
24-
\PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8",
25-
\PDO::ATTR_EMULATE_PREPARES=>false,
26-
\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION
27-
] ;
23+
private string $db_name;
2824
use BuildQuery;
2925
private function __construct(string $host="",string $db_name="",string $user_name="",string $password="")
3026
{
@@ -33,8 +29,13 @@ private function __construct(string $host="",string $db_name="",string $user_nam
3329
$this->_error = null;
3430
$this->_lastID = -1;
3531
$this->_count = 0;
32+
$this->db_name = $db_name;
3633
//
37-
$this->pdo = new \PDO("mysql:host=" . $host . ";dbname=" . $db_name.";charset=utf8mb4", $user_name,$password,$this->option);
34+
$this->pdo = new \PDO("mysql:host=" . $host . ";dbname=" . $db_name.";charset=utf8mb4", $user_name,$password);
35+
$this->pdo->setAttribute(\PDO::MYSQL_ATTR_INIT_COMMAND,'SET NAMES utf8');
36+
$this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES,false);
37+
$this->pdo->setAttribute(\PDO::ATTR_PERSISTENT,true);
38+
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION);
3839
} catch (\PDOException $ex) {
3940
echo $ex->getMessage();
4041
die();
@@ -181,4 +182,31 @@ function rowCount(){
181182
}
182183
return $this->_count;
183184
}
185+
186+
function beginTransaction(): bool
187+
{
188+
return $this->pdo->beginTransaction();
189+
}
190+
function commit(){
191+
return $this->pdo->commit();
192+
}
193+
function rollBack(){
194+
return $this->pdo->rollBack();
195+
}
196+
197+
/**
198+
* @throws Exception
199+
*/
200+
function transaction(\Closure $callable){
201+
try{
202+
$this->pdo->beginTransaction();
203+
$callable($this);
204+
$this->pdo->commit();
205+
}catch (\Exception $ex){
206+
if ($this->pdo->inTransaction()) {
207+
$this->pdo->rollBack();
208+
}
209+
throw $ex;
210+
}
211+
}
184212
}

src/DB_Insert.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ private function query(string $sql, array $params = [])
7171
{
7272
$q = $this->executeQuery($this->pdo, $sql, $params);
7373
$this->_results = $q['result'];
74+
$this->_error = $q['error']??"";
7475
$this->lastID = $q['lastID']??0;
75-
$this->_error = $q['error'];
7676
}
7777

7878
/**
@@ -88,10 +88,10 @@ private function insert()
8888
}
8989

9090
/**
91-
* @return bool
91+
* @return array
9292
* return result after a request select
9393
*/
94-
function result()
94+
function result(): array
9595
{
9696
$this->insert();
9797
return $this->_results;

test/index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"password"=>""
99
];
1010
$db = DB::getInstance($config);
11-
include("./test/insert.php");
11+
// include("./test/insert.php");
1212
//include("./test/select.php");
1313
// include("./test/delete.php");
1414
// include("./test/query.php");
15-
// include("./test/update.php");
15+
// include("./test/update.php");
16+
include("./test/transaction.php");

test/insert.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
$db=$db??[];
2+
$db = $db??[];
33
$field = [
44
"userid" => 1,
55
"message" => "hello from wepesi",

test/transaction.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
$db = $db??[];
3+
$user = [
4+
"fullname" => "Celestin Doe",
5+
"username" => "John Doe",
6+
"password" => md5("12345678"),
7+
"datecreated" => Date("Y-m-d H:i:s",time())
8+
];
9+
10+
$message = [
11+
"message" => "Hello Celestin",
12+
'datecreated' => Date('Y-m-d H:i:s', time())
13+
];
14+
try {
15+
$db->transaction(function($db) use ($user,$message){
16+
$db->insert('users')->field($user)->result();
17+
if ($db->error()) {
18+
throw new \Exception($db->error());
19+
}
20+
$user_id = $db->lastId();
21+
$user['id'] = $user_id;
22+
$message['userid'] = $user_id;
23+
$db->insert('message')->field($message)->result();
24+
if ($db->error()) {
25+
throw new \Exception($db->error());
26+
}
27+
$message['id'] = $db->lastId();
28+
$user['messages'] = $message;
29+
30+
print_r($user);
31+
});
32+
} catch (\Exception $ex) {
33+
var_dump($ex);
34+
}

0 commit comments

Comments
 (0)