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: $type = explode('(', $row['Type']);
147: $columns[] = [
148: 'name' => $row['Field'],
149: 'table' => $table,
150: 'nativetype' => strtoupper($type[0]),
151: 'size' => isset($type[1]) ? (int) $type[1] : null,
152: 'unsigned' => (bool) strstr($row['Type'], 'unsigned'),
153: 'nullable' => $row['Null'] === 'YES',
154: 'default' => $row['Default'],
155: 'autoincrement' => $row['Extra'] === 'auto_increment',
156: 'primary' => $row['Key'] === 'PRI',
157: 'vendor' => (array) $row,
158: ];
159: }
160: return $columns;
161: }
162:
163:
164: public function getIndexes($table)
165: {
166: $indexes = [];
167: foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
168: $indexes[$row['Key_name']]['name'] = $row['Key_name'];
169: $indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
170: $indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
171: $indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
172: }
173: return array_values($indexes);
174: }
175:
176:
177: public function getForeignKeys($table)
178: {
179: $keys = [];
180: $query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
181: . 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
182:
183: foreach ($this->connection->query($query) as $id => $row) {
184: $keys[$id]['name'] = $row['CONSTRAINT_NAME'];
185: $keys[$id]['local'] = $row['COLUMN_NAME'];
186: $keys[$id]['table'] = $row['REFERENCED_TABLE_NAME'];
187: $keys[$id]['foreign'] = $row['REFERENCED_COLUMN_NAME'];
188: }
189:
190: return array_values($keys);
191: }
192:
193:
194: public function getColumnTypes(\PDOStatement $statement)
195: {
196: $types = [];
197: $count = $statement->columnCount();
198: for ($col = 0; $col < $count; $col++) {
199: $meta = $statement->getColumnMeta($col);
200: if (isset($meta['native_type'])) {
201: $types[$meta['name']] = $type = Nette\Database\Helpers::detectType($meta['native_type']);
202: if ($type === Nette\Database\IStructure::FIELD_TIME) {
203: $types[$meta['name']] = Nette\Database\IStructure::FIELD_TIME_INTERVAL;
204: }
205: }
206: }
207: return $types;
208: }
209:
210:
211: public function isSupported($item)
212: {
213:
214:
215:
216:
217: return $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS || $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND;
218: }
219: }
220: