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