Total Complexity | 146 |
Total Lines | 1047 |
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 | 'net_buffer_length' => self::MAXLINESIZE, |
||
136 | 'no-autocommit' => true, |
||
137 | 'no-create-info' => false, |
||
138 | 'lock-tables' => true, |
||
139 | 'routines' => false, |
||
140 | 'single-transaction' => true, |
||
141 | 'skip-triggers' => false, |
||
142 | 'skip-tz-utc' => false, |
||
143 | 'skip-comments' => false, |
||
144 | 'skip-dump-date' => false, |
||
145 | 'skip-definer' => false, |
||
146 | 'where' => '', |
||
147 | /* deprecated */ |
||
148 | 'disable-foreign-keys-check' => true |
||
149 | ); |
||
150 | |||
151 | $pdoSettingsDefault = array( |
||
152 | PDO::ATTR_PERSISTENT => true, |
||
153 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
154 | ); |
||
155 | |||
156 | $this->user = $user; |
||
157 | $this->pass = $pass; |
||
158 | $this->parseDsn($dsn); |
||
159 | |||
160 | // this drops MYSQL dependency, only use the constant if it's defined |
||
161 | if ("mysql" === $this->dbType) { |
||
|
|||
162 | $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
||
163 | } |
||
164 | |||
165 | $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
||
166 | $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
||
167 | $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
||
168 | |||
169 | if (false === $this->dumpSettings['skip-tz-utc']) { |
||
170 | $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
||
171 | } |
||
172 | |||
173 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
||
174 | if (count($diff) > 0) { |
||
175 | throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
||
176 | } |
||
177 | |||
178 | if (!is_array($this->dumpSettings['include-tables']) || |
||
179 | !is_array($this->dumpSettings['exclude-tables'])) { |
||
180 | throw new Exception("Include-tables and exclude-tables should be arrays"); |
||
181 | } |
||
182 | |||
183 | // Dump the same views as tables, mimic mysqldump behaviour |
||
184 | $this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
||
185 | |||
186 | // Create a new compressManager to manage compressed output |
||
187 | $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Destructor of Mysqldump. Unsets dbHandlers and database objects. |
||
192 | * |
||
193 | */ |
||
194 | public function __destruct() |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Custom array_replace_recursive to be used if PHP < 5.3 |
||
201 | * Replaces elements from passed arrays into the first array recursively |
||
202 | * |
||
203 | * @param array $array1 The array in which elements are replaced |
||
204 | * @param array $array2 The array from which elements will be extracted |
||
205 | * |
||
206 | * @return array Returns an array, or NULL if an error occurs. |
||
207 | */ |
||
208 | public static function array_replace_recursive($array1, $array2) |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Parse DSN string and extract dbname value |
||
226 | * Several examples of a DSN string |
||
227 | * mysql:host=localhost;dbname=testdb |
||
228 | * mysql:host=localhost;port=3307;dbname=testdb |
||
229 | * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
230 | * |
||
231 | * @param string $dsn dsn string to parse |
||
232 | */ |
||
233 | private function parseDsn($dsn) |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Connect with PDO |
||
276 | * |
||
277 | * @return null |
||
278 | */ |
||
279 | private function connect() |
||
280 | { |
||
281 | // Connecting with PDO |
||
282 | try { |
||
283 | switch ($this->dbType) { |
||
284 | case 'sqlite': |
||
285 | $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
||
286 | break; |
||
287 | case 'mysql': |
||
288 | case 'pgsql': |
||
289 | case 'dblib': |
||
290 | $this->dbHandler = @new PDO( |
||
291 | $this->dsn, |
||
292 | $this->user, |
||
293 | $this->pass, |
||
294 | $this->pdoSettings |
||
295 | ); |
||
296 | // Execute init commands once connected |
||
297 | foreach ($this->dumpSettings['init_commands'] as $stmt) { |
||
298 | $this->dbHandler->exec($stmt); |
||
299 | } |
||
300 | // Store server version |
||
301 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
302 | break; |
||
303 | default: |
||
304 | throw new Exception("Unsupported database type (".$this->dbType.")"); |
||
305 | } |
||
306 | } catch (PDOException $e) { |
||
307 | throw new Exception( |
||
308 | "Connection to ".$this->dbType." failed with message: ". |
||
309 | $e->getMessage() |
||
310 | ); |
||
311 | } |
||
312 | |||
313 | if (is_null($this->dbHandler)) { |
||
314 | throw new Exception("Connection to ".$this->dbType."failed"); |
||
315 | } |
||
316 | |||
317 | $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
318 | $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Main call |
||
323 | * |
||
324 | * @param string $filename Name of file to write sql dump to |
||
325 | * @return null |
||
326 | */ |
||
327 | public function start($filename = '') |
||
328 | { |
||
329 | // Output file can be redefined here |
||
330 | if (!empty($filename)) { |
||
331 | $this->fileName = $filename; |
||
332 | } |
||
333 | |||
334 | // Connect to database |
||
335 | $this->connect(); |
||
336 | |||
337 | // Create output file |
||
338 | $this->compressManager->open($this->fileName); |
||
339 | |||
340 | // Write some basic info to output file |
||
341 | $this->compressManager->write($this->getDumpFileHeader()); |
||
342 | |||
343 | // Store server settings and use sanner defaults to dump |
||
344 | $this->compressManager->write( |
||
345 | $this->typeAdapter->backup_parameters() |
||
346 | ); |
||
347 | |||
348 | if ($this->dumpSettings['databases']) { |
||
349 | $this->compressManager->write( |
||
350 | $this->typeAdapter->getDatabaseHeader($this->dbName) |
||
351 | ); |
||
352 | if ($this->dumpSettings['add-drop-database']) { |
||
353 | $this->compressManager->write( |
||
354 | $this->typeAdapter->add_drop_database($this->dbName) |
||
355 | ); |
||
356 | } |
||
357 | } |
||
358 | |||
359 | // Get table, view and trigger structures from database |
||
360 | $this->getDatabaseStructure(); |
||
361 | |||
362 | if ($this->dumpSettings['databases']) { |
||
363 | $this->compressManager->write( |
||
364 | $this->typeAdapter->databases($this->dbName) |
||
365 | ); |
||
366 | } |
||
367 | |||
368 | // If there still are some tables/views in include-tables array, |
||
369 | // that means that some tables or views weren't found. |
||
370 | // Give proper error and exit. |
||
371 | // This check will be removed once include-tables supports regexps |
||
372 | if (0 < count($this->dumpSettings['include-tables'])) { |
||
373 | $name = implode(",", $this->dumpSettings['include-tables']); |
||
374 | throw new Exception("Table (".$name.") not found in database"); |
||
375 | } |
||
376 | |||
377 | $this->exportTables(); |
||
378 | $this->exportTriggers(); |
||
379 | $this->exportViews(); |
||
380 | $this->exportProcedures(); |
||
381 | $this->exportEvents(); |
||
382 | |||
383 | // Restore saved parameters |
||
384 | $this->compressManager->write( |
||
385 | $this->typeAdapter->restore_parameters() |
||
386 | ); |
||
387 | // Write some stats to output file |
||
388 | $this->compressManager->write($this->getDumpFileFooter()); |
||
389 | // Close output file |
||
390 | $this->compressManager->close(); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Returns header for dump file |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | private function getDumpFileHeader() |
||
399 | { |
||
400 | $header = ''; |
||
401 | if (!$this->dumpSettings['skip-comments']) { |
||
402 | // Some info about software, source and time |
||
403 | $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
||
404 | "--".PHP_EOL. |
||
405 | "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
||
406 | "-- ------------------------------------------------------".PHP_EOL; |
||
407 | |||
408 | if (!empty($this->version)) { |
||
409 | $header .= "-- Server version \t".$this->version.PHP_EOL; |
||
410 | } |
||
411 | |||
412 | if (!$this->dumpSettings['skip-dump-date']) { |
||
413 | $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
||
414 | } |
||
415 | } |
||
416 | return $header; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Returns footer for dump file |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | private function getDumpFileFooter() |
||
425 | { |
||
426 | $footer = ''; |
||
427 | if (!$this->dumpSettings['skip-comments']) { |
||
428 | $footer .= '-- Dump completed'; |
||
429 | if (!$this->dumpSettings['skip-dump-date']) { |
||
430 | $footer .= ' on: '.date('r'); |
||
431 | } |
||
432 | $footer .= PHP_EOL; |
||
433 | } |
||
434 | |||
435 | return $footer; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Reads table and views names from database. |
||
440 | * Fills $this->tables array so they will be dumped later. |
||
441 | * |
||
442 | * @return null |
||
443 | */ |
||
444 | private function getDatabaseStructure() |
||
445 | { |
||
446 | // Listing all tables from database |
||
447 | if (empty($this->dumpSettings['include-tables'])) { |
||
448 | // include all tables for now, blacklisting happens later |
||
449 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
450 | array_push($this->tables, current($row)); |
||
451 | } |
||
452 | } else { |
||
453 | // include only the tables mentioned in include-tables |
||
454 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
455 | if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
||
456 | array_push($this->tables, current($row)); |
||
457 | $elem = array_search( |
||
458 | current($row), |
||
459 | $this->dumpSettings['include-tables'] |
||
460 | ); |
||
461 | unset($this->dumpSettings['include-tables'][$elem]); |
||
462 | } |
||
463 | } |
||
464 | } |
||
465 | |||
466 | // Listing all views from database |
||
467 | if (empty($this->dumpSettings['include-views'])) { |
||
468 | // include all views for now, blacklisting happens later |
||
469 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
470 | array_push($this->views, current($row)); |
||
471 | } |
||
472 | } else { |
||
473 | // include only the tables mentioned in include-tables |
||
474 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
475 | if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
||
476 | array_push($this->views, current($row)); |
||
477 | $elem = array_search( |
||
478 | current($row), |
||
479 | $this->dumpSettings['include-views'] |
||
480 | ); |
||
481 | unset($this->dumpSettings['include-views'][$elem]); |
||
482 | } |
||
483 | } |
||
484 | } |
||
485 | |||
486 | // Listing all triggers from database |
||
487 | if (false === $this->dumpSettings['skip-triggers']) { |
||
488 | foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { |
||
489 | array_push($this->triggers, $row['Trigger']); |
||
490 | } |
||
491 | } |
||
492 | |||
493 | // Listing all procedures from database |
||
494 | if ($this->dumpSettings['routines']) { |
||
495 | foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { |
||
496 | array_push($this->procedures, $row['procedure_name']); |
||
497 | } |
||
498 | } |
||
499 | |||
500 | // Listing all events from database |
||
501 | if ($this->dumpSettings['events']) { |
||
502 | foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { |
||
503 | array_push($this->events, $row['event_name']); |
||
504 | } |
||
505 | } |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Compare if $table name matches with a definition inside $arr |
||
510 | * @param $table string |
||
511 | * @param $arr array with strings or patterns |
||
512 | * @return bool |
||
513 | */ |
||
514 | private function matches($table, $arr) { |
||
515 | $match = false; |
||
516 | |||
517 | foreach ($arr as $pattern) { |
||
518 | if ('/' != $pattern[0]) { |
||
519 | continue; |
||
520 | } |
||
521 | if (1 == preg_match($pattern, $table)) { |
||
522 | $match = true; |
||
523 | } |
||
524 | } |
||
525 | |||
526 | return in_array($table, $arr) || $match; |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * Exports all the tables selected from database |
||
531 | * |
||
532 | * @return null |
||
533 | */ |
||
534 | private function exportTables() |
||
535 | { |
||
536 | // Exporting tables one by one |
||
537 | foreach ($this->tables as $table) { |
||
538 | if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { |
||
539 | continue; |
||
540 | } |
||
541 | $this->getTableStructure($table); |
||
542 | if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger |
||
543 | $this->listValues($table); |
||
544 | } else if (true === $this->dumpSettings['no-data'] |
||
545 | || $this->matches($table, $this->dumpSettings['no-data'])) { |
||
546 | continue; |
||
547 | } else { |
||
548 | $this->listValues($table); |
||
549 | } |
||
550 | } |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * Exports all the views found in database |
||
555 | * |
||
556 | * @return null |
||
557 | */ |
||
558 | private function exportViews() |
||
559 | { |
||
560 | if (false === $this->dumpSettings['no-create-info']) { |
||
561 | // Exporting views one by one |
||
562 | foreach ($this->views as $view) { |
||
563 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
564 | continue; |
||
565 | } |
||
566 | $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); |
||
567 | $this->getViewStructureTable($view); |
||
568 | } |
||
569 | foreach ($this->views as $view) { |
||
570 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
571 | continue; |
||
572 | } |
||
573 | $this->getViewStructureView($view); |
||
574 | } |
||
575 | } |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Exports all the triggers found in database |
||
580 | * |
||
581 | * @return null |
||
582 | */ |
||
583 | private function exportTriggers() |
||
584 | { |
||
585 | // Exporting triggers one by one |
||
586 | foreach ($this->triggers as $trigger) { |
||
587 | $this->getTriggerStructure($trigger); |
||
588 | } |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Exports all the procedures found in database |
||
593 | * |
||
594 | * @return null |
||
595 | */ |
||
596 | private function exportProcedures() |
||
597 | { |
||
598 | // Exporting triggers one by one |
||
599 | foreach ($this->procedures as $procedure) { |
||
600 | $this->getProcedureStructure($procedure); |
||
601 | } |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Exports all the events found in database |
||
606 | * |
||
607 | * @return null |
||
608 | */ |
||
609 | private function exportEvents() |
||
610 | { |
||
611 | // Exporting triggers one by one |
||
612 | foreach ($this->events as $event) { |
||
613 | $this->getEventStructure($event); |
||
614 | } |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * Table structure extractor |
||
619 | * |
||
620 | * @todo move specific mysql code to typeAdapter |
||
621 | * @param string $tableName Name of table to export |
||
622 | * @return null |
||
623 | */ |
||
624 | private function getTableStructure($tableName) |
||
625 | { |
||
626 | if (!$this->dumpSettings['no-create-info']) { |
||
627 | $ret = ''; |
||
628 | if (!$this->dumpSettings['skip-comments']) { |
||
629 | $ret = "--".PHP_EOL. |
||
630 | "-- Table structure for table `$tableName`".PHP_EOL. |
||
631 | "--".PHP_EOL.PHP_EOL; |
||
632 | } |
||
633 | $stmt = $this->typeAdapter->show_create_table($tableName); |
||
634 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
635 | $this->compressManager->write($ret); |
||
636 | if ($this->dumpSettings['add-drop-table']) { |
||
637 | $this->compressManager->write( |
||
638 | $this->typeAdapter->drop_table($tableName) |
||
639 | ); |
||
640 | } |
||
641 | $this->compressManager->write( |
||
642 | $this->typeAdapter->create_table($r) |
||
643 | ); |
||
644 | break; |
||
645 | } |
||
646 | } |
||
647 | $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); |
||
648 | return; |
||
649 | } |
||
650 | |||
651 | /** |
||
652 | * Store column types to create data dumps and for Stand-In tables |
||
653 | * |
||
654 | * @param string $tableName Name of table to export |
||
655 | * @return array type column types detailed |
||
656 | */ |
||
657 | |||
658 | private function getTableColumnTypes($tableName) { |
||
659 | $columnTypes = array(); |
||
660 | $columns = $this->dbHandler->query( |
||
661 | $this->typeAdapter->show_columns($tableName) |
||
662 | ); |
||
663 | $columns->setFetchMode(PDO::FETCH_ASSOC); |
||
664 | |||
665 | foreach ($columns as $key => $col) { |
||
666 | $types = $this->typeAdapter->parseColumnType($col); |
||
667 | $columnTypes[$col['Field']] = array( |
||
668 | 'is_numeric'=> $types['is_numeric'], |
||
669 | 'is_blob' => $types['is_blob'], |
||
670 | 'type' => $types['type'], |
||
671 | 'type_sql' => $col['Type'], |
||
672 | 'is_virtual' => $types['is_virtual'] |
||
673 | ); |
||
674 | } |
||
675 | |||
676 | return $columnTypes; |
||
677 | } |
||
678 | |||
679 | /** |
||
680 | * View structure extractor, create table (avoids cyclic references) |
||
681 | * |
||
682 | * @todo move mysql specific code to typeAdapter |
||
683 | * @param string $viewName Name of view to export |
||
684 | * @return null |
||
685 | */ |
||
686 | private function getViewStructureTable($viewName) |
||
687 | { |
||
688 | if (!$this->dumpSettings['skip-comments']) { |
||
689 | $ret = "--".PHP_EOL. |
||
690 | "-- Stand-In structure for view `${viewName}`".PHP_EOL. |
||
691 | "--".PHP_EOL.PHP_EOL; |
||
692 | $this->compressManager->write($ret); |
||
693 | } |
||
694 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
695 | |||
696 | // create views as tables, to resolve dependencies |
||
697 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
698 | if ($this->dumpSettings['add-drop-table']) { |
||
699 | $this->compressManager->write( |
||
700 | $this->typeAdapter->drop_view($viewName) |
||
701 | ); |
||
702 | } |
||
703 | |||
704 | $this->compressManager->write( |
||
705 | $this->createStandInTable($viewName) |
||
706 | ); |
||
707 | break; |
||
708 | } |
||
709 | } |
||
710 | |||
711 | /** |
||
712 | * Write a create table statement for the table Stand-In, show create |
||
713 | * table would return a create algorithm when used on a view |
||
714 | * |
||
715 | * @param string $viewName Name of view to export |
||
716 | * @return string create statement |
||
717 | */ |
||
718 | function createStandInTable($viewName) { |
||
719 | $ret = array(); |
||
720 | foreach ($this->tableColumnTypes[$viewName] as $k => $v) { |
||
721 | $ret[] = "`${k}` ${v['type_sql']}"; |
||
722 | } |
||
723 | $ret = implode(PHP_EOL.",", $ret); |
||
724 | |||
725 | $ret = "CREATE TABLE IF NOT EXISTS `$viewName` (". |
||
726 | PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; |
||
727 | |||
728 | return $ret; |
||
729 | } |
||
730 | |||
731 | /** |
||
732 | * View structure extractor, create view |
||
733 | * |
||
734 | * @todo move mysql specific code to typeAdapter |
||
735 | * @param string $viewName Name of view to export |
||
736 | * @return null |
||
737 | */ |
||
738 | private function getViewStructureView($viewName) |
||
739 | { |
||
740 | if (!$this->dumpSettings['skip-comments']) { |
||
741 | $ret = "--".PHP_EOL. |
||
742 | "-- View structure for view `${viewName}`".PHP_EOL. |
||
743 | "--".PHP_EOL.PHP_EOL; |
||
744 | $this->compressManager->write($ret); |
||
745 | } |
||
746 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
747 | |||
748 | // create views, to resolve dependencies |
||
749 | // replacing tables with views |
||
750 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
751 | // because we must replace table with view, we should delete it |
||
752 | $this->compressManager->write( |
||
753 | $this->typeAdapter->drop_view($viewName) |
||
754 | ); |
||
755 | $this->compressManager->write( |
||
756 | $this->typeAdapter->create_view($r) |
||
757 | ); |
||
758 | break; |
||
759 | } |
||
760 | } |
||
761 | |||
762 | /** |
||
763 | * Trigger structure extractor |
||
764 | * |
||
765 | * @param string $triggerName Name of trigger to export |
||
766 | * @return null |
||
767 | */ |
||
768 | private function getTriggerStructure($triggerName) |
||
769 | { |
||
770 | $stmt = $this->typeAdapter->show_create_trigger($triggerName); |
||
771 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
772 | if ($this->dumpSettings['add-drop-trigger']) { |
||
773 | $this->compressManager->write( |
||
774 | $this->typeAdapter->add_drop_trigger($triggerName) |
||
775 | ); |
||
776 | } |
||
777 | $this->compressManager->write( |
||
778 | $this->typeAdapter->create_trigger($r) |
||
779 | ); |
||
780 | return; |
||
781 | } |
||
782 | } |
||
783 | |||
784 | /** |
||
785 | * Procedure structure extractor |
||
786 | * |
||
787 | * @param string $procedureName Name of procedure to export |
||
788 | * @return null |
||
789 | */ |
||
790 | private function getProcedureStructure($procedureName) |
||
791 | { |
||
792 | if (!$this->dumpSettings['skip-comments']) { |
||
793 | $ret = "--".PHP_EOL. |
||
794 | "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
||
795 | "--".PHP_EOL.PHP_EOL; |
||
796 | $this->compressManager->write($ret); |
||
797 | } |
||
798 | $stmt = $this->typeAdapter->show_create_procedure($procedureName); |
||
799 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
800 | $this->compressManager->write( |
||
801 | $this->typeAdapter->create_procedure($r) |
||
802 | ); |
||
803 | return; |
||
804 | } |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * Event structure extractor |
||
809 | * |
||
810 | * @param string $eventName Name of event to export |
||
811 | * @return null |
||
812 | */ |
||
813 | private function getEventStructure($eventName) |
||
814 | { |
||
815 | if (!$this->dumpSettings['skip-comments']) { |
||
816 | $ret = "--".PHP_EOL. |
||
817 | "-- Dumping events for database '".$this->dbName."'".PHP_EOL. |
||
818 | "--".PHP_EOL.PHP_EOL; |
||
819 | $this->compressManager->write($ret); |
||
820 | } |
||
821 | $stmt = $this->typeAdapter->show_create_event($eventName); |
||
822 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
823 | $this->compressManager->write( |
||
824 | $this->typeAdapter->create_event($r) |
||
825 | ); |
||
826 | return; |
||
827 | } |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * Prepare values for output |
||
832 | * |
||
833 | * @param string $tableName Name of table which contains rows |
||
834 | * @param array $row Associative array of column names and values to be |
||
835 | * quoted |
||
836 | * |
||
837 | * @return array |
||
838 | */ |
||
839 | private function prepareColumnValues($tableName, $row) |
||
849 | } |
||
850 | |||
851 | /** |
||
852 | * Escape values with quotes when needed |
||
853 | * |
||
854 | * @param string $tableName Name of table which contains rows |
||
855 | * @param array $row Associative array of column names and values to be quoted |
||
856 | * |
||
857 | * @return string |
||
858 | */ |
||
859 | private function escape($colValue, $colType) |
||
860 | { |
||
861 | if (is_null($colValue)) { |
||
862 | return "NULL"; |
||
863 | } elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) { |
||
864 | if ($colType['type'] == 'bit' || !empty($colValue)) { |
||
865 | return "0x${colValue}"; |
||
866 | } else { |
||
867 | return "''"; |
||
868 | } |
||
869 | } elseif ($colType['is_numeric']) { |
||
870 | return $colValue; |
||
871 | } |
||
872 | |||
873 | return $this->dbHandler->quote($colValue); |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * Give extending classes an opportunity to transform column values |
||
878 | * |
||
879 | * @param string $tableName Name of table which contains rows |
||
880 | * @param string $colName Name of the column in question |
||
881 | * @param string $colValue Value of the column in question |
||
882 | * |
||
883 | * @return string |
||
884 | */ |
||
885 | protected function hookTransformColumnValue( |
||
886 | /** @scrutinizer ignore-unused */ $tableName, |
||
887 | /** @scrutinizer ignore-unused */ $colName, |
||
888 | $colValue) |
||
889 | { |
||
890 | return $colValue; |
||
891 | } |
||
892 | |||
893 | /** |
||
894 | * Table rows extractor |
||
895 | * |
||
896 | * @param string $tableName Name of table to export |
||
897 | * |
||
898 | * @return null |
||
899 | */ |
||
900 | private function listValues($tableName) |
||
948 | } |
||
949 | |||
950 | /** |
||
951 | * Table rows extractor, append information prior to dump |
||
952 | * |
||
953 | * @param string $tableName Name of table to export |
||
954 | * |
||
955 | * @return null |
||
956 | */ |
||
957 | function prepareListValues($tableName) |
||
996 | } |
||
997 | |||
998 | /** |
||
999 | * Table rows extractor, close locks and commits after dump |
||
1000 | * |
||
1001 | * @param string $tableName Name of table to export |
||
1002 | * |
||
1003 | * @return null |
||
1004 | */ |
||
1005 | function endListValues($tableName) |
||
1037 | } |
||
1038 | |||
1039 | /** |
||
1040 | * Build SQL List of all columns on current table |
||
1041 | * |
||
1042 | * @param string $tableName Name of table to get columns |
||
1043 | * |
||
1044 | * @return array SQL sentence with columns |
||
1045 | */ |
||
1046 | function getColumnStmt($tableName) |
||
1047 | { |
||
1048 | $colStmt = array(); |
||
1049 | foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
||
1050 | if($hookColumnStmt = $this->hookTransformColumnStmt($tableName, $colName, $colType)) { |
||
1051 | $colStmt[] = $hookColumnStmt; |
||
1052 | } else if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
||
1053 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
||
1054 | } else if ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
||
1055 | $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
||
1056 | } else if ($colType['is_virtual']) { |
||
1057 | $this->dumpSettings['complete-insert'] = true; |
||
1058 | continue; |
||
1059 | } else { |
||
1060 | $colStmt[] = "`${colName}`"; |
||
1061 | } |
||
1062 | } |
||
1063 | |||
1064 | return $colStmt; |
||
1065 | } |
||
1066 | |||
1067 | /** |
||
1068 | * Give extending classes an opportunity to transform column values |
||
1069 | * |
||
1070 | * @param string $tableName Name of table which contains rows |
||
1071 | * @param string $colName Name of the column in question |
||
1072 | * @param string $colType Type of the column in question |
||
1073 | * |
||
1074 | * @return string |
||
1075 | */ |
||
1076 | function hookTransformColumnStmt( |
||
1081 | } |
||
1082 | } |
||
1968 |