Total Complexity | 152 |
Total Lines | 1101 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Mysqldump often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mysqldump, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Mysqldump |
||
35 | { |
||
36 | |||
37 | // Same as mysqldump |
||
38 | const MAXLINESIZE = 1000000; |
||
39 | |||
40 | // Available compression methods as constants |
||
41 | const GZIP = 'Gzip'; |
||
42 | const BZIP2 = 'Bzip2'; |
||
43 | const NONE = 'None'; |
||
44 | |||
45 | // Available connection strings |
||
46 | const UTF8 = 'utf8'; |
||
47 | const UTF8MB4 = 'utf8mb4'; |
||
48 | |||
49 | /** |
||
50 | * Database username |
||
51 | * @var string |
||
52 | */ |
||
53 | public $user; |
||
54 | /** |
||
55 | * Database password |
||
56 | * @var string |
||
57 | */ |
||
58 | public $pass; |
||
59 | /** |
||
60 | * Connection string for PDO |
||
61 | * @var string |
||
62 | */ |
||
63 | public $dsn; |
||
64 | /** |
||
65 | * Destination filename, defaults to stdout |
||
66 | * @var string |
||
67 | */ |
||
68 | public $fileName = 'php://output'; |
||
69 | |||
70 | // Internal stuff |
||
71 | private $tables = array(); |
||
72 | private $views = array(); |
||
73 | private $triggers = array(); |
||
74 | private $procedures = array(); |
||
75 | private $events = array(); |
||
76 | private $dbHandler = null; |
||
77 | private $dbType = ""; |
||
78 | private $compressManager; |
||
79 | private $typeAdapter; |
||
80 | private $dumpSettings = array(); |
||
81 | private $pdoSettings = array(); |
||
82 | private $version; |
||
83 | private $tableColumnTypes = array(); |
||
84 | /** |
||
85 | * database name, parsed from dsn |
||
86 | * @var string |
||
87 | */ |
||
88 | private $dbName; |
||
89 | /** |
||
90 | * host name, parsed from dsn |
||
91 | * @var string |
||
92 | */ |
||
93 | private $host; |
||
94 | /** |
||
95 | * dsn string parsed as an array |
||
96 | * @var array |
||
97 | */ |
||
98 | private $dsnArray = array(); |
||
99 | |||
100 | /** |
||
101 | * Constructor of Mysqldump. Note that in the case of an SQLite database |
||
102 | * connection, the filename must be in the $db parameter. |
||
103 | * |
||
104 | * @param string $dsn PDO DSN connection string |
||
105 | * @param string $user SQL account username |
||
106 | * @param string $pass SQL account password |
||
107 | * @param array $dumpSettings SQL database settings |
||
108 | * @param array $pdoSettings PDO configured attributes |
||
109 | */ |
||
110 | public function __construct( |
||
111 | $dsn = '', |
||
112 | $user = '', |
||
113 | $pass = '', |
||
114 | $dumpSettings = array(), |
||
115 | $pdoSettings = array() |
||
116 | ) { |
||
117 | $dumpSettingsDefault = array( |
||
118 | 'include-tables' => array(), |
||
119 | 'exclude-tables' => array(), |
||
120 | 'compress' => Mysqldump::NONE, |
||
121 | 'init_commands' => array(), |
||
122 | 'no-data' => array(), |
||
123 | 'reset-auto-increment' => false, |
||
124 | 'add-drop-database' => false, |
||
125 | 'add-drop-table' => false, |
||
126 | 'add-drop-trigger' => true, |
||
127 | 'add-locks' => true, |
||
128 | 'complete-insert' => false, |
||
129 | 'databases' => false, |
||
130 | 'default-character-set' => Mysqldump::UTF8, |
||
131 | 'disable-keys' => true, |
||
132 | 'extended-insert' => true, |
||
133 | 'events' => false, |
||
134 | 'hex-blob' => true, /* faster than escaped content */ |
||
135 | 'insert-ignore' => false, |
||
136 | 'net_buffer_length' => self::MAXLINESIZE, |
||
137 | 'no-autocommit' => true, |
||
138 | 'no-create-info' => false, |
||
139 | 'lock-tables' => true, |
||
140 | 'routines' => false, |
||
141 | 'single-transaction' => true, |
||
142 | 'skip-triggers' => false, |
||
143 | 'skip-tz-utc' => false, |
||
144 | 'skip-comments' => false, |
||
145 | 'skip-dump-date' => false, |
||
146 | 'skip-definer' => false, |
||
147 | 'where' => '', |
||
148 | /* deprecated */ |
||
149 | 'disable-foreign-keys-check' => true |
||
150 | ); |
||
151 | |||
152 | $pdoSettingsDefault = array( |
||
153 | PDO::ATTR_PERSISTENT => true, |
||
154 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
155 | ); |
||
156 | |||
157 | $this->user = $user; |
||
158 | $this->pass = $pass; |
||
159 | $this->parseDsn($dsn); |
||
160 | |||
161 | // this drops MYSQL dependency, only use the constant if it's defined |
||
162 | if ("mysql" === $this->dbType) { |
||
|
|||
163 | $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
||
164 | } |
||
165 | |||
166 | $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
||
167 | $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
||
168 | $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
||
169 | |||
170 | if (false === $this->dumpSettings['skip-tz-utc']) { |
||
171 | $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
||
172 | } |
||
173 | |||
174 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
||
175 | if (count($diff) > 0) { |
||
176 | throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
||
177 | } |
||
178 | |||
179 | if (!is_array($this->dumpSettings['include-tables']) || |
||
180 | !is_array($this->dumpSettings['exclude-tables'])) { |
||
181 | throw new Exception("Include-tables and exclude-tables should be arrays"); |
||
182 | } |
||
183 | |||
184 | // Dump the same views as tables, mimic mysqldump behaviour |
||
185 | $this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
||
186 | |||
187 | // Create a new compressManager to manage compressed output |
||
188 | $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Destructor of Mysqldump. Unsets dbHandlers and database objects. |
||
193 | * |
||
194 | */ |
||
195 | public function __destruct() |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Custom array_replace_recursive to be used if PHP < 5.3 |
||
202 | * Replaces elements from passed arrays into the first array recursively |
||
203 | * |
||
204 | * @param array $array1 The array in which elements are replaced |
||
205 | * @param array $array2 The array from which elements will be extracted |
||
206 | * |
||
207 | * @return array Returns an array, or NULL if an error occurs. |
||
208 | */ |
||
209 | public static function array_replace_recursive($array1, $array2) |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Parse DSN string and extract dbname value |
||
227 | * Several examples of a DSN string |
||
228 | * mysql:host=localhost;dbname=testdb |
||
229 | * mysql:host=localhost;port=3307;dbname=testdb |
||
230 | * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
231 | * |
||
232 | * @param string $dsn dsn string to parse |
||
233 | */ |
||
234 | private function parseDsn($dsn) |
||
235 | { |
||
236 | if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) { |
||
237 | throw new Exception("Empty DSN string"); |
||
238 | } |
||
239 | |||
240 | $this->dsn = $dsn; |
||
241 | $this->dbType = strtolower(substr($dsn, 0, $pos)); // always returns a string |
||
242 | |||
243 | if (empty($this->dbType)) { |
||
244 | throw new Exception("Missing database type from DSN string"); |
||
245 | } |
||
246 | |||
247 | $dsn = substr($dsn, $pos + 1); |
||
248 | |||
249 | foreach (explode(";", $dsn) as $kvp) { |
||
250 | $kvpArr = explode("=", $kvp); |
||
251 | $this->dsnArray[strtolower($kvpArr[0])] = $kvpArr[1]; |
||
252 | } |
||
253 | |||
254 | if (empty($this->dsnArray['host']) && |
||
255 | empty($this->dsnArray['unix_socket'])) { |
||
256 | throw new Exception("Missing host from DSN string"); |
||
257 | } |
||
258 | $this->host = (!empty($this->dsnArray['host'])) ? |
||
259 | $this->dsnArray['host'] : $this->dsnArray['unix_socket']; |
||
260 | |||
261 | if (empty($this->dsnArray['dbname'])) { |
||
262 | throw new Exception("Missing database name from DSN string"); |
||
263 | } |
||
264 | |||
265 | $this->dbName = $this->dsnArray['dbname']; |
||
266 | |||
267 | return true; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Connect with PDO |
||
272 | * |
||
273 | * @return null |
||
274 | */ |
||
275 | private function connect() |
||
276 | { |
||
277 | // Connecting with PDO |
||
278 | try { |
||
279 | switch ($this->dbType) { |
||
280 | case 'sqlite': |
||
281 | $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
||
282 | break; |
||
283 | case 'mysql': |
||
284 | case 'pgsql': |
||
285 | case 'dblib': |
||
286 | $this->dbHandler = @new PDO( |
||
287 | $this->dsn, |
||
288 | $this->user, |
||
289 | $this->pass, |
||
290 | $this->pdoSettings |
||
291 | ); |
||
292 | // Execute init commands once connected |
||
293 | foreach ($this->dumpSettings['init_commands'] as $stmt) { |
||
294 | $this->dbHandler->exec($stmt); |
||
295 | } |
||
296 | // Store server version |
||
297 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
298 | break; |
||
299 | default: |
||
300 | throw new Exception("Unsupported database type (".$this->dbType.")"); |
||
301 | } |
||
302 | } catch (PDOException $e) { |
||
303 | throw new Exception( |
||
304 | "Connection to ".$this->dbType." failed with message: ". |
||
305 | $e->getMessage() |
||
306 | ); |
||
307 | } |
||
308 | |||
309 | if (is_null($this->dbHandler)) { |
||
310 | throw new Exception("Connection to ".$this->dbType."failed"); |
||
311 | } |
||
312 | |||
313 | $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
314 | $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Main call |
||
319 | * |
||
320 | * @param string $filename Name of file to write sql dump to |
||
321 | * @return null |
||
322 | */ |
||
323 | public function start($filename = '') |
||
324 | { |
||
325 | // Output file can be redefined here |
||
326 | if (!empty($filename)) { |
||
327 | $this->fileName = $filename; |
||
328 | } |
||
329 | |||
330 | // Connect to database |
||
331 | $this->connect(); |
||
332 | |||
333 | // Create output file |
||
334 | $this->compressManager->open($this->fileName); |
||
335 | |||
336 | // Write some basic info to output file |
||
337 | $this->compressManager->write($this->getDumpFileHeader()); |
||
338 | |||
339 | // Store server settings and use sanner defaults to dump |
||
340 | $this->compressManager->write( |
||
341 | $this->typeAdapter->backup_parameters() |
||
342 | ); |
||
343 | |||
344 | if ($this->dumpSettings['databases']) { |
||
345 | $this->compressManager->write( |
||
346 | $this->typeAdapter->getDatabaseHeader($this->dbName) |
||
347 | ); |
||
348 | if ($this->dumpSettings['add-drop-database']) { |
||
349 | $this->compressManager->write( |
||
350 | $this->typeAdapter->add_drop_database($this->dbName) |
||
351 | ); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | // Get table, view, trigger, procedures and events |
||
356 | // structures from database |
||
357 | $this->getDatabaseStructureTables(); |
||
358 | $this->getDatabaseStructureViews(); |
||
359 | $this->getDatabaseStructureTriggers(); |
||
360 | $this->getDatabaseStructureProcedures(); |
||
361 | $this->getDatabaseStructureEvents(); |
||
362 | |||
363 | if ($this->dumpSettings['databases']) { |
||
364 | $this->compressManager->write( |
||
365 | $this->typeAdapter->databases($this->dbName) |
||
366 | ); |
||
367 | } |
||
368 | |||
369 | // If there still are some tables/views in include-tables array, |
||
370 | // that means that some tables or views weren't found. |
||
371 | // Give proper error and exit. |
||
372 | // This check will be removed once include-tables supports regexps |
||
373 | if (0 < count($this->dumpSettings['include-tables'])) { |
||
374 | $name = implode(",", $this->dumpSettings['include-tables']); |
||
375 | throw new Exception("Table (".$name.") not found in database"); |
||
376 | } |
||
377 | |||
378 | $this->exportTables(); |
||
379 | $this->exportTriggers(); |
||
380 | $this->exportViews(); |
||
381 | $this->exportProcedures(); |
||
382 | $this->exportEvents(); |
||
383 | |||
384 | // Restore saved parameters |
||
385 | $this->compressManager->write( |
||
386 | $this->typeAdapter->restore_parameters() |
||
387 | ); |
||
388 | // Write some stats to output file |
||
389 | $this->compressManager->write($this->getDumpFileFooter()); |
||
390 | // Close output file |
||
391 | $this->compressManager->close(); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Returns header for dump file |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | private function getDumpFileHeader() |
||
400 | { |
||
401 | $header = ''; |
||
402 | if (!$this->dumpSettings['skip-comments']) { |
||
403 | // Some info about software, source and time |
||
404 | $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
||
405 | "--".PHP_EOL. |
||
406 | "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
||
407 | "-- ------------------------------------------------------".PHP_EOL; |
||
408 | |||
409 | if (!empty($this->version)) { |
||
410 | $header .= "-- Server version \t".$this->version.PHP_EOL; |
||
411 | } |
||
412 | |||
413 | if (!$this->dumpSettings['skip-dump-date']) { |
||
414 | $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
||
415 | } |
||
416 | } |
||
417 | return $header; |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Returns footer for dump file |
||
422 | * |
||
423 | * @return string |
||
424 | */ |
||
425 | private function getDumpFileFooter() |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Reads table names from database. |
||
441 | * Fills $this->tables array so they will be dumped later. |
||
442 | * |
||
443 | * @return null |
||
444 | */ |
||
445 | function getDatabaseStructureTables() |
||
446 | { |
||
447 | // Listing all tables from database |
||
448 | if (empty($this->dumpSettings['include-tables'])) { |
||
449 | // include all tables for now, blacklisting happens later |
||
450 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
451 | array_push($this->tables, current($row)); |
||
452 | } |
||
453 | } else { |
||
454 | // include only the tables mentioned in include-tables |
||
455 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
456 | if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
||
457 | array_push($this->tables, current($row)); |
||
458 | $elem = array_search( |
||
459 | current($row), |
||
460 | $this->dumpSettings['include-tables'] |
||
461 | ); |
||
462 | unset($this->dumpSettings['include-tables'][$elem]); |
||
463 | } |
||
464 | } |
||
465 | } |
||
466 | return; |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Reads view names from database. |
||
471 | * Fills $this->tables array so they will be dumped later. |
||
472 | * |
||
473 | * @return null |
||
474 | */ |
||
475 | function getDatabaseStructureViews() |
||
476 | { |
||
477 | // Listing all views from database |
||
478 | if (empty($this->dumpSettings['include-views'])) { |
||
479 | // include all views for now, blacklisting happens later |
||
480 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
481 | array_push($this->views, current($row)); |
||
482 | } |
||
483 | } else { |
||
484 | // include only the tables mentioned in include-tables |
||
485 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
486 | if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
||
487 | array_push($this->views, current($row)); |
||
488 | $elem = array_search( |
||
489 | current($row), |
||
490 | $this->dumpSettings['include-views'] |
||
491 | ); |
||
492 | unset($this->dumpSettings['include-views'][$elem]); |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 | return; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Reads trigger names from database. |
||
501 | * Fills $this->tables array so they will be dumped later. |
||
502 | * |
||
503 | * @return null |
||
504 | */ |
||
505 | |||
506 | function getDatabaseStructureTriggers() |
||
507 | { |
||
508 | // Listing all triggers from database |
||
509 | if (false === $this->dumpSettings['skip-triggers']) { |
||
510 | foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { |
||
511 | array_push($this->triggers, $row['Trigger']); |
||
512 | } |
||
513 | } |
||
514 | return; |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Reads procedure names from database. |
||
519 | * Fills $this->tables array so they will be dumped later. |
||
520 | * |
||
521 | * @return null |
||
522 | */ |
||
523 | function getDatabaseStructureProcedures() |
||
524 | { |
||
525 | // Listing all procedures from database |
||
526 | if ($this->dumpSettings['routines']) { |
||
527 | foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { |
||
528 | array_push($this->procedures, $row['procedure_name']); |
||
529 | } |
||
530 | } |
||
531 | return; |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * Reads event names from database. |
||
536 | * Fills $this->tables array so they will be dumped later. |
||
537 | * |
||
538 | * @return null |
||
539 | */ |
||
540 | function getDatabaseStructureEvents() |
||
541 | { |
||
542 | // Listing all events from database |
||
543 | if ($this->dumpSettings['events']) { |
||
544 | foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { |
||
545 | array_push($this->events, $row['event_name']); |
||
546 | } |
||
547 | } |
||
548 | return; |
||
549 | } |
||
550 | |||
551 | /** |
||
552 | * Compare if $table name matches with a definition inside $arr |
||
553 | * @param $table string |
||
554 | * @param $arr array with strings or patterns |
||
555 | * @return bool |
||
556 | */ |
||
557 | private function matches($table, $arr) { |
||
558 | $match = false; |
||
559 | |||
560 | foreach ($arr as $pattern) { |
||
561 | if ('/' != $pattern[0]) { |
||
562 | continue; |
||
563 | } |
||
564 | if (1 == preg_match($pattern, $table)) { |
||
565 | $match = true; |
||
566 | } |
||
567 | } |
||
568 | |||
569 | return in_array($table, $arr) || $match; |
||
570 | } |
||
571 | |||
572 | /** |
||
573 | * Exports all the tables selected from database |
||
574 | * |
||
575 | * @return null |
||
576 | */ |
||
577 | private function exportTables() |
||
578 | { |
||
579 | // Exporting tables one by one |
||
580 | foreach ($this->tables as $table) { |
||
581 | if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { |
||
582 | continue; |
||
583 | } |
||
584 | $this->getTableStructure($table); |
||
585 | if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger |
||
586 | $this->listValues($table); |
||
587 | } else if (true === $this->dumpSettings['no-data'] |
||
588 | || $this->matches($table, $this->dumpSettings['no-data'])) { |
||
589 | continue; |
||
590 | } else { |
||
591 | $this->listValues($table); |
||
592 | } |
||
593 | } |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * Exports all the views found in database |
||
598 | * |
||
599 | * @return null |
||
600 | */ |
||
601 | private function exportViews() |
||
602 | { |
||
603 | if (false === $this->dumpSettings['no-create-info']) { |
||
604 | // Exporting views one by one |
||
605 | foreach ($this->views as $view) { |
||
606 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
607 | continue; |
||
608 | } |
||
609 | $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); |
||
610 | $this->getViewStructureTable($view); |
||
611 | } |
||
612 | foreach ($this->views as $view) { |
||
613 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
614 | continue; |
||
615 | } |
||
616 | $this->getViewStructureView($view); |
||
617 | } |
||
618 | } |
||
619 | } |
||
620 | |||
621 | /** |
||
622 | * Exports all the triggers found in database |
||
623 | * |
||
624 | * @return null |
||
625 | */ |
||
626 | private function exportTriggers() |
||
627 | { |
||
628 | // Exporting triggers one by one |
||
629 | foreach ($this->triggers as $trigger) { |
||
630 | $this->getTriggerStructure($trigger); |
||
631 | } |
||
632 | } |
||
633 | |||
634 | /** |
||
635 | * Exports all the procedures found in database |
||
636 | * |
||
637 | * @return null |
||
638 | */ |
||
639 | private function exportProcedures() |
||
640 | { |
||
641 | // Exporting triggers one by one |
||
642 | foreach ($this->procedures as $procedure) { |
||
643 | $this->getProcedureStructure($procedure); |
||
644 | } |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Exports all the events found in database |
||
649 | * |
||
650 | * @return null |
||
651 | */ |
||
652 | private function exportEvents() |
||
653 | { |
||
654 | // Exporting triggers one by one |
||
655 | foreach ($this->events as $event) { |
||
656 | $this->getEventStructure($event); |
||
657 | } |
||
658 | } |
||
659 | |||
660 | /** |
||
661 | * Table structure extractor |
||
662 | * |
||
663 | * @todo move specific mysql code to typeAdapter |
||
664 | * @param string $tableName Name of table to export |
||
665 | * @return null |
||
666 | */ |
||
667 | private function getTableStructure($tableName) |
||
668 | { |
||
669 | if (!$this->dumpSettings['no-create-info']) { |
||
670 | $ret = ''; |
||
671 | if (!$this->dumpSettings['skip-comments']) { |
||
672 | $ret = "--".PHP_EOL. |
||
673 | "-- Table structure for table `$tableName`".PHP_EOL. |
||
674 | "--".PHP_EOL.PHP_EOL; |
||
675 | } |
||
676 | $stmt = $this->typeAdapter->show_create_table($tableName); |
||
677 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
678 | $this->compressManager->write($ret); |
||
679 | if ($this->dumpSettings['add-drop-table']) { |
||
680 | $this->compressManager->write( |
||
681 | $this->typeAdapter->drop_table($tableName) |
||
682 | ); |
||
683 | } |
||
684 | $this->compressManager->write( |
||
685 | $this->typeAdapter->create_table($r) |
||
686 | ); |
||
687 | break; |
||
688 | } |
||
689 | } |
||
690 | $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); |
||
691 | return; |
||
692 | } |
||
693 | |||
694 | /** |
||
695 | * Store column types to create data dumps and for Stand-In tables |
||
696 | * |
||
697 | * @param string $tableName Name of table to export |
||
698 | * @return array type column types detailed |
||
699 | */ |
||
700 | |||
701 | private function getTableColumnTypes($tableName) { |
||
702 | $columnTypes = array(); |
||
703 | $columns = $this->dbHandler->query( |
||
704 | $this->typeAdapter->show_columns($tableName) |
||
705 | ); |
||
706 | $columns->setFetchMode(PDO::FETCH_ASSOC); |
||
707 | |||
708 | foreach ($columns as $key => $col) { |
||
709 | $types = $this->typeAdapter->parseColumnType($col); |
||
710 | $columnTypes[$col['Field']] = array( |
||
711 | 'is_numeric'=> $types['is_numeric'], |
||
712 | 'is_blob' => $types['is_blob'], |
||
713 | 'type' => $types['type'], |
||
714 | 'type_sql' => $col['Type'], |
||
715 | 'is_virtual' => $types['is_virtual'] |
||
716 | ); |
||
717 | } |
||
718 | |||
719 | return $columnTypes; |
||
720 | } |
||
721 | |||
722 | /** |
||
723 | * View structure extractor, create table (avoids cyclic references) |
||
724 | * |
||
725 | * @todo move mysql specific code to typeAdapter |
||
726 | * @param string $viewName Name of view to export |
||
727 | * @return null |
||
728 | */ |
||
729 | private function getViewStructureTable($viewName) |
||
730 | { |
||
731 | if (!$this->dumpSettings['skip-comments']) { |
||
732 | $ret = "--".PHP_EOL. |
||
733 | "-- Stand-In structure for view `${viewName}`".PHP_EOL. |
||
734 | "--".PHP_EOL.PHP_EOL; |
||
735 | $this->compressManager->write($ret); |
||
736 | } |
||
737 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
738 | |||
739 | // create views as tables, to resolve dependencies |
||
740 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
741 | if ($this->dumpSettings['add-drop-table']) { |
||
742 | $this->compressManager->write( |
||
743 | $this->typeAdapter->drop_view($viewName) |
||
744 | ); |
||
745 | } |
||
746 | |||
747 | $this->compressManager->write( |
||
748 | $this->createStandInTable($viewName) |
||
749 | ); |
||
750 | break; |
||
751 | } |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * Write a create table statement for the table Stand-In, show create |
||
756 | * table would return a create algorithm when used on a view |
||
757 | * |
||
758 | * @param string $viewName Name of view to export |
||
759 | * @return string create statement |
||
760 | */ |
||
761 | function createStandInTable($viewName) { |
||
762 | $ret = array(); |
||
763 | foreach ($this->tableColumnTypes[$viewName] as $k => $v) { |
||
764 | $ret[] = "`${k}` ${v['type_sql']}"; |
||
765 | } |
||
766 | $ret = implode(PHP_EOL.",", $ret); |
||
767 | |||
768 | $ret = "CREATE TABLE IF NOT EXISTS `$viewName` (". |
||
769 | PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; |
||
770 | |||
771 | return $ret; |
||
772 | } |
||
773 | |||
774 | /** |
||
775 | * View structure extractor, create view |
||
776 | * |
||
777 | * @todo move mysql specific code to typeAdapter |
||
778 | * @param string $viewName Name of view to export |
||
779 | * @return null |
||
780 | */ |
||
781 | private function getViewStructureView($viewName) |
||
782 | { |
||
783 | if (!$this->dumpSettings['skip-comments']) { |
||
784 | $ret = "--".PHP_EOL. |
||
785 | "-- View structure for view `${viewName}`".PHP_EOL. |
||
786 | "--".PHP_EOL.PHP_EOL; |
||
787 | $this->compressManager->write($ret); |
||
788 | } |
||
789 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
790 | |||
791 | // create views, to resolve dependencies |
||
792 | // replacing tables with views |
||
793 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
794 | // because we must replace table with view, we should delete it |
||
795 | $this->compressManager->write( |
||
796 | $this->typeAdapter->drop_view($viewName) |
||
797 | ); |
||
798 | $this->compressManager->write( |
||
799 | $this->typeAdapter->create_view($r) |
||
800 | ); |
||
801 | break; |
||
802 | } |
||
803 | } |
||
804 | |||
805 | /** |
||
806 | * Trigger structure extractor |
||
807 | * |
||
808 | * @param string $triggerName Name of trigger to export |
||
809 | * @return null |
||
810 | */ |
||
811 | private function getTriggerStructure($triggerName) |
||
812 | { |
||
813 | $stmt = $this->typeAdapter->show_create_trigger($triggerName); |
||
814 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
815 | if ($this->dumpSettings['add-drop-trigger']) { |
||
816 | $this->compressManager->write( |
||
817 | $this->typeAdapter->add_drop_trigger($triggerName) |
||
818 | ); |
||
819 | } |
||
820 | $this->compressManager->write( |
||
821 | $this->typeAdapter->create_trigger($r) |
||
822 | ); |
||
823 | return; |
||
824 | } |
||
825 | } |
||
826 | |||
827 | /** |
||
828 | * Procedure structure extractor |
||
829 | * |
||
830 | * @param string $procedureName Name of procedure to export |
||
831 | * @return null |
||
832 | */ |
||
833 | private function getProcedureStructure($procedureName) |
||
834 | { |
||
835 | if (!$this->dumpSettings['skip-comments']) { |
||
836 | $ret = "--".PHP_EOL. |
||
837 | "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
||
838 | "--".PHP_EOL.PHP_EOL; |
||
839 | $this->compressManager->write($ret); |
||
840 | } |
||
841 | $stmt = $this->typeAdapter->show_create_procedure($procedureName); |
||
842 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
843 | $this->compressManager->write( |
||
844 | $this->typeAdapter->create_procedure($r) |
||
845 | ); |
||
846 | return; |
||
847 | } |
||
848 | } |
||
849 | |||
850 | /** |
||
851 | * Event structure extractor |
||
852 | * |
||
853 | * @param string $eventName Name of event to export |
||
854 | * @return null |
||
855 | */ |
||
856 | private function getEventStructure($eventName) |
||
857 | { |
||
858 | if (!$this->dumpSettings['skip-comments']) { |
||
859 | $ret = "--".PHP_EOL. |
||
860 | "-- Dumping events for database '".$this->dbName."'".PHP_EOL. |
||
861 | "--".PHP_EOL.PHP_EOL; |
||
862 | $this->compressManager->write($ret); |
||
863 | } |
||
864 | $stmt = $this->typeAdapter->show_create_event($eventName); |
||
865 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
866 | $this->compressManager->write( |
||
867 | $this->typeAdapter->create_event($r) |
||
868 | ); |
||
869 | return; |
||
870 | } |
||
871 | } |
||
872 | |||
873 | /** |
||
874 | * Prepare values for output |
||
875 | * |
||
876 | * @param string $tableName Name of table which contains rows |
||
877 | * @param array $row Associative array of column names and values to be |
||
878 | * quoted |
||
879 | * |
||
880 | * @return array |
||
881 | */ |
||
882 | private function prepareColumnValues($tableName, $row) |
||
892 | } |
||
893 | |||
894 | /** |
||
895 | * Escape values with quotes when needed |
||
896 | * |
||
897 | * @param string $tableName Name of table which contains rows |
||
898 | * @param array $row Associative array of column names and values to be quoted |
||
899 | * |
||
900 | * @return string |
||
901 | */ |
||
902 | private function escape($colValue, $colType) |
||
903 | { |
||
904 | if (is_null($colValue)) { |
||
905 | return "NULL"; |
||
906 | } elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) { |
||
907 | if ($colType['type'] == 'bit' || !empty($colValue)) { |
||
908 | return "0x${colValue}"; |
||
909 | } else { |
||
910 | return "''"; |
||
911 | } |
||
912 | } elseif ($colType['is_numeric']) { |
||
913 | return $colValue; |
||
914 | } |
||
915 | |||
916 | return $this->dbHandler->quote($colValue); |
||
917 | } |
||
918 | |||
919 | /** |
||
920 | * Give extending classes an opportunity to transform column values |
||
921 | * |
||
922 | * @param string $tableName Name of table which contains rows |
||
923 | * @param string $colName Name of the column in question |
||
924 | * @param string $colValue Value of the column in question |
||
925 | * |
||
926 | * @return string |
||
927 | */ |
||
928 | protected function hookTransformColumnValue( |
||
929 | /** @scrutinizer ignore-unused */ $tableName, |
||
930 | /** @scrutinizer ignore-unused */ $colName, |
||
931 | $colValue) |
||
932 | { |
||
933 | return $colValue; |
||
934 | } |
||
935 | |||
936 | /** |
||
937 | * Table rows extractor |
||
938 | * |
||
939 | * @param string $tableName Name of table to export |
||
940 | * |
||
941 | * @return null |
||
942 | */ |
||
943 | private function listValues($tableName) |
||
999 | } |
||
1000 | |||
1001 | /** |
||
1002 | * Table rows extractor, append information prior to dump |
||
1003 | * |
||
1004 | * @param string $tableName Name of table to export |
||
1005 | * |
||
1006 | * @return null |
||
1007 | */ |
||
1008 | function prepareListValues($tableName) |
||
1009 | { |
||
1010 | if (!$this->dumpSettings['skip-comments']) { |
||
1011 | $this->compressManager->write( |
||
1012 | "--".PHP_EOL. |
||
1013 | "-- Dumping data for table `$tableName`".PHP_EOL. |
||
1014 | "--".PHP_EOL.PHP_EOL |
||
1015 | ); |
||
1016 | } |
||
1017 | |||
1018 | if ($this->dumpSettings['single-transaction']) { |
||
1019 | $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
||
1020 | $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
||
1021 | } |
||
1022 | |||
1023 | if ($this->dumpSettings['lock-tables']) { |
||
1024 | $this->typeAdapter->lock_table($tableName); |
||
1025 | } |
||
1026 | |||
1027 | if ($this->dumpSettings['add-locks']) { |
||
1028 | $this->compressManager->write( |
||
1029 | $this->typeAdapter->start_add_lock_table($tableName) |
||
1030 | ); |
||
1031 | } |
||
1032 | |||
1033 | if ($this->dumpSettings['disable-keys']) { |
||
1034 | $this->compressManager->write( |
||
1035 | $this->typeAdapter->start_add_disable_keys($tableName) |
||
1036 | ); |
||
1037 | } |
||
1038 | |||
1039 | // Disable autocommit for faster reload |
||
1040 | if ($this->dumpSettings['no-autocommit']) { |
||
1041 | $this->compressManager->write( |
||
1042 | $this->typeAdapter->start_disable_autocommit() |
||
1043 | ); |
||
1044 | } |
||
1045 | |||
1046 | return; |
||
1047 | } |
||
1048 | |||
1049 | /** |
||
1050 | * Table rows extractor, close locks and commits after dump |
||
1051 | * |
||
1052 | * @param string $tableName Name of table to export |
||
1053 | * |
||
1054 | * @return null |
||
1055 | */ |
||
1056 | function endListValues($tableName) |
||
1057 | { |
||
1058 | if ($this->dumpSettings['disable-keys']) { |
||
1059 | $this->compressManager->write( |
||
1060 | $this->typeAdapter->end_add_disable_keys($tableName) |
||
1061 | ); |
||
1062 | } |
||
1063 | |||
1064 | if ($this->dumpSettings['add-locks']) { |
||
1065 | $this->compressManager->write( |
||
1066 | $this->typeAdapter->end_add_lock_table($tableName) |
||
1067 | ); |
||
1068 | } |
||
1069 | |||
1070 | if ($this->dumpSettings['single-transaction']) { |
||
1071 | $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
||
1072 | } |
||
1073 | |||
1074 | if ($this->dumpSettings['lock-tables']) { |
||
1075 | $this->typeAdapter->unlock_table($tableName); |
||
1076 | } |
||
1077 | |||
1078 | // Commit to enable autocommit |
||
1079 | if ($this->dumpSettings['no-autocommit']) { |
||
1080 | $this->compressManager->write( |
||
1081 | $this->typeAdapter->end_disable_autocommit() |
||
1082 | ); |
||
1083 | } |
||
1084 | |||
1085 | $this->compressManager->write(PHP_EOL); |
||
1086 | |||
1087 | return; |
||
1088 | } |
||
1089 | |||
1090 | /** |
||
1091 | * Build SQL List of all columns on current table which will be used for selecting |
||
1092 | * |
||
1093 | * @param string $tableName Name of table to get columns |
||
1094 | * |
||
1095 | * @return array SQL sentence with columns for select |
||
1096 | */ |
||
1097 | function getColumnStmt($tableName) |
||
1098 | { |
||
1099 | $colStmt = array(); |
||
1100 | foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
||
1101 | if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
||
1102 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
||
1103 | } else if ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
||
1104 | $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
||
1105 | } else if ($colType['is_virtual']) { |
||
1106 | $this->dumpSettings['complete-insert'] = true; |
||
1107 | continue; |
||
1108 | } else { |
||
1109 | $colStmt[] = "`${colName}`"; |
||
1110 | } |
||
1111 | } |
||
1112 | |||
1113 | return $colStmt; |
||
1114 | } |
||
1115 | |||
1116 | /** |
||
1117 | * Build SQL List of all columns on current table which will be used for inserting |
||
1118 | * |
||
1119 | * @param string $tableName Name of table to get columns |
||
1120 | * |
||
1121 | * @return array columns for sql sentence for insert |
||
1122 | */ |
||
1123 | function getColumnNames($tableName) |
||
1135 | } |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * Enum with all available compression methods |
||
1140 | * |
||
1141 | */ |
||
1142 | abstract class CompressMethod |
||
1143 | { |
||
1144 | public static $enums = array( |
||
1145 | "None", |
||
2022 |