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