1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Database\Drivers;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class MySqlDriver extends Nette\Object implements Nette\Database\ISupplementalDriver
24: {
25: const ERROR_ACCESS_DENIED = 1045;
26: const ERROR_DUPLICATE_ENTRY = 1062;
27: const ERROR_DATA_TRUNCATED = 1265;
28:
29:
30: private $connection;
31:
32:
33:
34: 35: 36: 37: 38:
39: public function __construct(Nette\Database\Connection $connection, array $options)
40: {
41: $this->connection = $connection;
42: $charset = isset($options['charset']) ? $options['charset'] : 'utf8';
43: if ($charset) {
44: $connection->exec("SET NAMES '$charset'");
45: }
46: if (isset($options['sqlmode'])) {
47: $connection->exec("SET sql_mode='$options[sqlmode]'");
48: }
49: $connection->exec("SET time_zone='" . date('P') . "'");
50: }
51:
52:
53:
54:
55:
56:
57:
58: 59: 60:
61: public function delimite($name)
62: {
63:
64: return '`' . str_replace('`', '``', $name) . '`';
65: }
66:
67:
68:
69: 70: 71:
72: public function formatDateTime(\DateTime $value)
73: {
74: return $value->format("'Y-m-d H:i:s'");
75: }
76:
77:
78:
79: 80: 81:
82: public function formatLike($value, $pos)
83: {
84: $value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
85: return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
86: }
87:
88:
89:
90: 91: 92:
93: public function applyLimit(&$sql, $limit, $offset)
94: {
95: if ($limit >= 0 || $offset > 0) {
96:
97: $sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
98: . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
99: }
100: }
101:
102:
103:
104: 105: 106:
107: public function normalizeRow($row, $statement)
108: {
109: return $row;
110: }
111:
112:
113:
114:
115:
116:
117:
118: 119: 120:
121: public function getTables()
122: {
123: 124: 125: 126: 127:
128: $tables = array();
129: foreach ($this->connection->query('SHOW FULL TABLES') as $row) {
130: $tables[] = array(
131: 'name' => $row[0],
132: 'view' => isset($row[1]) && $row[1] === 'VIEW',
133: );
134: }
135: return $tables;
136: }
137:
138:
139:
140: 141: 142:
143: public function getColumns($table)
144: {
145: 146: 147: 148: 149:
150: $columns = array();
151: foreach ($this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table)) as $row) {
152: $type = explode('(', $row['Type']);
153: $columns[] = array(
154: 'name' => $row['Field'],
155: 'table' => $table,
156: 'nativetype' => strtoupper($type[0]),
157: 'size' => isset($type[1]) ? (int) $type[1] : NULL,
158: 'unsigned' => (bool) strstr($row['Type'], 'unsigned'),
159: 'nullable' => $row['Null'] === 'YES',
160: 'default' => $row['Default'],
161: 'autoincrement' => $row['Extra'] === 'auto_increment',
162: 'primary' => $row['Key'] === 'PRI',
163: 'vendor' => (array) $row,
164: );
165: }
166: return $columns;
167: }
168:
169:
170:
171: 172: 173:
174: public function getIndexes($table)
175: {
176: 177: 178: 179: 180: 181:
182: $indexes = array();
183: foreach ($this->connection->query('SHOW INDEX FROM ' . $this->delimite($table)) as $row) {
184: $indexes[$row['Key_name']]['name'] = $row['Key_name'];
185: $indexes[$row['Key_name']]['unique'] = !$row['Non_unique'];
186: $indexes[$row['Key_name']]['primary'] = $row['Key_name'] === 'PRIMARY';
187: $indexes[$row['Key_name']]['columns'][$row['Seq_in_index'] - 1] = $row['Column_name'];
188: }
189: return array_values($indexes);
190: }
191:
192:
193:
194: 195: 196:
197: public function getForeignKeys($table)
198: {
199: $keys = array();
200: $query = 'SELECT CONSTRAINT_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE '
201: . 'WHERE TABLE_SCHEMA = DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_NAME = ' . $this->connection->quote($table);
202:
203: foreach ($this->connection->query($query) as $id => $row) {
204: $keys[$id]['name'] = $row['CONSTRAINT_NAME'];
205: $keys[$id]['local'] = $row['COLUMN_NAME'];
206: $keys[$id]['table'] = $row['REFERENCED_TABLE_NAME'];
207: $keys[$id]['foreign'] = $row['REFERENCED_COLUMN_NAME'];
208: }
209:
210: return array_values($keys);
211: }
212:
213:
214:
215: 216: 217:
218: public function isSupported($item)
219: {
220: return $item === self::META;
221: }
222:
223: }
224: