1
1
<?php
2
2
namespace Kir \MySQL \Databases ;
3
3
4
- use PDO ;
5
- use Exception ;
6
- use PDOStatement ;
7
4
use Kir \MySQL \Builder ;
5
+ use Kir \MySQL \Builder \Exception ;
8
6
use Kir \MySQL \Database ;
9
7
use UnexpectedValueException ;
10
8
use Kir \MySQL \Builder \RunnableSelect ;
@@ -16,21 +14,21 @@ class MySQL implements Database {
16
14
private static $ tableFields = array ();
17
15
18
16
/**
19
- * @var PDO
17
+ * @var \ PDO
20
18
*/
21
19
private $ pdo ;
22
20
23
21
/**
24
- * @param PDO $pdo
22
+ * @param \ PDO $pdo
25
23
*/
26
- public function __construct (PDO $ pdo ) {
24
+ public function __construct (\ PDO $ pdo ) {
27
25
$ this ->pdo = $ pdo ;
28
26
}
29
27
30
28
/**
31
29
* @param string $query
32
30
* @throws Exception
33
- * @return PDOStatement
31
+ * @return \ PDOStatement
34
32
*/
35
33
public function query ($ query ) {
36
34
$ stmt = $ this ->pdo ->query ($ query );
@@ -43,7 +41,7 @@ public function query($query) {
43
41
/**
44
42
* @param string $query
45
43
* @throws Exception
46
- * @return PDOStatement
44
+ * @return \ PDOStatement
47
45
*/
48
46
public function prepare ($ query ) {
49
47
$ stmt = $ this ->pdo ->prepare ($ query );
@@ -55,10 +53,15 @@ public function prepare($query) {
55
53
56
54
/**
57
55
* @param string $query
56
+ * @param array $params
58
57
* @return int
59
58
*/
60
- public function exec ($ query ) {
61
- return $ this ->pdo ->exec ($ query );
59
+ public function exec ($ query , array $ params = array ()) {
60
+ $ stmt = $ this ->pdo ->prepare ($ query );
61
+ $ stmt ->execute ($ params );
62
+ $ result = $ stmt ->rowCount ();
63
+ $ stmt ->closeCursor ();
64
+ return $ result ;
62
65
}
63
66
64
67
/**
@@ -78,7 +81,7 @@ public function getTableFields($table) {
78
81
}
79
82
$ stmt = $ this ->pdo ->query ("DESCRIBE {$ table }" );
80
83
$ stmt ->execute ();
81
- $ rows = $ stmt ->fetchAll (PDO ::FETCH_ASSOC );
84
+ $ rows = $ stmt ->fetchAll (\ PDO ::FETCH_ASSOC );
82
85
self ::$ tableFields [$ table ] = array_map (function ($ row ) { return $ row ['Field ' ]; }, $ rows );
83
86
$ stmt ->closeCursor ();
84
87
return self ::$ tableFields [$ table ];
@@ -192,4 +195,21 @@ public function transactionRollback() {
192
195
$ this ->pdo ->rollBack ();
193
196
return $ this ;
194
197
}
198
+
199
+ /**
200
+ * @param callable $callback
201
+ * @throws \Exception
202
+ * @return $this
203
+ */
204
+ public function transaction ($ callback ) {
205
+ try {
206
+ $ this ->pdo ->beginTransaction ();
207
+ call_user_func ($ callback , $ this );
208
+ $ this ->pdo ->commit ();
209
+ } catch (\Exception $ e ) {
210
+ $ this ->pdo ->rollBack ();
211
+ throw $ e ;
212
+ }
213
+ return $ this ;
214
+ }
195
215
}
0 commit comments