-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathMySqlDriver.php
More file actions
216 lines (169 loc) · 6.21 KB
/
MySqlDriver.php
File metadata and controls
216 lines (169 loc) · 6.21 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Database\Drivers;
use Nette;
/**
* Supplemental MySQL database driver.
*/
class MySqlDriver implements Nette\Database\ISupplementalDriver
{
use Nette\SmartObject;
public const
ERROR_ACCESS_DENIED = 1045,
ERROR_DUPLICATE_ENTRY = 1062,
ERROR_DATA_TRUNCATED = 1265;
/** @var Nette\Database\Connection */
private $connection;
/**
* Driver options:
* - charset => character encoding to set (default is utf8 or utf8mb4 since MySQL 5.5.3)
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
*/
public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
$charset = $options['charset']
?? (version_compare($connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
if ($charset) {
$connection->query('SET NAMES ?', $charset);
}
if (isset($options['sqlmode'])) {
$connection->query('SET sql_mode=?', $options['sqlmode']);
}
}
public function convertException(\PDOException $e): Nette\Database\DriverException
{
$code = $e->errorInfo[1] ?? null;
if (in_array($code, [1216, 1217, 1451, 1452, 1701], true)) {
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
} elseif (in_array($code, [1062, 1557, 1569, 1586], true)) {
return Nette\Database\UniqueConstraintViolationException::from($e);
} elseif ($code >= 2001 && $code <= 2028) {
return Nette\Database\ConnectionException::from($e);
} elseif (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], true)) {
return Nette\Database\NotNullConstraintViolationException::from($e);
} else {
return Nette\Database\DriverException::from($e);
}
}
/********************* SQL ****************d*g**/
public function delimite(string $name): string
{
// @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
return '`' . str_replace('`', '``', $name) . '`';
}
public function undelimite(string $name): string
{
$name = preg_replace('#(?<!`)`(?!`)#', '', $name);
return str_replace('``', '`', $name);
}
public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format("'Y-m-d H:i:s'");
}
public function formatDateInterval(\DateInterval $value): string
{
return $value->format("'%r%h:%I:%S'");
}
public function formatLike(string $value, int $pos): string
{
$value = str_replace('\\', '\\\\', $value);
$value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_');
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
}
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
{
if ($limit < 0 || $offset < 0) {
throw new Nette\InvalidArgumentException('Negative offset or limit.');
} elseif ($limit !== null || $offset) {
// see http://dev.mysql.com/doc/refman/5.0/en/select.html
$sql .= ' LIMIT ' . ($limit === null ? '18446744073709551615' : $limit)
. ($offset ? ' OFFSET ' . $offset : '');
}
}
/********************* reflection ****************d*g**/
public function getTables(): array
{
$tables = [];
foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
$tables[] = [
'name' => $row[0],
'view' => ($row[1] ?? null) === 'VIEW',
];
}
return $tables;
}
public function getColumns(string $table): array
{
$columns = [];
foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
$type = explode('(', $row['type']);
$columns[] = [
'name' => $row['field'],
'table' => $table,
'nativetype' => strtoupper($type[0]),
'size' => isset($type[1]) ? (int) $type[1] : null,
'nullable' => $row['null'] === 'YES',
'default' => $row['default'],
'autoincrement' => $row['extra'] === 'auto_increment',
'primary' => $row['key'] === 'PRI',
'vendor' => (array) $row,
];
}
return $columns;
}
public function getIndexes(string $table): array
{
$indexes = [];
foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
$indexes[$row['key_name']]['name'] = $row['key_name'];
$indexes[$row['key_name']]['unique'] = !$row['non_unique'];
$indexes[$row['key_name']]['primary'] = $row['key_name'] === 'PRIMARY';
$indexes[$row['key_name']]['columns'][$row['seq_in_index'] - 1] = $row['column_name'];
}
return array_values($indexes);
}
public function getForeignKeys(string $table): array
{
$keys = [];
$query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
. 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
foreach ($this->connection->query($query) as $id => $row) {
$row = array_change_key_case((array) $row, CASE_LOWER);
$keys[$id]['name'] = $row['constraint_name']; // foreign key name
$keys[$id]['local'] = $row['column_name']; // local columns
$keys[$id]['table'] = $row['referenced_table_name']; // referenced table
$keys[$id]['foreign'] = $row['referenced_column_name']; // referenced columns
}
return array_values($keys);
}
public function getColumnTypes(\PDOStatement $statement): array
{
$types = [];
$count = $statement->columnCount();
for ($col = 0; $col < $count; $col++) {
$meta = $statement->getColumnMeta($col);
if (isset($meta['native_type'])) {
$types[$meta['name']] = $type = Nette\Database\Helpers::detectType($meta['native_type']);
if ($type === Nette\Database\IStructure::FIELD_TIME) {
$types[$meta['name']] = Nette\Database\IStructure::FIELD_TIME_INTERVAL;
}
}
}
return $types;
}
public function isSupported(string $item): bool
{
// MULTI_COLUMN_AS_OR_COND due to mysql bugs:
// - http://bugs.mysql.com/bug.php?id=31188
// - http://bugs.mysql.com/bug.php?id=35819
// and more.
return $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS || $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND;
}
}