1
|
|
|
<?php |
2
|
|
|
namespace Kir\MySQL\Builder; |
3
|
|
|
|
4
|
|
|
use Kir\MySQL\Databases\MySQL\MySQLExceptionInterpreter; |
5
|
|
|
use Kir\MySQL\Exceptions\SqlException; |
6
|
|
|
use PDO; |
7
|
|
|
use PDOException; |
8
|
|
|
use PDOStatement; |
9
|
|
|
use Kir\MySQL\Database\DatabaseStatement; |
10
|
|
|
use Kir\MySQL\QueryLogger\QueryLoggers; |
11
|
|
|
|
12
|
|
|
class QueryStatement implements DatabaseStatement { |
13
|
|
|
/** @var PDOStatement */ |
14
|
|
|
private $statement; |
15
|
|
|
/** @var QueryLoggers */ |
16
|
|
|
private $queryLoggers; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $query; |
19
|
|
|
/** @var MySQLExceptionInterpreter */ |
20
|
|
|
private $exceptionInterpreter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param PDOStatement $stmt |
24
|
|
|
* @param string $query |
25
|
|
|
* @param MySQLExceptionInterpreter $exceptionInterpreter |
26
|
|
|
* @param QueryLoggers $queryLoggers |
27
|
|
|
*/ |
28
|
|
|
public function __construct(PDOStatement $stmt, $query, MySQLExceptionInterpreter $exceptionInterpreter, QueryLoggers $queryLoggers) { |
29
|
|
|
$this->statement = $stmt; |
30
|
|
|
$this->queryLoggers = $queryLoggers; |
31
|
|
|
$this->query = $query; |
32
|
|
|
$this->exceptionInterpreter = $exceptionInterpreter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return PDOStatement |
37
|
|
|
*/ |
38
|
|
|
public function getStatement() { |
39
|
|
|
return $this->statement; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param int $mode |
44
|
|
|
* @param mixed $arg0 |
45
|
|
|
* @param array $arg1 |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setFetchMode($mode, $arg0 = null, array $arg1 = null) { |
49
|
|
|
if($arg1 !== null) { |
50
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
51
|
|
|
$this->statement->setFetchMode($mode, $arg0, $arg1); |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
if($arg0 !== null) { |
55
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
56
|
|
|
$this->statement->setFetchMode($mode, $arg0); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
$this->statement->setFetchMode($mode); |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $params |
65
|
|
|
* @throws SqlException |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function execute(array $params = []) { |
69
|
|
|
$this->exceptionHandler(function() use ($params) { |
70
|
|
|
$timer = microtime(true); |
71
|
|
|
$response = $this->statement->execute($params); |
72
|
|
|
$this->queryLoggers->log($this->query, microtime(true)-$timer); |
73
|
|
|
if(!$response) { |
74
|
|
|
throw new SqlException('Execution returned with "false".'); |
75
|
|
|
} |
76
|
|
|
}); |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $fetchStyle |
82
|
|
|
* @param mixed $fetchArgument |
83
|
|
|
* @param array $ctorArgs |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function fetchAll($fetchStyle = null, $fetchArgument = null, array $ctorArgs = []) { |
87
|
|
|
return $this->exceptionHandler(function() use ($fetchStyle, $fetchArgument, $ctorArgs) { |
88
|
|
|
if($fetchArgument !== null) { |
89
|
|
|
return $this->statement->fetchAll($fetchStyle, $fetchArgument, $ctorArgs); |
90
|
|
|
} |
91
|
|
|
return $this->statement->fetchAll($fetchStyle); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param int $fetchStyle |
97
|
|
|
* @param int $cursorOrientation |
98
|
|
|
* @param int $cursorOffset |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function fetch($fetchStyle = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) { |
102
|
|
|
return $this->exceptionHandler(function() use ($fetchStyle, $cursorOrientation, $cursorOffset) { |
103
|
|
|
return $this->statement->fetch($fetchStyle, $cursorOrientation, $cursorOffset); |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int $columnNo |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function fetchColumn($columnNo = 0) { |
112
|
|
|
return $this->exceptionHandler(function() use ($columnNo) { |
113
|
|
|
return $this->statement->fetchColumn($columnNo); |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function closeCursor() { |
121
|
|
|
return $this->exceptionHandler(function() { |
122
|
|
|
return $this->statement->closeCursor(); |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function columnCount() { |
130
|
|
|
return $this->exceptionHandler(function() { |
131
|
|
|
return $this->statement->columnCount(); |
132
|
|
|
}); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param int $columnNo |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public function getColumnMeta($columnNo) { |
140
|
|
|
return $this->exceptionHandler(function() use ($columnNo) { |
141
|
|
|
return $this->statement->getColumnMeta($columnNo); |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param callable $fn |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
private function exceptionHandler($fn) { |
150
|
|
|
try { |
151
|
|
|
return call_user_func($fn); |
152
|
|
|
} catch (PDOException $e) { |
153
|
|
|
$this->exceptionInterpreter->throwMoreConcreteException($e); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|