5
5
use Kir \MySQL \Builder \Exception ;
6
6
use Kir \MySQL \Database ;
7
7
use Kir \MySQL \Tools \AliasRegistry ;
8
+ use PDO ;
9
+ use PDOStatement ;
10
+ use Psr \Log \LoggerInterface ;
11
+ use Psr \Log \NullLogger ;
8
12
use UnexpectedValueException ;
9
13
use Kir \MySQL \Builder \RunnableSelect ;
10
14
@@ -16,7 +20,7 @@ class MySQL implements Database {
16
20
*/
17
21
private static $ tableFields = array ();
18
22
/**
19
- * @var \ PDO
23
+ * @var PDO
20
24
*/
21
25
private $ pdo ;
22
26
/**
@@ -27,12 +31,22 @@ class MySQL implements Database {
27
31
* @var int
28
32
*/
29
33
private $ transactionLevel = 0 ;
34
+ /**
35
+ * @var LoggerInterface
36
+ */
37
+ private $ logger ;
30
38
31
39
/**
32
- * @param \PDO $pdo
40
+ * @param PDO $pdo
41
+ * @param LoggerInterface $logger
33
42
*/
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
+
35
48
$ this ->pdo = $ pdo ;
49
+ $ this ->logger = $ logger ;
36
50
$ this ->aliasRegistry = new AliasRegistry ();
37
51
}
38
52
@@ -46,10 +60,13 @@ public function getAliasRegistry() {
46
60
/**
47
61
* @param string $query
48
62
* @throws Exception
49
- * @return \ PDOStatement
63
+ * @return PDOStatement
50
64
*/
51
65
public function query ($ query ) {
66
+ $ this ->logger ->info ($ query );
67
+ $ timer = microtime (true );
52
68
$ stmt = $ this ->pdo ->query ($ query );
69
+ $ this ->logger ->debug (sprintf ("Last query duration: %0.5f sec. " , microtime (true ) - $ timer ));
53
70
if (!$ stmt ) {
54
71
throw new Exception ("Could not execute statement: \n{$ query }" );
55
72
}
@@ -59,9 +76,10 @@ public function query($query) {
59
76
/**
60
77
* @param string $query
61
78
* @throws Exception
62
- * @return \ PDOStatement
79
+ * @return PDOStatement
63
80
*/
64
81
public function prepare ($ query ) {
82
+ $ this ->logger ->info ("PREPARE: {$ query }" );
65
83
$ stmt = $ this ->pdo ->prepare ($ query );
66
84
if (!$ stmt ) {
67
85
throw new Exception ("Could not execute statement: \n{$ query }" );
@@ -75,8 +93,11 @@ public function prepare($query) {
75
93
* @return int
76
94
*/
77
95
public function exec ($ query , array $ params = array ()) {
96
+ $ this ->logger ->info ($ query );
97
+ $ timer = microtime (true );
78
98
$ stmt = $ this ->pdo ->prepare ($ query );
79
99
$ stmt ->execute ($ params );
100
+ $ this ->logger ->debug (sprintf ("Last query duration: %0.5f sec. " , microtime (true ) - $ timer ));
80
101
$ result = $ stmt ->rowCount ();
81
102
$ stmt ->closeCursor ();
82
103
return $ result ;
@@ -99,7 +120,7 @@ public function getTableFields($table) {
99
120
}
100
121
$ stmt = $ this ->pdo ->query ("DESCRIBE {$ table }" );
101
122
$ stmt ->execute ();
102
- $ rows = $ stmt ->fetchAll (\ PDO ::FETCH_ASSOC );
123
+ $ rows = $ stmt ->fetchAll (PDO ::FETCH_ASSOC );
103
124
self ::$ tableFields [$ table ] = array_map (function ($ row ) { return $ row ['Field ' ]; }, $ rows );
104
125
$ stmt ->closeCursor ();
105
126
return self ::$ tableFields [$ table ];
0 commit comments