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