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