1
|
|
|
<?php /** DriverMicro */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Db\Drivers; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Driver class file. |
9
|
|
|
* |
10
|
|
|
* @author Oleg Lunegov <[email protected]> |
11
|
|
|
* @link https://github.com/linpax/microphp-framework |
12
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
13
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
14
|
|
|
* @package Micro |
15
|
|
|
* @subpackage Db\Drivers |
16
|
|
|
* @version 1.0 |
17
|
|
|
* @since 1.0 |
18
|
|
|
*/ |
19
|
|
|
abstract class Driver implements IDriver |
20
|
|
|
{ |
21
|
|
|
/** @var \PDO|null $conn Connection to DB */ |
22
|
|
|
protected $conn; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Driver constructor. |
27
|
|
|
* |
28
|
|
|
* @access public |
29
|
|
|
* |
30
|
|
|
* @param string $dsn DSN connection string |
31
|
|
|
* @param array $config Configuration of connection |
32
|
|
|
* @param array $options Other options |
33
|
|
|
* |
34
|
|
|
* @result void |
35
|
|
|
* @throws Exception |
36
|
|
|
*/ |
37
|
|
|
public function __construct($dsn, array $config = [], array $options = []) |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
|
|
$this->conn = new \PDO($dsn, $config['username'], $config['password'], $options); |
41
|
|
|
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
42
|
|
|
|
43
|
|
|
} catch (\PDOException $e) { |
44
|
|
|
if (!array_key_exists('ignoreFail', $config) || !$config['ignoreFail']) { |
45
|
|
|
throw new Exception('Connect to DB failed: ' . $e->getMessage()); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get driver type of current connection |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getDriverType() |
57
|
|
|
{ |
58
|
|
|
return $this->conn->getAttribute(\PDO::ATTR_DRIVER_NAME); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Destructor for this class |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function __destruct() |
68
|
|
|
{ |
69
|
|
|
$this->conn = null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Table exists in db |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* |
77
|
|
|
* @param string $table Table name |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function tableExists($table) |
82
|
|
|
{ |
83
|
|
|
return in_array($table, $this->listTables(), false); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Remove table from database |
88
|
|
|
* |
89
|
|
|
* @access public |
90
|
|
|
* |
91
|
|
|
* @param string $name Table name |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function removeTable($name) |
96
|
|
|
{ |
97
|
|
|
return $this->conn->exec("DROP TABLE {$name};"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Clear all data from table |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
* |
105
|
|
|
* @param string $name Table name |
106
|
|
|
* |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function clearTable($name) |
110
|
|
|
{ |
111
|
|
|
return $this->conn->exec("TRUNCATE {$name};"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get info of a field |
116
|
|
|
* |
117
|
|
|
* @access public |
118
|
|
|
* |
119
|
|
|
* @param string $field Field name |
120
|
|
|
* @param string $table Table name |
121
|
|
|
* |
122
|
|
|
* @return array|boolean |
123
|
|
|
*/ |
124
|
|
|
public function fieldInfo($field, $table) |
125
|
|
|
{ |
126
|
|
|
if ($this->fieldExists($field, $table)) { |
127
|
|
|
return $this->conn->query("SELECT {$field} FROM {$table} LIMIT 1;")->getColumnMeta(0); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Field exists in table |
135
|
|
|
* |
136
|
|
|
* @access public |
137
|
|
|
* |
138
|
|
|
* @param string $field Field name |
139
|
|
|
* @param string $table Table name |
140
|
|
|
* |
141
|
|
|
* @return boolean |
142
|
|
|
*/ |
143
|
|
|
public function fieldExists($field, $table) |
144
|
|
|
{ |
145
|
|
|
foreach ($this->listFields($table) AS $tbl) { |
146
|
|
|
if ($tbl['field'] === $field) { |
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Delete row from table |
156
|
|
|
* |
157
|
|
|
* @access public |
158
|
|
|
* |
159
|
|
|
* @param string $table Table name |
160
|
|
|
* @param string $conditions Conditions to search |
161
|
|
|
* @param array $params Params array |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function delete($table, $conditions, array $params = []) |
166
|
|
|
{ |
167
|
|
|
return $this->conn->prepare("DELETE FROM {$table} WHERE {$conditions};")->execute($params); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Count element in sub-query |
172
|
|
|
* |
173
|
|
|
* @access public |
174
|
|
|
* |
175
|
|
|
* @param string $query Query |
176
|
|
|
* @param string $table Table name |
177
|
|
|
* |
178
|
|
|
* @return integer|boolean |
179
|
|
|
*/ |
180
|
|
|
public function count($query = '', $table = '') |
181
|
|
|
{ |
182
|
|
|
if ($query) { |
183
|
|
|
$sth = $this->conn->prepare("SELECT COUNT(*) FROM ({$query}) AS m;"); |
184
|
|
|
} elseif ($table) { |
185
|
|
|
$sth = $this->conn->prepare("SELECT COUNT(*) FROM {$table} AS m;"); |
186
|
|
|
} else { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
if ($sth->execute()) { |
190
|
|
|
return $sth->fetchColumn(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Send RAW query to DB |
198
|
|
|
* |
199
|
|
|
* @access public |
200
|
|
|
* |
201
|
|
|
* @param string $query Raw query to db |
202
|
|
|
* @param array $params Params for query |
203
|
|
|
* @param int $fetchType Fetching type |
204
|
|
|
* @param string $fetchClass Fetching class |
205
|
|
|
* |
206
|
|
|
* @return \PDOStatement|array |
207
|
|
|
* @throws Exception |
208
|
|
|
*/ |
209
|
|
|
public function rawQuery($query = '', array $params = [], $fetchType = \PDO::FETCH_ASSOC, $fetchClass = 'Model') |
210
|
|
|
{ |
211
|
|
|
$sth = $this->conn->prepare($query); |
212
|
|
|
|
213
|
|
|
if ($fetchType === \PDO::FETCH_CLASS) { |
214
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
215
|
|
|
$sth->setFetchMode($fetchType, $fetchClass, ['new' => false]); |
216
|
|
|
} else { |
217
|
|
|
$sth->setFetchMode($fetchType); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
foreach ($params AS $name => $value) { |
221
|
|
|
$sth->bindValue($name, $value); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$sth->execute(); |
225
|
|
|
|
226
|
|
|
return $sth->fetchAll(); |
227
|
|
|
} |
228
|
|
|
} |