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-
1211namespace Simps \DB ;
1312
1413use PDO ;
1514use RuntimeException ;
1615use Swoole \Coroutine ;
1716use Swoole \Database \PDOStatementProxy ;
17+ use Throwable ;
1818
1919class DB
2020{
21-
2221 protected $ pool ;
2322
2423 /** @var PDO */
2524 protected $ pdo ;
25+
2626 private $ in_transaction = false ;
2727
2828 public function __construct ($ config = null )
2929 {
30- if (!empty ($ config )) {
30+ if (! empty ($ config )) {
3131 $ this ->pool = \Simps \DB \PDO ::getInstance ($ config );
3232 } else {
3333 $ this ->pool = \Simps \DB \PDO ::getInstance ();
@@ -39,9 +39,9 @@ public function quote(string $string, int $parameter_type = PDO::PARAM_STR)
3939 $ this ->realGetConn ();
4040 try {
4141 $ ret = $ this ->pdo ->quote ($ string , $ parameter_type );
42- } catch (\ Exception $ exc ) {
43- $ this ->release ($ this -> pdo );
44- throw $ exc ;
42+ } catch (Throwable $ th ) {
43+ $ this ->release ();
44+ throw $ th ;
4545 }
4646
4747 $ this ->release ($ this ->pdo );
@@ -56,9 +56,9 @@ public function beginTransaction(): void
5656 $ this ->realGetConn ();
5757 try {
5858 $ this ->pdo ->beginTransaction ();
59- } catch (\ Exception $ exc ) {
60- $ this ->release ($ this -> pdo );
61- throw $ exc ;
59+ } catch (Throwable $ th ) {
60+ $ this ->release ();
61+ throw $ th ;
6262 }
6363 $ this ->in_transaction = true ;
6464 Coroutine::defer (function () {
@@ -73,9 +73,9 @@ public function commit(): void
7373 $ this ->in_transaction = false ;
7474 try {
7575 $ this ->pdo ->commit ();
76- } catch (\ Exception $ exc ) {
77- $ this ->release ($ this -> pdo );
78- throw $ exc ;
76+ } catch (Throwable $ th ) {
77+ $ this ->release ();
78+ throw $ th ;
7979 }
8080 $ this ->release ($ this ->pdo );
8181 }
@@ -86,9 +86,9 @@ public function rollBack(): void
8686
8787 try {
8888 $ this ->pdo ->rollBack ();
89- } catch (\ Exception $ exc ) {
90- $ this ->release ($ this -> pdo );
91- throw $ exc ;
89+ } catch (Throwable $ th ) {
90+ $ this ->release ();
91+ throw $ th ;
9292 }
9393
9494 $ this ->release ($ this ->pdo );
@@ -98,17 +98,16 @@ public function query(string $query, array $bindings = []): array
9898 {
9999 $ this ->realGetConn ();
100100 try {
101-
102101 $ statement = $ this ->pdo ->prepare ($ query );
103102
104103 $ this ->bindValues ($ statement , $ bindings );
105104
106105 $ statement ->execute ();
107106
108107 $ ret = $ statement ->fetchAll ();
109- } catch (\ Exception $ exc ) {
110- $ this ->release ($ this -> pdo );
111- throw $ exc ;
108+ } catch (Throwable $ th ) {
109+ $ this ->release ();
110+ throw $ th ;
112111 }
113112
114113 $ this ->release ($ this ->pdo );
@@ -127,17 +126,16 @@ public function execute(string $query, array $bindings = []): int
127126 {
128127 $ this ->realGetConn ();
129128 try {
130-
131129 $ statement = $ this ->pdo ->prepare ($ query );
132130
133131 $ this ->bindValues ($ statement , $ bindings );
134132
135133 $ statement ->execute ();
136134
137135 $ ret = $ statement ->rowCount ();
138- } catch (\ Exception $ exc ) {
139- $ this ->release ($ this -> pdo );
140- throw $ exc ;
136+ } catch (Throwable $ th ) {
137+ $ this ->release ();
138+ throw $ th ;
141139 }
142140
143141 $ this ->release ($ this ->pdo );
@@ -149,14 +147,12 @@ public function exec(string $sql): int
149147 {
150148 $ this ->realGetConn ();
151149 try {
152-
153150 $ ret = $ this ->pdo ->exec ($ sql );
154- } catch (\ Exception $ exc ) {
155- $ this ->release ($ this -> pdo );
156- throw $ exc ;
151+ } catch (Throwable $ th ) {
152+ $ this ->release ();
153+ throw $ th ;
157154 }
158155
159-
160156 $ this ->release ($ this ->pdo );
161157
162158 return $ ret ;
@@ -174,12 +170,11 @@ public function insert(string $query, array $bindings = []): int
174170 $ statement ->execute ();
175171
176172 $ ret = (int ) $ this ->pdo ->lastInsertId ();
177- } catch (\ Exception $ exc ) {
178- $ this ->release ($ this -> pdo );
179- throw $ exc ;
173+ } catch (Throwable $ th ) {
174+ $ this ->release ();
175+ throw $ th ;
180176 }
181177
182-
183178 $ this ->release ($ this ->pdo );
184179
185180 return $ ret ;
@@ -191,7 +186,7 @@ public function release($connection = null)
191186 $ this ->in_transaction = false ;
192187 }
193188
194- if (!$ this ->in_transaction ) {
189+ if (! $ this ->in_transaction ) {
195190 $ this ->pool ->close ($ connection );
196191 return true ;
197192 }
@@ -203,16 +198,17 @@ protected function bindValues(PDOStatementProxy $statement, array $bindings): vo
203198 {
204199 foreach ($ bindings as $ key => $ value ) {
205200 $ statement ->bindValue (
206- is_string ($ key ) ? $ key : $ key + 1 , $ value , is_int ($ value ) ? PDO ::PARAM_INT : PDO ::PARAM_STR
201+ is_string ($ key ) ? $ key : $ key + 1 ,
202+ $ value ,
203+ is_int ($ value ) ? PDO ::PARAM_INT : PDO ::PARAM_STR
207204 );
208205 }
209206 }
210207
211208 private function realGetConn ()
212209 {
213- if (!$ this ->in_transaction ) {
210+ if (! $ this ->in_transaction ) {
214211 $ this ->pdo = $ this ->pool ->getConnection ();
215212 }
216213 }
217-
218214}
0 commit comments