1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Database\Drivers;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class MySqlDriver implements Nette\Database\ISupplementalDriver
17: {
18: use Nette\SmartObject;
19:
20: const ERROR_ACCESS_DENIED = 1045;
21:
22: const ERROR_DUPLICATE_ENTRY = 1062;
23:
24: const ERROR_DATA_TRUNCATED = 1265;
25:
26:
27: private $connection;
28:
29:
30: 31: 32: 33: 34:
35: public function __construct(Nette\Database\Connection $connection, array $options)
36: {
37: $this->connection = $connection;
38: $charset = isset($options['charset'])
39: ? $options['charset']
40: : (version_compare($connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
41: if ($charset) {
42: $connection->query("SET NAMES '$charset'");
43: }
44: if (isset($options['sqlmode'])) {
45: $connection->query("SET sql_mode='$options[sqlmode]'");
46: }
47: }
48:
49:
50: public function convertException(\PDOException $e)
51: {
52: $code = isset($e->errorInfo[1]) ? $e->errorInfo[1] : null;
53: if (in_array($code, [1216, 1217, 1451, 1452, 1701], true)) {
54: return Nette\Database\ForeignKeyConstraintViolationException::from($e);
55:
56: } elseif (in_array($code, [1062, 1557, 1569, 1586], true)) {
57: return Nette\Database\UniqueConstraintViolationException::from($e);
58:
59: } elseif ($code >= 2001 && $code <= 2028) {
60: return Nette\Database\ConnectionException::from($e);
61:
62: } elseif (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], true)) {
63: return Nette\Database\NotNullConstraintViolationException::from($e);
64:
65: } else {
66: return Nette\Database\DriverException::from($e);
67: }
68: }
69:
70:
71:
72:
73:
74: public function delimite($name)
75: {
76:
77: return '`' . str_replace('`', '``', $name) . '`';
78: }
79:
80:
81: public function formatBool($value)
82: {
83: return $value ? '1' : '0';
84: }
85:
86:
87: public function formatDateTime( $value)
88: {
89: return $value->format("'Y-m-d H:i:s'");
90: }
91:
92:
93: public function formatDateInterval(\DateInterval $value)
94: {
95: return $value->format("'%r%h:%I:%S'");
96: }
97:
98:
99: public function formatLike($value, $pos)
100: {
101: $value = str_replace('\\', '\\\\', $value);
102: $value = addcslashes(substr($this->connection->quote($value), 1, -1), '%_');
103: return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
104: }
105:
106:
107: public function applyLimit(&$sql, $limit, $offset)
108: {
109: if ($limit < 0 || $offset < 0) {
110: throw new Nette\InvalidArgumentException('Negative offset or limit.');
111:
112: } elseif ($limit !== null || $offset) {
113:
114: $sql .= ' LIMIT ' . ($limit === null ? '18446744073709551615' : (int) $limit)
115: . ($offset ? ' OFFSET ' . (int) $offset : '');
116: }
117: }
118:
119:
120: public function normalizeRow($row)
121: {
122: return $row;
123: }
124:
125:
126:
127:
128:
129: public function getTables()
130: {
131: $tables = [];
132: foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
133: $tables[] = [
134: 'name' => $row[0],
135: 'view' => isset($row[1]) && $row[1] === 'VIEW',
136: ];
137: }
138: return $tables;
139: }
140:
141:
142: public function getColumns($table)
143: {
144: $columns = [];
145: foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
146: $row = array_change_key_case((array) $row, CASE_LOWER);
147: $type = explode('(', $row['type']);
148: $columns[] = [
149: 'name' => $row['field'],
150: 'table' => $table,
151: 'nativetype' => strtoupper($type[0]),
152: 'size' => isset($type[1]) ? (int) $type[1] : null,
153: 'unsigned' => (bool) strstr($row['type'], 'unsigned'),
154: 'nullable' => $row['null'] === 'YES',
155: 'default' => $row['default'],
156: 'autoincrement' => $row['extra'] === 'auto_increment',
157: 'primary' => $row['key'] === 'PRI',
158: 'vendor' => (array) $row,
159: ];
160: }
161: return $columns;
162: }
163:
164:
165: public function getIndexes($table)
166: {
167: $indexes = [];
168: foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
169: $row = array_change_key_case((array) $row, CASE_LOWER);
170: $indexes[$row['key_name']]['name'] = $row['key_name'];
171: $indexes[$row['key_name']]['unique'] = !$row['non_unique'];
172: $indexes[$row['key_name']]['primary'] = $row['key_name'] === 'PRIMARY';
173: $indexes[$row['key_name']]['columns'][$row['seq_in_index'] - 1] = $row['column_name'];
174: }
175: return array_values($indexes);
176: }
177:
178:
179: public function getForeignKeys($table)
180: {
181: $keys = [];
182: $query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
183: . 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
184:
185: foreach ($this->connection->query($query) as $id => $row) {
186: $row = array_change_key_case((array) $row, CASE_LOWER);
187: $keys[$id]['name'] = $row['constraint_name'];
188: $keys[$id]['local'] = $row['column_name'];
189: $keys[$id]['table'] = $row['referenced_table_name'];
190: $keys[$id]['foreign'] = $row['referenced_column_name'];
191: }
192:
193: return array_values($keys);
194: }
195:
196:
197: public function getColumnTypes(\PDOStatement $statement)
198: {
199: $types = [];
200: $count = $statement->columnCount();
201: for ($col = 0; $col < $count; $col++) {
202: $meta = $statement->getColumnMeta($col);
203: if (isset($meta['native_type'])) {
204: $types[$meta['name']] = $type = Nette\Database\Helpers::detectType($meta['native_type']);
205: if ($type === Nette\Database\IStructure::FIELD_TIME) {
206: $types[$meta['name']] = Nette\Database\IStructure::FIELD_TIME_INTERVAL;
207: }
208: }
209: }
210: return $types;
211: }
212:
213:
214: public function isSupported($item)
215: {
216:
217:
218:
219:
220: return $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS || $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND;
221: }
222: }
223: