|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebServCo\Framework\Libraries; |
|
3
|
|
|
|
|
4
|
|
|
final class PdoDatabase extends \WebServCo\Framework\AbstractDatabase implements |
|
5
|
|
|
\WebServCo\Framework\Interfaces\DatabaseInterface |
|
6
|
|
|
{ |
|
7
|
|
|
use \WebServCo\Framework\Traits\DatabaseTrait; |
|
8
|
|
|
use \WebServCo\Framework\Traits\MysqlDatabaseTrait; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct($config) |
|
11
|
|
|
{ |
|
12
|
|
|
parent::__construct($config); |
|
13
|
|
|
|
|
14
|
|
|
try { |
|
15
|
|
|
$this->db = new \PDO( |
|
16
|
|
|
$this->setting('driver', 'mysql') . |
|
|
|
|
|
|
17
|
|
|
':host=' . $this->setting('connection/host', '127.0.0.1') . |
|
|
|
|
|
|
18
|
|
|
';dbname=' . $this->setting('connection/dbname', 'test') . |
|
|
|
|
|
|
19
|
|
|
';port=' . $this->setting('connection/port', 3306) . |
|
|
|
|
|
|
20
|
|
|
';charset=utf8mb4', |
|
21
|
|
|
$this->setting('connection/username', 'root'), |
|
|
|
|
|
|
22
|
|
|
$this->setting('connection/passwd', ''), |
|
|
|
|
|
|
23
|
|
|
[ |
|
24
|
|
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, |
|
25
|
|
|
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, |
|
26
|
|
|
\PDO::ATTR_EMULATE_PREPARES => false, |
|
27
|
|
|
\PDO::ATTR_PERSISTENT => false |
|
28
|
|
|
] |
|
29
|
|
|
); |
|
30
|
|
|
} catch (\Exception $e) { |
|
31
|
|
|
throw new \ErrorException($e->getMessage()); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function escape($string) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->db->quote($string); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function query($query, $params = []) |
|
41
|
|
|
{ |
|
42
|
|
|
if (empty($query)) { |
|
43
|
|
|
throw new \ErrorException('No query specified'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (!empty($params)) { |
|
47
|
|
|
$this->stmt = $this->db->prepare($query); |
|
48
|
|
|
$this->bindParams($params); |
|
49
|
|
|
$this->stmt->execute(); |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->stmt = $this->db->query($query); |
|
52
|
|
|
} |
|
53
|
|
|
$this->setLastInsertId(); |
|
54
|
|
|
return $this->stmt; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function transaction($queries) |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
|
|
$this->db->beginTransaction(); |
|
|
|
|
|
|
61
|
|
|
foreach ($queries as $item) { |
|
62
|
|
|
if (!isset($item[0])) { |
|
63
|
|
|
throw new \ErrorException('No query specified'); |
|
64
|
|
|
} |
|
65
|
|
|
$params = isset($item[1]) ? $item[1] : []; |
|
66
|
|
|
$this->query($item[0], $params); |
|
67
|
|
|
} |
|
68
|
|
|
$this->db->commit(); |
|
69
|
|
|
return true; |
|
70
|
|
|
} catch (\Exception $e) { |
|
71
|
|
|
$this->db->rollBack(); |
|
72
|
|
|
throw new \ErrorException($e->getMessage()); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function numRows() |
|
77
|
|
|
{ |
|
78
|
|
|
if (!is_object($this->stmt)) { |
|
79
|
|
|
throw new \ErrorException('No Statement object available.'); |
|
80
|
|
|
} |
|
81
|
|
|
if ('mysql' == $this->setting('driver')) { |
|
82
|
|
|
return $this->stmt->rowCount(); |
|
83
|
|
|
} |
|
84
|
|
|
$rows = $this->rows ?: $this->stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
85
|
|
|
return count($rows); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function affectedRows() |
|
89
|
|
|
{ |
|
90
|
|
|
if (!is_object($this->stmt)) { |
|
91
|
|
|
throw new \ErrorException('No Statement object available.'); |
|
92
|
|
|
} |
|
93
|
|
|
return $this->stmt->rowCount(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getRows($query, $params = []) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->query($query, $params); |
|
99
|
|
|
$this->rows = $this->stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
100
|
|
|
return $this->rows; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getRow($query, $params = []) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->query($query, $params); |
|
106
|
|
|
return $this->stmt->fetch(\PDO::FETCH_ASSOC); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getColumn($query, $params = [], $columnNumber = 0) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->query($query, $params); |
|
112
|
|
|
return $this->stmt->fetchColumn($columnNumber); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function bindParams($data) |
|
116
|
|
|
{ |
|
117
|
|
|
if (empty($data)) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$i = 1; |
|
122
|
|
|
foreach ($data as $item) { |
|
123
|
|
|
if (is_array($item)) { |
|
124
|
|
|
foreach ($item as $v) { |
|
125
|
|
|
$this->stmt->bindValue($i, $v, $this->getDataType($v)); |
|
126
|
|
|
$i++; |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
|
|
$this->stmt->bindValue($i, $item, $this->getDataType($item)); |
|
130
|
|
|
$i++; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
return true; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
protected function getDataType($variable) |
|
137
|
|
|
{ |
|
138
|
|
|
$type = gettype($variable); |
|
139
|
|
|
|
|
140
|
|
|
switch ($type) { |
|
141
|
|
|
case 'NULL': |
|
142
|
|
|
return PDO::PARAM_NULL; |
|
143
|
|
|
break; |
|
|
|
|
|
|
144
|
|
|
case 'boolean': |
|
145
|
|
|
return \PDO::PARAM_BOOL; |
|
146
|
|
|
break; |
|
|
|
|
|
|
147
|
|
|
case 'integer': |
|
148
|
|
|
return \PDO::PARAM_INT; |
|
149
|
|
|
break; |
|
|
|
|
|
|
150
|
|
|
case 'string': |
|
151
|
|
|
case 'double': |
|
152
|
|
|
case 'array': |
|
153
|
|
|
case 'object': |
|
154
|
|
|
case 'resource': |
|
155
|
|
|
case 'resource (closed)': |
|
156
|
|
|
case 'unknown type': |
|
157
|
|
|
return \PDO::PARAM_STR; |
|
158
|
|
|
break; |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function setLastInsertId() |
|
163
|
|
|
{ |
|
164
|
|
|
$this->lastInsertId = $this->db->lastInsertId(); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: