11<?php
22namespace Kir \MySQL \Databases ;
33
4- use PDO ;
5- use Exception ;
6- use PDOStatement ;
74use Kir \MySQL \Builder ;
5+ use Kir \MySQL \Builder \Exception ;
86use Kir \MySQL \Database ;
97use UnexpectedValueException ;
108use Kir \MySQL \Builder \RunnableSelect ;
@@ -16,21 +14,21 @@ class MySQL implements Database {
1614 private static $ tableFields = array ();
1715
1816 /**
19- * @var PDO
17+ * @var \ PDO
2018 */
2119 private $ pdo ;
2220
2321 /**
24- * @param PDO $pdo
22+ * @param \ PDO $pdo
2523 */
26- public function __construct (PDO $ pdo ) {
24+ public function __construct (\ PDO $ pdo ) {
2725 $ this ->pdo = $ pdo ;
2826 }
2927
3028 /**
3129 * @param string $query
3230 * @throws Exception
33- * @return PDOStatement
31+ * @return \ PDOStatement
3432 */
3533 public function query ($ query ) {
3634 $ stmt = $ this ->pdo ->query ($ query );
@@ -43,7 +41,7 @@ public function query($query) {
4341 /**
4442 * @param string $query
4543 * @throws Exception
46- * @return PDOStatement
44+ * @return \ PDOStatement
4745 */
4846 public function prepare ($ query ) {
4947 $ stmt = $ this ->pdo ->prepare ($ query );
@@ -55,10 +53,15 @@ public function prepare($query) {
5553
5654 /**
5755 * @param string $query
56+ * @param array $params
5857 * @return int
5958 */
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 ;
6265 }
6366
6467 /**
@@ -78,7 +81,7 @@ public function getTableFields($table) {
7881 }
7982 $ stmt = $ this ->pdo ->query ("DESCRIBE {$ table }" );
8083 $ stmt ->execute ();
81- $ rows = $ stmt ->fetchAll (PDO ::FETCH_ASSOC );
84+ $ rows = $ stmt ->fetchAll (\ PDO ::FETCH_ASSOC );
8285 self ::$ tableFields [$ table ] = array_map (function ($ row ) { return $ row ['Field ' ]; }, $ rows );
8386 $ stmt ->closeCursor ();
8487 return self ::$ tableFields [$ table ];
@@ -192,4 +195,21 @@ public function transactionRollback() {
192195 $ this ->pdo ->rollBack ();
193196 return $ this ;
194197 }
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+ }
195215}
0 commit comments