55use Kir \MySQL \Builder \Exception ;
66use Kir \MySQL \Database ;
77use Kir \MySQL \Tools \AliasRegistry ;
8+ use PDO ;
9+ use PDOStatement ;
10+ use Psr \Log \LoggerInterface ;
11+ use Psr \Log \NullLogger ;
812use UnexpectedValueException ;
913use Kir \MySQL \Builder \RunnableSelect ;
1014
@@ -16,7 +20,7 @@ class MySQL implements Database {
1620 */
1721 private static $ tableFields = array ();
1822 /**
19- * @var \ PDO
23+ * @var PDO
2024 */
2125 private $ pdo ;
2226 /**
@@ -27,12 +31,22 @@ class MySQL implements Database {
2731 * @var int
2832 */
2933 private $ transactionLevel = 0 ;
34+ /**
35+ * @var LoggerInterface
36+ */
37+ private $ logger ;
3038
3139 /**
32- * @param \PDO $pdo
40+ * @param PDO $pdo
41+ * @param LoggerInterface $logger
3342 */
34- public function __construct (\PDO $ pdo ) {
43+ public function __construct (PDO $ pdo , LoggerInterface $ logger = null ) {
44+ if ($ logger === null ) {
45+ $ logger = new NullLogger ();
46+ }
47+
3548 $ this ->pdo = $ pdo ;
49+ $ this ->logger = $ logger ;
3650 $ this ->aliasRegistry = new AliasRegistry ();
3751 }
3852
@@ -46,10 +60,13 @@ public function getAliasRegistry() {
4660 /**
4761 * @param string $query
4862 * @throws Exception
49- * @return \ PDOStatement
63+ * @return PDOStatement
5064 */
5165 public function query ($ query ) {
66+ $ this ->logger ->info ($ query );
67+ $ timer = microtime (true );
5268 $ stmt = $ this ->pdo ->query ($ query );
69+ $ this ->logger ->debug (sprintf ("Last query duration: %0.5f sec. " , microtime (true ) - $ timer ));
5370 if (!$ stmt ) {
5471 throw new Exception ("Could not execute statement: \n{$ query }" );
5572 }
@@ -59,9 +76,10 @@ public function query($query) {
5976 /**
6077 * @param string $query
6178 * @throws Exception
62- * @return \ PDOStatement
79+ * @return PDOStatement
6380 */
6481 public function prepare ($ query ) {
82+ $ this ->logger ->info ("PREPARE: {$ query }" );
6583 $ stmt = $ this ->pdo ->prepare ($ query );
6684 if (!$ stmt ) {
6785 throw new Exception ("Could not execute statement: \n{$ query }" );
@@ -75,8 +93,11 @@ public function prepare($query) {
7593 * @return int
7694 */
7795 public function exec ($ query , array $ params = array ()) {
96+ $ this ->logger ->info ($ query );
97+ $ timer = microtime (true );
7898 $ stmt = $ this ->pdo ->prepare ($ query );
7999 $ stmt ->execute ($ params );
100+ $ this ->logger ->debug (sprintf ("Last query duration: %0.5f sec. " , microtime (true ) - $ timer ));
80101 $ result = $ stmt ->rowCount ();
81102 $ stmt ->closeCursor ();
82103 return $ result ;
@@ -99,7 +120,7 @@ public function getTableFields($table) {
99120 }
100121 $ stmt = $ this ->pdo ->query ("DESCRIBE {$ table }" );
101122 $ stmt ->execute ();
102- $ rows = $ stmt ->fetchAll (\ PDO ::FETCH_ASSOC );
123+ $ rows = $ stmt ->fetchAll (PDO ::FETCH_ASSOC );
103124 self ::$ tableFields [$ table ] = array_map (function ($ row ) { return $ row ['Field ' ]; }, $ rows );
104125 $ stmt ->closeCursor ();
105126 return self ::$ tableFields [$ table ];
0 commit comments