1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebStream\Database; |
4
|
|
|
|
5
|
|
|
use WebStream\DI\Injector; |
|
|
|
|
6
|
|
|
use WebStream\Database\Driver\DatabaseDriver; |
7
|
|
|
use WebStream\Exception\Extend\DatabaseException; |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Query |
11
|
|
|
* @author Ryuichi TANAKA. |
12
|
|
|
* @since 2013/12/07 |
13
|
|
|
* @version 0.7 |
14
|
|
|
*/ |
15
|
|
|
class Query |
16
|
|
|
{ |
17
|
|
|
use Injector; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var DatabaseDriver データベースコネクション |
21
|
|
|
*/ |
22
|
|
|
private $connection; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string SQL |
26
|
|
|
*/ |
27
|
|
|
private $sql; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array<mixed> バインドパラメータ |
31
|
|
|
*/ |
32
|
|
|
private $bind; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Doctrine\DBAL\Statement ステートメント |
36
|
|
|
*/ |
37
|
|
|
private $stmt; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor |
41
|
|
|
* @param DatabaseDriver データベースコネクション |
|
|
|
|
42
|
|
|
*/ |
43
|
52 |
|
public function __construct(DatabaseDriver $connection) |
44
|
|
|
{ |
45
|
52 |
|
$this->connection = $connection; |
46
|
52 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* SQLを設定する |
50
|
|
|
* @param string SQL |
|
|
|
|
51
|
|
|
*/ |
52
|
52 |
|
public function setSql($sql) |
53
|
|
|
{ |
54
|
52 |
|
$this->sql = $sql; |
55
|
52 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* バインドパラメータを設定する |
59
|
|
|
* @param array<string> バインドパラメータ |
|
|
|
|
60
|
|
|
*/ |
61
|
52 |
|
public function setBind(array $bind) |
62
|
|
|
{ |
63
|
52 |
|
$this->bind = $bind; |
64
|
52 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* SELECT |
68
|
|
|
* @return object 取得結果 |
69
|
|
|
*/ |
70
|
44 |
|
public function select() |
71
|
|
|
{ |
72
|
44 |
|
$this->logger->debug("execute select."); |
73
|
44 |
|
$this->execute(); |
74
|
44 |
|
$result = new Result($this->stmt); |
|
|
|
|
75
|
44 |
|
$result->inject('logger', $this->logger); |
76
|
|
|
|
77
|
44 |
|
return $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* INSERT |
82
|
|
|
* @return integer 処理結果件数 |
83
|
|
|
*/ |
84
|
20 |
|
public function insert() |
85
|
|
|
{ |
86
|
20 |
|
$this->logger->debug("execute insert."); |
87
|
|
|
|
88
|
20 |
|
return $this->execute(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* UPDATE |
93
|
|
|
* @return integer 処理結果件数 |
94
|
|
|
*/ |
95
|
4 |
|
public function update() |
96
|
|
|
{ |
97
|
4 |
|
$this->logger->debug("execute update."); |
98
|
|
|
|
99
|
4 |
|
return $this->execute(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* DELETE |
104
|
|
|
* @return integer 処理結果件数 |
105
|
|
|
*/ |
106
|
20 |
|
public function delete() |
107
|
|
|
{ |
108
|
20 |
|
$this->logger->debug("execute delete."); |
109
|
|
|
|
110
|
20 |
|
return $this->execute(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* SQLを実行する |
115
|
|
|
* @return integer 結果件数 |
116
|
|
|
*/ |
117
|
52 |
|
private function execute() |
118
|
|
|
{ |
119
|
52 |
|
$this->stmt = null; |
120
|
|
|
|
121
|
|
|
try { |
122
|
52 |
|
$stmt = $this->connection->getStatement($this->sql); |
123
|
52 |
|
if ($stmt === false) { |
|
|
|
|
124
|
|
|
throw new DatabaseException("Can't create statement: " . $this->sql); |
125
|
|
|
} |
126
|
52 |
|
$this->logger->info("Executed SQL: " . $this->sql); |
127
|
52 |
|
foreach ($this->bind as $key => $value) { |
128
|
36 |
|
$this->logger->info("Bind statement: $key => $value"); |
129
|
36 |
|
if (preg_match("/^[0-9]+$/", $value) && is_int($value)) { |
130
|
16 |
|
$stmt->bindValue($key, $value, \PDO::PARAM_INT); |
131
|
|
|
} else { |
132
|
24 |
|
$stmt->bindValue($key, $value, \PDO::PARAM_STR); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
52 |
|
if ($stmt->execute()) { |
137
|
52 |
|
$this->stmt = $stmt; |
138
|
52 |
|
$rowCount = $stmt->rowCount(); |
139
|
|
|
|
140
|
52 |
|
return $rowCount; |
141
|
|
|
} else { |
142
|
|
|
$messages = $stmt->errorInfo(); |
143
|
|
|
$message = $messages[2]; |
144
|
|
|
$sqlState = "(SQL STATE: ${messages[0]})"; |
145
|
|
|
$errorCode = "(ERROR CODE: ${messages[1]})"; |
146
|
|
|
throw new DatabaseException("${message} ${sqlState} ${errorCode}"); |
147
|
|
|
} |
148
|
|
|
} catch (\Exception $e) { |
149
|
|
|
throw new DatabaseException($e); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths