1 | <?php |
||
2 | |||
3 | namespace Lagdo\DbAdmin\Driver\MySql\Db; |
||
4 | |||
5 | use Lagdo\DbAdmin\Driver\Entity\TableEntity; |
||
6 | use Lagdo\DbAdmin\Driver\Entity\RoutineEntity; |
||
7 | |||
8 | use Lagdo\DbAdmin\Driver\Db\Database as AbstractDatabase; |
||
9 | |||
10 | class Database extends AbstractDatabase |
||
11 | { |
||
12 | /** |
||
13 | * @param TableEntity $tableAttrs |
||
14 | * |
||
15 | * @return string |
||
16 | */ |
||
17 | private function _tableStatus(TableEntity $tableAttrs) |
||
18 | { |
||
19 | return 'COMMENT=' . $this->driver->quote($tableAttrs->comment) . |
||
20 | ($tableAttrs->engine ? ' ENGINE=' . $this->driver->quote($tableAttrs->engine) : '') . |
||
21 | ($tableAttrs->collation ? ' COLLATE ' . $this->driver->quote($tableAttrs->collation) : '') . |
||
22 | ($tableAttrs->autoIncrement !== 0 ? " AUTO_INCREMENT=$tableAttrs->autoIncrement" : ''); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @inheritDoc |
||
27 | */ |
||
28 | public function createTable(TableEntity $tableAttrs) |
||
29 | { |
||
30 | $clauses = []; |
||
31 | foreach ($tableAttrs->fields as $field) { |
||
32 | $clauses[] = implode($field[1]); |
||
33 | } |
||
34 | |||
35 | $clauses = array_merge($clauses, $tableAttrs->foreign); |
||
36 | $status = $this->_tableStatus($tableAttrs); |
||
37 | |||
38 | $result = $this->driver->execute('CREATE TABLE ' . $this->driver->escapeTableName($tableAttrs->name) . |
||
0 ignored issues
–
show
|
|||
39 | ' (' . implode(', ', $clauses) . ") $status $tableAttrs->partitioning"); |
||
40 | return $result !== false; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @inheritDoc |
||
45 | */ |
||
46 | public function alterTable(string $table, TableEntity $tableAttrs) |
||
47 | { |
||
48 | $clauses = []; |
||
49 | foreach ($tableAttrs->fields as $field) { |
||
50 | $clauses[] = 'ADD ' . implode($field[1]) . $field[2]; |
||
51 | } |
||
52 | foreach ($tableAttrs->edited as $field) { |
||
53 | $clauses[] = 'CHANGE ' . $this->driver->escapeId($field[0]) . ' ' . implode($field[1]) . $field[2]; |
||
54 | } |
||
55 | foreach ($tableAttrs->dropped as $column) { |
||
56 | $clauses[] = 'DROP ' . $this->driver->escapeId($column); |
||
57 | } |
||
58 | |||
59 | $clauses = array_merge($clauses, $tableAttrs->foreign); |
||
60 | if ($tableAttrs->name !== '' && $table !== $tableAttrs->name) { |
||
61 | $clauses[] = 'RENAME TO ' . $this->driver->escapeTableName($tableAttrs->name); |
||
62 | } |
||
63 | $clauses[] = $this->_tableStatus($tableAttrs); |
||
64 | |||
65 | $result = $this->driver->execute('ALTER TABLE ' . $this->driver->escapeTableName($table) . ' ' . |
||
66 | implode(', ', $clauses) . ' ' . $tableAttrs->partitioning); |
||
67 | return $result !== false; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @inheritDoc |
||
72 | */ |
||
73 | public function alterIndexes(string $table, array $alter, array $drop) |
||
74 | { |
||
75 | $clauses = []; |
||
76 | foreach ($drop as $index) { |
||
77 | $clauses[] = 'DROP INDEX ' . $this->driver->escapeId($index->name); |
||
78 | } |
||
79 | foreach ($alter as $index) { |
||
80 | $clauses[] = 'ADD ' . ($index->type == 'PRIMARY' ? 'PRIMARY KEY ' : $index->type . ' ') . |
||
81 | ($index->name != '' ? $this->driver->escapeId($index->name) . ' ' : '') . |
||
82 | '(' . implode(', ', $index->columns) . ')'; |
||
83 | } |
||
84 | $result = $this->driver->execute('ALTER TABLE ' . $this->driver->escapeTableName($table) . ' ' . implode(', ', $clauses)); |
||
85 | return $result !== false; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | public function tables() |
||
92 | { |
||
93 | return $this->driver->keyValues($this->driver->minVersion(5) ? |
||
94 | 'SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() ORDER BY TABLE_NAME' : |
||
95 | 'SHOW TABLES'); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | public function countTables(array $databases) |
||
102 | { |
||
103 | $counts = []; |
||
104 | foreach ($databases as $database) { |
||
105 | $counts[$database] = count($this->driver->values('SHOW TABLES IN ' . $this->driver->escapeId($database))); |
||
106 | } |
||
107 | return $counts; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function dropViews(array $views) |
||
114 | { |
||
115 | $this->driver->execute('DROP VIEW ' . implode(', ', array_map(function ($view) { |
||
116 | return $this->driver->escapeTableName($view); |
||
117 | }, $views))); |
||
118 | return true; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @inheritDoc |
||
123 | */ |
||
124 | public function dropTables(array $tables) |
||
125 | { |
||
126 | $this->driver->execute('DROP TABLE ' . implode(', ', array_map(function ($table) { |
||
127 | return $this->driver->escapeTableName($table); |
||
128 | }, $tables))); |
||
129 | return true; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | public function truncateTables(array $tables) |
||
136 | { |
||
137 | return $this->driver->applyQueries('TRUNCATE TABLE', $tables); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function moveTables(array $tables, array $views, string $target) |
||
144 | { |
||
145 | // The feature is not natively provided by latest MySQL versions, thus it is disabled here. |
||
146 | return false; |
||
147 | /*$rename = []; |
||
148 | foreach ($tables as $table) { |
||
149 | $rename[] = $this->driver->escapeTableName($table) . ' TO ' . $this->driver->escapeId($target) . '.' . $this->driver->escapeTableName($table); |
||
150 | } |
||
151 | if (!$rename || $this->driver->execute('RENAME TABLE ' . implode(', ', $rename))) { |
||
152 | $definitions = []; |
||
153 | foreach ($views as $table) { |
||
154 | $definitions[$this->driver->escapeTableName($table)] = $this->driver->view($table); |
||
155 | } |
||
156 | // $this->connection->open($target); |
||
157 | $database = $this->driver->escapeId($this->driver->database()); |
||
158 | foreach ($definitions as $name => $view) { |
||
159 | if (!$this->driver->execute("CREATE VIEW $name AS " . str_replace(" $database.", ' ', $view['select'])) || |
||
160 | !$this->driver->execute("DROP VIEW $database.$name")) { |
||
161 | return false; |
||
162 | } |
||
163 | } |
||
164 | return true; |
||
165 | } |
||
166 | //! move triggers |
||
167 | return false;*/ |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @inheritDoc |
||
172 | */ |
||
173 | // public function copyTables(array $tables, array $views, string $target) |
||
174 | // { |
||
175 | // $this->driver->execute("SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'"); |
||
176 | // $overwrite = $this->utils->input->getOverwrite(); |
||
177 | // foreach ($tables as $table) { |
||
178 | // $name = ($target == $this->driver->database() ? $this->driver->escapeTableName("copy_$table") : $this->driver->escapeId($target) . '.' . $this->driver->escapeTableName($table)); |
||
179 | // if (($overwrite && !$this->driver->execute("\nDROP TABLE IF EXISTS $name")) |
||
180 | // || !$this->driver->execute("CREATE TABLE $name LIKE " . $this->driver->escapeTableName($table)) |
||
181 | // || !$this->driver->execute("INSERT INTO $name SELECT * FROM " . $this->driver->escapeTableName($table)) |
||
182 | // ) { |
||
183 | // return false; |
||
184 | // } |
||
185 | // foreach ($this->driver->rows('SHOW TRIGGERS LIKE ' . $this->driver->quote(addcslashes($table, "%_\\"))) as $row) { |
||
186 | // $trigger = $row['Trigger']; |
||
187 | // if (!$this->driver->execute('CREATE TRIGGER ' . |
||
188 | // ($target == $this->driver->database() ? $this->driver->escapeId("copy_$trigger") : |
||
189 | // $this->driver->escapeId($target) . '.' . $this->driver->escapeId($trigger)) . |
||
190 | // " $row[Timing] $row[Event] ON $name FOR EACH ROW\n$row[Statement];")) { |
||
191 | // return false; |
||
192 | // } |
||
193 | // } |
||
194 | // } |
||
195 | // foreach ($views as $table) { |
||
196 | // $name = ($target == $this->driver->database() ? $this->driver->escapeTableName("copy_$table") : |
||
197 | // $this->driver->escapeId($target) . '.' . $this->driver->escapeTableName($table)); |
||
198 | // $view = $this->driver->view($table); |
||
199 | // if (($overwrite && !$this->driver->execute("DROP VIEW IF EXISTS $name")) |
||
200 | // || !$this->driver->execute("CREATE VIEW $name AS $view[select]")) { //! USE to avoid db.table |
||
201 | // return false; |
||
202 | // } |
||
203 | // } |
||
204 | // return true; |
||
205 | // } |
||
206 | |||
207 | /** |
||
208 | * @inheritDoc |
||
209 | */ |
||
210 | public function events() |
||
211 | { |
||
212 | return $this->driver->rows('SHOW EVENTS'); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @inheritDoc |
||
217 | */ |
||
218 | public function routine(string $name, string $type) |
||
219 | { |
||
220 | $enumLength = $this->driver->enumLength(); |
||
221 | $aliases = ['bool', 'boolean', 'integer', 'double precision', 'real', 'dec', |
||
222 | 'numeric', 'fixed', 'national char', 'national varchar']; |
||
223 | $space = "(?:\\s|/\\*[\s\S]*?\\*/|(?:#|-- )[^\n]*\n?|--\r?\n)"; |
||
224 | $type_pattern = '((' . implode('|', array_merge(array_keys($this->driver->types()), $aliases)) . |
||
225 | ")\\b(?:\\s*\\(((?:[^'\")]|$enumLength)++)\\))?\\s*(zerofill\\s*)?(unsigned" . |
||
226 | "(?:\\s+zerofill)?)?)(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s,]+)['\"]?)?"; |
||
227 | $pattern = "$space*(" . ($type == 'FUNCTION' ? '' : $this->driver->inout()) . |
||
228 | ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern"; |
||
229 | $create = $this->driver->result("SHOW CREATE $type " . $this->driver->escapeId($name), 2); |
||
230 | preg_match("~\\(((?:$pattern\\s*,?)*)\\)\\s*" . |
||
231 | ($type == "FUNCTION" ? "RETURNS\\s+$type_pattern\\s+" : '') . "(.*)~is", $create, $match); |
||
232 | $fields = []; |
||
233 | preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER); |
||
234 | foreach ($matches as $param) { |
||
235 | $fields[] = [ |
||
236 | 'field' => str_replace('``', '`', $param[2]) . $param[3], |
||
237 | 'type' => strtolower($param[5]), |
||
238 | 'length' => preg_replace_callback("~$enumLength~s", 'normalize_enum', $param[6]), |
||
239 | 'unsigned' => strtolower(preg_replace('~\s+~', ' ', trim("$param[8] $param[7]"))), |
||
240 | 'null' => 1, |
||
241 | 'full_type' => $param[4], |
||
242 | 'inout' => strtoupper($param[1]), |
||
243 | 'collation' => strtolower($param[9]), |
||
244 | ]; |
||
245 | } |
||
246 | if ($type != 'FUNCTION') { |
||
247 | return ['fields' => $fields, 'definition' => $match[11]]; |
||
248 | } |
||
249 | return [ |
||
250 | 'fields' => $fields, |
||
251 | 'returns' => ['type' => $match[12], 'length' => $match[13], 'unsigned' => $match[15], 'collation' => $match[16]], |
||
252 | 'definition' => $match[17], |
||
253 | 'language' => 'SQL', // available in information_schema.ROUTINES.PARAMETER_STYLE |
||
254 | ]; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @inheritDoc |
||
259 | */ |
||
260 | public function routines() |
||
261 | { |
||
262 | $rows = $this->driver->rows('SELECT ROUTINE_NAME, ROUTINE_TYPE, DTD_IDENTIFIER ' . |
||
263 | 'FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA = ' . |
||
264 | $this->driver->quote($this->driver->database())); |
||
265 | return array_map(function($row) { |
||
266 | return new RoutineEntity($row['ROUTINE_NAME'], $row['ROUTINE_NAME'], |
||
267 | $row['ROUTINE_TYPE'], $row['DTD_IDENTIFIER']); |
||
268 | }, $rows); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @inheritDoc |
||
273 | */ |
||
274 | public function routineId(string $name, array $row) |
||
275 | { |
||
276 | return $this->driver->escapeId($name); |
||
277 | } |
||
278 | } |
||
279 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.