11<?php
22
3- declare (strict_types= 1 );
3+ declare (strict_types = 1 );
44/**
55 * This file is part of Simps.
66 *
77 * @link https://simps.io
88 * @document https://doc.simps.io
99 * @license https://github.com/simple-swoole/simps/blob/master/LICENSE
1010 */
11+
1112namespace Simps \DB ;
1213
1314use PDO ;
1718
1819class DB
1920{
21+
2022 protected $ pool ;
2123
2224 /** @var PDO */
2325 protected $ pdo ;
24-
2526 private $ in_transaction = false ;
2627
2728 public function __construct ($ config = null )
2829 {
29- if (! empty ($ config )) {
30+ if (!empty ($ config )) {
3031 $ this ->pool = \Simps \DB \PDO ::getInstance ($ config );
3132 } else {
3233 $ this ->pool = \Simps \DB \PDO ::getInstance ();
@@ -36,7 +37,13 @@ public function __construct($config = null)
3637 public function quote (string $ string , int $ parameter_type = PDO ::PARAM_STR )
3738 {
3839 $ this ->realGetConn ();
39- $ ret = $ this ->pdo ->quote ($ string , $ parameter_type );
40+ try {
41+ $ ret = $ this ->pdo ->quote ($ string , $ parameter_type );
42+ } catch (\Exception $ exc ) {
43+ $ this ->release ($ this ->pdo );
44+ throw $ exc ;
45+ }
46+
4047 $ this ->release ($ this ->pdo );
4148 return $ ret ;
4249 }
@@ -47,7 +54,12 @@ public function beginTransaction(): void
4754 throw new RuntimeException ('do not support nested transaction now ' );
4855 }
4956 $ this ->realGetConn ();
50- $ this ->pdo ->beginTransaction ();
57+ try {
58+ $ this ->pdo ->beginTransaction ();
59+ } catch (\Exception $ exc ) {
60+ $ this ->release ($ this ->pdo );
61+ throw $ exc ;
62+ }
5163 $ this ->in_transaction = true ;
5264 Coroutine::defer (function () {
5365 if ($ this ->in_transaction ) {
@@ -58,29 +70,46 @@ public function beginTransaction(): void
5870
5971 public function commit (): void
6072 {
61- $ this ->pdo ->commit ();
6273 $ this ->in_transaction = false ;
74+ try {
75+ $ this ->pdo ->commit ();
76+ } catch (\Exception $ exc ) {
77+ $ this ->release ($ this ->pdo );
78+ throw $ exc ;
79+ }
6380 $ this ->release ($ this ->pdo );
6481 }
6582
6683 public function rollBack (): void
6784 {
68- $ this ->pdo ->rollBack ();
6985 $ this ->in_transaction = false ;
86+
87+ try {
88+ $ this ->pdo ->rollBack ();
89+ } catch (\Exception $ exc ) {
90+ $ this ->release ($ this ->pdo );
91+ throw $ exc ;
92+ }
93+
7094 $ this ->release ($ this ->pdo );
7195 }
7296
7397 public function query (string $ query , array $ bindings = []): array
7498 {
7599 $ this ->realGetConn ();
100+ try {
76101
77- $ statement = $ this ->pdo ->prepare ($ query );
102+ $ statement = $ this ->pdo ->prepare ($ query );
78103
79- $ this ->bindValues ($ statement , $ bindings );
104+ $ this ->bindValues ($ statement , $ bindings );
80105
81- $ statement ->execute ();
106+ $ statement ->execute ();
82107
83- $ ret = $ statement ->fetchAll ();
108+ $ ret = $ statement ->fetchAll ();
109+ } catch (\Exception $ exc ) {
110+ $ this ->release ($ this ->pdo );
111+ throw $ exc ;
112+ }
84113
85114 $ this ->release ($ this ->pdo );
86115
@@ -97,14 +126,19 @@ public function fetch(string $query, array $bindings = [])
97126 public function execute (string $ query , array $ bindings = []): int
98127 {
99128 $ this ->realGetConn ();
129+ try {
100130
101- $ statement = $ this ->pdo ->prepare ($ query );
131+ $ statement = $ this ->pdo ->prepare ($ query );
102132
103- $ this ->bindValues ($ statement , $ bindings );
133+ $ this ->bindValues ($ statement , $ bindings );
104134
105- $ statement ->execute ();
135+ $ statement ->execute ();
106136
107- $ ret = $ statement ->rowCount ();
137+ $ ret = $ statement ->rowCount ();
138+ } catch (\Exception $ exc ) {
139+ $ this ->release ($ this ->pdo );
140+ throw $ exc ;
141+ }
108142
109143 $ this ->release ($ this ->pdo );
110144
@@ -114,8 +148,14 @@ public function execute(string $query, array $bindings = []): int
114148 public function exec (string $ sql ): int
115149 {
116150 $ this ->realGetConn ();
151+ try {
152+
153+ $ ret = $ this ->pdo ->exec ($ sql );
154+ } catch (\Exception $ exc ) {
155+ $ this ->release ($ this ->pdo );
156+ throw $ exc ;
157+ }
117158
118- $ ret = $ this ->pdo ->exec ($ sql );
119159
120160 $ this ->release ($ this ->pdo );
121161
@@ -126,13 +166,19 @@ public function insert(string $query, array $bindings = []): int
126166 {
127167 $ this ->realGetConn ();
128168
129- $ statement = $ this ->pdo ->prepare ($ query );
169+ try {
170+ $ statement = $ this ->pdo ->prepare ($ query );
171+
172+ $ this ->bindValues ($ statement , $ bindings );
130173
131- $ this -> bindValues ( $ statement , $ bindings );
174+ $ statement -> execute ( );
132175
133- $ statement ->execute ();
176+ $ ret = (int ) $ this ->pdo ->lastInsertId ();
177+ } catch (\Exception $ exc ) {
178+ $ this ->release ($ this ->pdo );
179+ throw $ exc ;
180+ }
134181
135- $ ret = (int ) $ this ->pdo ->lastInsertId ();
136182
137183 $ this ->release ($ this ->pdo );
138184
@@ -145,7 +191,7 @@ public function release($connection = null)
145191 $ this ->in_transaction = false ;
146192 }
147193
148- if (! $ this ->in_transaction ) {
194+ if (!$ this ->in_transaction ) {
149195 $ this ->pool ->close ($ connection );
150196 return true ;
151197 }
@@ -157,17 +203,16 @@ protected function bindValues(PDOStatementProxy $statement, array $bindings): vo
157203 {
158204 foreach ($ bindings as $ key => $ value ) {
159205 $ statement ->bindValue (
160- is_string ($ key ) ? $ key : $ key + 1 ,
161- $ value ,
162- is_int ($ value ) ? PDO ::PARAM_INT : PDO ::PARAM_STR
206+ is_string ($ key ) ? $ key : $ key + 1 , $ value , is_int ($ value ) ? PDO ::PARAM_INT : PDO ::PARAM_STR
163207 );
164208 }
165209 }
166210
167211 private function realGetConn ()
168212 {
169- if (! $ this ->in_transaction ) {
213+ if (!$ this ->in_transaction ) {
170214 $ this ->pdo = $ this ->pool ->getConnection ();
171215 }
172216 }
217+
173218}
0 commit comments