Total Complexity | 162 |
Total Lines | 1173 |
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 | private $transformColumnValueCallable; |
||
85 | private $transformTableNameCallable; |
||
86 | /** |
||
87 | * database name, parsed from dsn |
||
88 | * @var string |
||
89 | */ |
||
90 | private $dbName; |
||
91 | /** |
||
92 | * host name, parsed from dsn |
||
93 | * @var string |
||
94 | */ |
||
95 | private $host; |
||
96 | /** |
||
97 | * dsn string parsed as an array |
||
98 | * @var array |
||
99 | */ |
||
100 | private $dsnArray = array(); |
||
101 | |||
102 | /** |
||
103 | * Constructor of Mysqldump. Note that in the case of an SQLite database |
||
104 | * connection, the filename must be in the $db parameter. |
||
105 | * |
||
106 | * @param string $dsn PDO DSN connection string |
||
107 | * @param string $user SQL account username |
||
108 | * @param string $pass SQL account password |
||
109 | * @param array $dumpSettings SQL database settings |
||
110 | * @param array $pdoSettings PDO configured attributes |
||
111 | */ |
||
112 | public function __construct( |
||
113 | $dsn = '', |
||
114 | $user = '', |
||
115 | $pass = '', |
||
116 | $dumpSettings = array(), |
||
117 | $pdoSettings = array() |
||
118 | ) { |
||
119 | $dumpSettingsDefault = array( |
||
120 | 'include-tables' => array(), |
||
121 | 'exclude-tables' => array(), |
||
122 | 'compress' => Mysqldump::NONE, |
||
123 | 'init_commands' => array(), |
||
124 | 'no-data' => array(), |
||
125 | 'reset-auto-increment' => false, |
||
126 | 'add-drop-database' => false, |
||
127 | 'add-drop-table' => false, |
||
128 | 'add-drop-trigger' => true, |
||
129 | 'add-locks' => true, |
||
130 | 'complete-insert' => false, |
||
131 | 'databases' => false, |
||
132 | 'default-character-set' => Mysqldump::UTF8, |
||
133 | 'disable-keys' => true, |
||
134 | 'extended-insert' => true, |
||
135 | 'events' => false, |
||
136 | 'hex-blob' => true, /* faster than escaped content */ |
||
137 | 'insert-ignore' => false, |
||
138 | 'net_buffer_length' => self::MAXLINESIZE, |
||
139 | 'no-autocommit' => true, |
||
140 | 'no-create-info' => false, |
||
141 | 'lock-tables' => true, |
||
142 | 'routines' => false, |
||
143 | 'single-transaction' => true, |
||
144 | 'skip-triggers' => false, |
||
145 | 'skip-tz-utc' => false, |
||
146 | 'skip-comments' => false, |
||
147 | 'skip-dump-date' => false, |
||
148 | 'skip-definer' => false, |
||
149 | 'where' => '', |
||
150 | /* deprecated */ |
||
151 | 'disable-foreign-keys-check' => true |
||
152 | ); |
||
153 | |||
154 | $pdoSettingsDefault = array( |
||
155 | PDO::ATTR_PERSISTENT => true, |
||
156 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
157 | ); |
||
158 | |||
159 | $this->user = $user; |
||
160 | $this->pass = $pass; |
||
161 | $this->parseDsn($dsn); |
||
162 | |||
163 | // this drops MYSQL dependency, only use the constant if it's defined |
||
164 | if ("mysql" === $this->dbType) { |
||
165 | $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
||
166 | } |
||
167 | |||
168 | $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
||
169 | $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
||
170 | $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
||
171 | |||
172 | if (false === $this->dumpSettings['skip-tz-utc']) { |
||
173 | $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
||
174 | } |
||
175 | |||
176 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
||
177 | if (count($diff) > 0) { |
||
178 | throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
||
179 | } |
||
180 | |||
181 | if (!is_array($this->dumpSettings['include-tables']) || |
||
182 | !is_array($this->dumpSettings['exclude-tables'])) { |
||
183 | throw new Exception("Include-tables and exclude-tables should be arrays"); |
||
184 | } |
||
185 | |||
186 | // Dump the same views as tables, mimic mysqldump behaviour |
||
187 | $this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
||
188 | |||
189 | // Create a new compressManager to manage compressed output |
||
190 | $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Destructor of Mysqldump. Unsets dbHandlers and database objects. |
||
195 | * |
||
196 | */ |
||
197 | public function __destruct() |
||
198 | { |
||
199 | $this->dbHandler = null; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Custom array_replace_recursive to be used if PHP < 5.3 |
||
204 | * Replaces elements from passed arrays into the first array recursively |
||
205 | * |
||
206 | * @param array $array1 The array in which elements are replaced |
||
207 | * @param array $array2 The array from which elements will be extracted |
||
208 | * |
||
209 | * @return array Returns an array, or NULL if an error occurs. |
||
210 | */ |
||
211 | public static function array_replace_recursive($array1, $array2) |
||
212 | { |
||
213 | if (function_exists('array_replace_recursive')) { |
||
214 | return array_replace_recursive($array1, $array2); |
||
215 | } |
||
216 | |||
217 | foreach ($array2 as $key => $value) { |
||
218 | if (is_array($value)) { |
||
219 | $array1[$key] = self::array_replace_recursive($array1[$key], $value); |
||
220 | } else { |
||
221 | $array1[$key] = $value; |
||
222 | } |
||
223 | } |
||
224 | return $array1; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Parse DSN string and extract dbname value |
||
229 | * Several examples of a DSN string |
||
230 | * mysql:host=localhost;dbname=testdb |
||
231 | * mysql:host=localhost;port=3307;dbname=testdb |
||
232 | * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
233 | * |
||
234 | * @param string $dsn dsn string to parse |
||
235 | */ |
||
236 | private function parseDsn($dsn) |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Connect with PDO |
||
274 | * |
||
275 | * @return null |
||
276 | */ |
||
277 | private function connect() |
||
278 | { |
||
279 | // Connecting with PDO |
||
280 | try { |
||
281 | switch ($this->dbType) { |
||
282 | case 'sqlite': |
||
283 | $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
||
284 | break; |
||
285 | case 'mysql': |
||
286 | case 'pgsql': |
||
287 | case 'dblib': |
||
288 | $this->dbHandler = @new PDO( |
||
289 | $this->dsn, |
||
290 | $this->user, |
||
291 | $this->pass, |
||
292 | $this->pdoSettings |
||
293 | ); |
||
294 | // Execute init commands once connected |
||
295 | foreach ($this->dumpSettings['init_commands'] as $stmt) { |
||
296 | $this->dbHandler->exec($stmt); |
||
297 | } |
||
298 | // Store server version |
||
299 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
300 | break; |
||
301 | default: |
||
302 | throw new Exception("Unsupported database type (".$this->dbType.")"); |
||
303 | } |
||
304 | } catch (PDOException $e) { |
||
305 | throw new Exception( |
||
306 | "Connection to ".$this->dbType." failed with message: ". |
||
307 | $e->getMessage() |
||
308 | ); |
||
309 | } |
||
310 | |||
311 | if (is_null($this->dbHandler)) { |
||
312 | throw new Exception("Connection to ".$this->dbType."failed"); |
||
313 | } |
||
314 | |||
315 | $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
316 | $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Main call |
||
321 | * |
||
322 | * @param string $filename Name of file to write sql dump to |
||
323 | * @return null |
||
324 | */ |
||
325 | public function start($filename = '') |
||
326 | { |
||
327 | // Output file can be redefined here |
||
328 | if (!empty($filename)) { |
||
329 | $this->fileName = $filename; |
||
330 | } |
||
331 | |||
332 | // Connect to database |
||
333 | $this->connect(); |
||
334 | |||
335 | // Create output file |
||
336 | $this->compressManager->open($this->fileName); |
||
337 | |||
338 | // Write some basic info to output file |
||
339 | $this->compressManager->write($this->getDumpFileHeader()); |
||
340 | |||
341 | // Store server settings and use sanner defaults to dump |
||
342 | $this->compressManager->write( |
||
343 | $this->typeAdapter->backup_parameters() |
||
344 | ); |
||
345 | |||
346 | if ($this->dumpSettings['databases']) { |
||
347 | $this->compressManager->write( |
||
348 | $this->typeAdapter->getDatabaseHeader($this->dbName) |
||
349 | ); |
||
350 | if ($this->dumpSettings['add-drop-database']) { |
||
351 | $this->compressManager->write( |
||
352 | $this->typeAdapter->add_drop_database($this->dbName) |
||
353 | ); |
||
354 | } |
||
355 | } |
||
356 | |||
357 | // Get table, view, trigger, procedures and events |
||
358 | // structures from database |
||
359 | $this->getDatabaseStructureTables(); |
||
360 | $this->getDatabaseStructureViews(); |
||
361 | $this->getDatabaseStructureTriggers(); |
||
362 | $this->getDatabaseStructureProcedures(); |
||
363 | $this->getDatabaseStructureEvents(); |
||
364 | |||
365 | if ($this->dumpSettings['databases']) { |
||
366 | $this->compressManager->write( |
||
367 | $this->typeAdapter->databases($this->dbName) |
||
368 | ); |
||
369 | } |
||
370 | |||
371 | // If there still are some tables/views in include-tables array, |
||
372 | // that means that some tables or views weren't found. |
||
373 | // Give proper error and exit. |
||
374 | // This check will be removed once include-tables supports regexps |
||
375 | if (0 < count($this->dumpSettings['include-tables'])) { |
||
376 | $name = implode(",", $this->dumpSettings['include-tables']); |
||
377 | throw new Exception("Table (".$name.") not found in database"); |
||
378 | } |
||
379 | |||
380 | $this->exportTables(); |
||
381 | $this->exportTriggers(); |
||
382 | $this->exportViews(); |
||
383 | $this->exportProcedures(); |
||
384 | $this->exportEvents(); |
||
385 | |||
386 | // Restore saved parameters |
||
387 | $this->compressManager->write( |
||
388 | $this->typeAdapter->restore_parameters() |
||
389 | ); |
||
390 | // Write some stats to output file |
||
391 | $this->compressManager->write($this->getDumpFileFooter()); |
||
392 | // Close output file |
||
393 | $this->compressManager->close(); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Returns header for dump file |
||
398 | * |
||
399 | * @return string |
||
400 | */ |
||
401 | private function getDumpFileHeader() |
||
402 | { |
||
403 | $header = ''; |
||
404 | if (!$this->dumpSettings['skip-comments']) { |
||
405 | // Some info about software, source and time |
||
406 | $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
||
407 | "--".PHP_EOL. |
||
408 | "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
||
409 | "-- ------------------------------------------------------".PHP_EOL; |
||
410 | |||
411 | if (!empty($this->version)) { |
||
412 | $header .= "-- Server version \t".$this->version.PHP_EOL; |
||
413 | } |
||
414 | |||
415 | if (!$this->dumpSettings['skip-dump-date']) { |
||
416 | $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
||
417 | } |
||
418 | } |
||
419 | return $header; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Returns footer for dump file |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | private function getDumpFileFooter() |
||
428 | { |
||
429 | $footer = ''; |
||
430 | if (!$this->dumpSettings['skip-comments']) { |
||
431 | $footer .= '-- Dump completed'; |
||
432 | if (!$this->dumpSettings['skip-dump-date']) { |
||
433 | $footer .= ' on: '.date('r'); |
||
434 | } |
||
435 | $footer .= PHP_EOL; |
||
436 | } |
||
437 | |||
438 | return $footer; |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Reads table names from database. |
||
443 | * Fills $this->tables array so they will be dumped later. |
||
444 | * |
||
445 | * @return null |
||
446 | */ |
||
447 | private function getDatabaseStructureTables() |
||
448 | { |
||
449 | // Listing all tables from database |
||
450 | if (empty($this->dumpSettings['include-tables'])) { |
||
451 | // include all tables for now, blacklisting happens later |
||
452 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
453 | $this->tables[] = current($row); |
||
454 | } |
||
455 | } else { |
||
456 | // include only the tables mentioned in include-tables |
||
457 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
458 | if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
||
459 | $this->tables[] = current($row); |
||
460 | $elem = array_search( |
||
461 | current($row), |
||
462 | $this->dumpSettings['include-tables'] |
||
463 | ); |
||
464 | unset($this->dumpSettings['include-tables'][$elem]); |
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | return; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Reads view names from database. |
||
473 | * Fills $this->tables array so they will be dumped later. |
||
474 | * |
||
475 | * @return null |
||
476 | */ |
||
477 | private function getDatabaseStructureViews() |
||
478 | { |
||
479 | // Listing all views from database |
||
480 | if (empty($this->dumpSettings['include-views'])) { |
||
481 | // include all views for now, blacklisting happens later |
||
482 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
483 | $this->views[] = current($row); |
||
484 | } |
||
485 | } else { |
||
486 | // include only the tables mentioned in include-tables |
||
487 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
488 | if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
||
489 | $this->views[] = current($row); |
||
490 | $elem = array_search( |
||
491 | current($row), |
||
492 | $this->dumpSettings['include-views'] |
||
493 | ); |
||
494 | unset($this->dumpSettings['include-views'][$elem]); |
||
495 | } |
||
496 | } |
||
497 | } |
||
498 | return; |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Reads trigger names from database. |
||
503 | * Fills $this->tables array so they will be dumped later. |
||
504 | * |
||
505 | * @return null |
||
506 | */ |
||
507 | private function getDatabaseStructureTriggers() |
||
508 | { |
||
509 | // Listing all triggers from database |
||
510 | if (false === $this->dumpSettings['skip-triggers']) { |
||
511 | foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { |
||
512 | $this->triggers[] = $row['Trigger']; |
||
513 | } |
||
514 | } |
||
515 | return; |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Reads procedure names from database. |
||
520 | * Fills $this->tables array so they will be dumped later. |
||
521 | * |
||
522 | * @return null |
||
523 | */ |
||
524 | private function getDatabaseStructureProcedures() |
||
525 | { |
||
526 | // Listing all procedures from database |
||
527 | if ($this->dumpSettings['routines']) { |
||
528 | foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { |
||
529 | $this->procedures[] = $row['procedure_name']; |
||
530 | } |
||
531 | } |
||
532 | return; |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Reads event names from database. |
||
537 | * Fills $this->tables array so they will be dumped later. |
||
538 | * |
||
539 | * @return null |
||
540 | */ |
||
541 | private function getDatabaseStructureEvents() |
||
542 | { |
||
543 | // Listing all events from database |
||
544 | if ($this->dumpSettings['events']) { |
||
545 | foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { |
||
546 | $this->events[] = $row['event_name']; |
||
547 | } |
||
548 | } |
||
549 | return; |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Compare if $table name matches with a definition inside $arr |
||
554 | * @param $table string |
||
555 | * @param $arr array with strings or patterns |
||
556 | * @return bool |
||
557 | */ |
||
558 | private function matches($table, $arr) |
||
559 | { |
||
560 | $match = false; |
||
561 | |||
562 | foreach ($arr as $pattern) { |
||
563 | if ('/' != $pattern[0]) { |
||
564 | continue; |
||
565 | } |
||
566 | if (1 == preg_match($pattern, $table)) { |
||
567 | $match = true; |
||
568 | } |
||
569 | } |
||
570 | |||
571 | return in_array($table, $arr) || $match; |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Exports all the tables selected from database |
||
576 | * |
||
577 | * @return null |
||
578 | */ |
||
579 | private function exportTables() |
||
580 | { |
||
581 | // Exporting tables one by one |
||
582 | foreach ($this->tables as $table) { |
||
583 | if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { |
||
584 | continue; |
||
585 | } |
||
586 | $this->getTableStructure($table); |
||
587 | if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger |
||
588 | $this->listValues($table); |
||
589 | } elseif (true === $this->dumpSettings['no-data'] |
||
590 | || $this->matches($table, $this->dumpSettings['no-data'])) { |
||
591 | continue; |
||
592 | } else { |
||
593 | $this->listValues($table); |
||
594 | } |
||
595 | } |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * Exports all the views found in database |
||
600 | * |
||
601 | * @return null |
||
602 | */ |
||
603 | private function exportViews() |
||
604 | { |
||
605 | if (false === $this->dumpSettings['no-create-info']) { |
||
606 | // Exporting views one by one |
||
607 | foreach ($this->views as $view) { |
||
608 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
609 | continue; |
||
610 | } |
||
611 | $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); |
||
612 | $this->getViewStructureTable($view); |
||
613 | } |
||
614 | foreach ($this->views as $view) { |
||
615 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
616 | continue; |
||
617 | } |
||
618 | $this->getViewStructureView($view); |
||
619 | } |
||
620 | } |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Exports all the triggers found in database |
||
625 | * |
||
626 | * @return null |
||
627 | */ |
||
628 | private function exportTriggers() |
||
629 | { |
||
630 | // Exporting triggers one by one |
||
631 | foreach ($this->triggers as $trigger) { |
||
632 | $this->getTriggerStructure($trigger); |
||
633 | } |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Exports all the procedures found in database |
||
638 | * |
||
639 | * @return null |
||
640 | */ |
||
641 | private function exportProcedures() |
||
642 | { |
||
643 | // Exporting triggers one by one |
||
644 | foreach ($this->procedures as $procedure) { |
||
645 | $this->getProcedureStructure($procedure); |
||
646 | } |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * Exports all the events found in database |
||
651 | * |
||
652 | * @return null |
||
653 | */ |
||
654 | private function exportEvents() |
||
655 | { |
||
656 | // Exporting triggers one by one |
||
657 | foreach ($this->events as $event) { |
||
658 | $this->getEventStructure($event); |
||
659 | } |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * Table structure extractor |
||
664 | * |
||
665 | * @todo move specific mysql code to typeAdapter |
||
666 | * @param string $tableName Name of table to export |
||
667 | * @return null |
||
668 | */ |
||
669 | private function getTableStructure($tableName) |
||
670 | { |
||
671 | if (!$this->dumpSettings['no-create-info']) { |
||
672 | $ret = ''; |
||
673 | if (!$this->dumpSettings['skip-comments']) { |
||
674 | $ret = "--".PHP_EOL. |
||
675 | "-- Table structure for table `$tableName`".PHP_EOL. |
||
676 | "--".PHP_EOL.PHP_EOL; |
||
677 | } |
||
678 | $stmt = $this->typeAdapter->show_create_table($tableName); |
||
679 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
680 | $this->compressManager->write($ret); |
||
681 | if ($this->dumpSettings['add-drop-table']) { |
||
682 | $this->compressManager->write( |
||
683 | $this->typeAdapter->drop_table($tableName) |
||
684 | ); |
||
685 | } |
||
686 | $r = $this->hookTransformTableName($tableName, $r, TRUE); |
||
687 | $this->compressManager->write( |
||
688 | $this->typeAdapter->create_table($r) |
||
689 | ); |
||
690 | break; |
||
691 | } |
||
692 | } |
||
693 | $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); |
||
694 | return; |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Store column types to create data dumps and for Stand-In tables |
||
699 | * |
||
700 | * @param string $tableName Name of table to export |
||
701 | * @return array type column types detailed |
||
702 | */ |
||
703 | |||
704 | private function getTableColumnTypes($tableName) |
||
705 | { |
||
706 | $columnTypes = array(); |
||
707 | $columns = $this->dbHandler->query( |
||
708 | $this->typeAdapter->show_columns($tableName) |
||
709 | ); |
||
710 | $columns->setFetchMode(PDO::FETCH_ASSOC); |
||
711 | |||
712 | foreach ($columns as $key => $col) { |
||
713 | $types = $this->typeAdapter->parseColumnType($col); |
||
714 | $columnTypes[$col['Field']] = array( |
||
715 | 'is_numeric'=> $types['is_numeric'], |
||
716 | 'is_blob' => $types['is_blob'], |
||
717 | 'type' => $types['type'], |
||
718 | 'type_sql' => $col['Type'], |
||
719 | 'is_virtual' => $types['is_virtual'] |
||
720 | ); |
||
721 | } |
||
722 | |||
723 | return $columnTypes; |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * View structure extractor, create table (avoids cyclic references) |
||
728 | * |
||
729 | * @todo move mysql specific code to typeAdapter |
||
730 | * @param string $viewName Name of view to export |
||
731 | * @return null |
||
732 | */ |
||
733 | private function getViewStructureTable($viewName) |
||
734 | { |
||
735 | if (!$this->dumpSettings['skip-comments']) { |
||
736 | $ret = "--".PHP_EOL. |
||
737 | "-- Stand-In structure for view `${viewName}`".PHP_EOL. |
||
738 | "--".PHP_EOL.PHP_EOL; |
||
739 | $this->compressManager->write($ret); |
||
740 | } |
||
741 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
742 | |||
743 | // create views as tables, to resolve dependencies |
||
744 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
745 | if ($this->dumpSettings['add-drop-table']) { |
||
746 | $this->compressManager->write( |
||
747 | $this->typeAdapter->drop_view($viewName) |
||
748 | ); |
||
749 | } |
||
750 | |||
751 | $this->compressManager->write( |
||
752 | $this->createStandInTable($viewName) |
||
753 | ); |
||
754 | break; |
||
755 | } |
||
756 | } |
||
757 | |||
758 | /** |
||
759 | * Write a create table statement for the table Stand-In, show create |
||
760 | * table would return a create algorithm when used on a view |
||
761 | * |
||
762 | * @param string $viewName Name of view to export |
||
763 | * @return string create statement |
||
764 | */ |
||
765 | public function createStandInTable($viewName) |
||
766 | { |
||
767 | $ret = array(); |
||
768 | foreach ($this->tableColumnTypes[$viewName] as $k => $v) { |
||
769 | $ret[] = "`${k}` ${v['type_sql']}"; |
||
770 | } |
||
771 | $ret = implode(PHP_EOL.",", $ret); |
||
772 | |||
773 | $ret = "CREATE TABLE IF NOT EXISTS `$viewName` (". |
||
774 | PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; |
||
775 | |||
776 | return $ret; |
||
777 | } |
||
778 | |||
779 | /** |
||
780 | * View structure extractor, create view |
||
781 | * |
||
782 | * @todo move mysql specific code to typeAdapter |
||
783 | * @param string $viewName Name of view to export |
||
784 | * @return null |
||
785 | */ |
||
786 | private function getViewStructureView($viewName) |
||
787 | { |
||
788 | if (!$this->dumpSettings['skip-comments']) { |
||
789 | $ret = "--".PHP_EOL. |
||
790 | "-- View structure for view `${viewName}`".PHP_EOL. |
||
791 | "--".PHP_EOL.PHP_EOL; |
||
792 | $this->compressManager->write($ret); |
||
793 | } |
||
794 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
795 | |||
796 | // create views, to resolve dependencies |
||
797 | // replacing tables with views |
||
798 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
799 | // because we must replace table with view, we should delete it |
||
800 | $this->compressManager->write( |
||
801 | $this->typeAdapter->drop_view($viewName) |
||
802 | ); |
||
803 | $this->compressManager->write( |
||
804 | $this->typeAdapter->create_view($r) |
||
805 | ); |
||
806 | break; |
||
807 | } |
||
808 | } |
||
809 | |||
810 | /** |
||
811 | * Trigger structure extractor |
||
812 | * |
||
813 | * @param string $triggerName Name of trigger to export |
||
814 | * @return null |
||
815 | */ |
||
816 | private function getTriggerStructure($triggerName) |
||
817 | { |
||
818 | $stmt = $this->typeAdapter->show_create_trigger($triggerName); |
||
819 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
820 | if ($this->dumpSettings['add-drop-trigger']) { |
||
821 | $this->compressManager->write( |
||
822 | $this->typeAdapter->add_drop_trigger($triggerName) |
||
823 | ); |
||
824 | } |
||
825 | $this->compressManager->write( |
||
826 | $this->typeAdapter->create_trigger($r) |
||
827 | ); |
||
828 | return; |
||
829 | } |
||
830 | } |
||
831 | |||
832 | /** |
||
833 | * Procedure structure extractor |
||
834 | * |
||
835 | * @param string $procedureName Name of procedure to export |
||
836 | * @return null |
||
837 | */ |
||
838 | private function getProcedureStructure($procedureName) |
||
839 | { |
||
840 | if (!$this->dumpSettings['skip-comments']) { |
||
841 | $ret = "--".PHP_EOL. |
||
842 | "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
||
843 | "--".PHP_EOL.PHP_EOL; |
||
844 | $this->compressManager->write($ret); |
||
845 | } |
||
846 | $stmt = $this->typeAdapter->show_create_procedure($procedureName); |
||
847 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
848 | $this->compressManager->write( |
||
849 | $this->typeAdapter->create_procedure($r) |
||
850 | ); |
||
851 | return; |
||
852 | } |
||
853 | } |
||
854 | |||
855 | /** |
||
856 | * Event structure extractor |
||
857 | * |
||
858 | * @param string $eventName Name of event to export |
||
859 | * @return null |
||
860 | */ |
||
861 | private function getEventStructure($eventName) |
||
862 | { |
||
863 | if (!$this->dumpSettings['skip-comments']) { |
||
864 | $ret = "--".PHP_EOL. |
||
865 | "-- Dumping events for database '".$this->dbName."'".PHP_EOL. |
||
866 | "--".PHP_EOL.PHP_EOL; |
||
867 | $this->compressManager->write($ret); |
||
868 | } |
||
869 | $stmt = $this->typeAdapter->show_create_event($eventName); |
||
870 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
871 | $this->compressManager->write( |
||
872 | $this->typeAdapter->create_event($r) |
||
873 | ); |
||
874 | return; |
||
875 | } |
||
876 | } |
||
877 | |||
878 | /** |
||
879 | * Prepare values for output |
||
880 | * |
||
881 | * @param string $tableName Name of table which contains rows |
||
882 | * @param array $row Associative array of column names and values to be |
||
883 | * quoted |
||
884 | * |
||
885 | * @return array |
||
886 | */ |
||
887 | private function prepareColumnValues($tableName, $row) |
||
897 | } |
||
898 | |||
899 | /** |
||
900 | * Escape values with quotes when needed |
||
901 | * |
||
902 | * @param string $tableName Name of table which contains rows |
||
903 | * @param array $row Associative array of column names and values to be quoted |
||
904 | * |
||
905 | * @return string |
||
906 | */ |
||
907 | private function escape($colValue, $colType) |
||
908 | { |
||
909 | if (is_null($colValue)) { |
||
910 | return "NULL"; |
||
911 | } elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) { |
||
912 | if ($colType['type'] == 'bit' || !empty($colValue)) { |
||
913 | return "0x${colValue}"; |
||
914 | } else { |
||
915 | return "''"; |
||
916 | } |
||
917 | } elseif ($colType['is_numeric']) { |
||
918 | return $colValue; |
||
919 | } |
||
920 | |||
921 | return $this->dbHandler->quote($colValue); |
||
922 | } |
||
923 | |||
924 | /** |
||
925 | * Set a callable that will will be used to transform column values. |
||
926 | * |
||
927 | * @param callable $callable |
||
928 | * |
||
929 | * @return void |
||
930 | */ |
||
931 | public function setTransformColumnValueHook($callable) |
||
932 | { |
||
933 | $this->transformColumnValueCallable = $callable; |
||
934 | } |
||
935 | |||
936 | /** |
||
937 | * Set a callable that will transform table names. |
||
938 | * |
||
939 | * @param callable $callable |
||
940 | * |
||
941 | * @return void |
||
942 | */ |
||
943 | public function setTransformTableNameHook($callable) |
||
946 | } |
||
947 | |||
948 | /** |
||
949 | * Give extending classes an opportunity to transform column values |
||
950 | * |
||
951 | * @param string $tableName Name of table which contains rows |
||
952 | * @param string $colName Name of the column in question |
||
953 | * @param string $colValue Value of the column in question |
||
954 | * |
||
955 | * @return string |
||
956 | */ |
||
957 | protected function hookTransformColumnValue($tableName, $colName, $colValue, $row) |
||
958 | { |
||
959 | if (! $this->transformColumnValueCallable) { |
||
960 | return $colValue; |
||
961 | } |
||
962 | |||
963 | return call_user_func_array($this->transformColumnValueCallable, array( |
||
964 | $tableName, |
||
965 | $colName, |
||
966 | $colValue, |
||
967 | $row |
||
968 | )); |
||
969 | } |
||
970 | |||
971 | /** |
||
972 | * Give extending classes an opportunity to transform table names |
||
973 | * |
||
974 | * @param string $tableName Name of table which contains rows |
||
975 | * |
||
976 | * @return string |
||
977 | */ |
||
978 | protected function hookTransformTableName($tableName, $row = FALSE, $reset = FALSE) |
||
1002 | } |
||
1003 | |||
1004 | /** |
||
1005 | * Table rows extractor |
||
1006 | * |
||
1007 | * @param string $tableName Name of table to export |
||
1008 | * |
||
1009 | * @return null |
||
1010 | */ |
||
1011 | private function listValues($tableName) |
||
1068 | } |
||
1069 | |||
1070 | /** |
||
1071 | * Table rows extractor, append information prior to dump |
||
1072 | * |
||
1073 | * @param string $tableName Name of table to export |
||
1074 | * |
||
1075 | * @return null |
||
1076 | */ |
||
1077 | public function prepareListValues($tableName) |
||
1078 | { |
||
1079 | if (!$this->dumpSettings['skip-comments']) { |
||
1080 | $tableName = $this->hookTransformTableName($tableName, FALSE, TRUE); |
||
1081 | $this->compressManager->write( |
||
1082 | "--".PHP_EOL. |
||
1083 | "-- Dumping data for table `$tableName`".PHP_EOL. |
||
1084 | "--".PHP_EOL.PHP_EOL |
||
1085 | ); |
||
1086 | $tableName = $this->hookTransformTableName($tableName, FALSE); |
||
1087 | } |
||
1088 | |||
1089 | if ($this->dumpSettings['single-transaction']) { |
||
1090 | $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
||
1091 | $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
||
1092 | } |
||
1093 | |||
1094 | if ($this->dumpSettings['lock-tables']) { |
||
1095 | $this->typeAdapter->lock_table($tableName); |
||
1096 | } |
||
1097 | |||
1098 | if ($this->dumpSettings['add-locks']) { |
||
1099 | $this->compressManager->write( |
||
1100 | $this->typeAdapter->start_add_lock_table($tableName) |
||
1101 | ); |
||
1102 | } |
||
1103 | |||
1104 | if ($this->dumpSettings['disable-keys']) { |
||
1105 | $tableName = $this->hookTransformTableName($tableName, FALSE, TRUE); |
||
1106 | $this->compressManager->write( |
||
1107 | $this->typeAdapter->start_add_disable_keys($tableName) |
||
1108 | ); |
||
1109 | } |
||
1110 | |||
1111 | // Disable autocommit for faster reload |
||
1112 | if ($this->dumpSettings['no-autocommit']) { |
||
1113 | $this->compressManager->write( |
||
1114 | $this->typeAdapter->start_disable_autocommit() |
||
1115 | ); |
||
1116 | } |
||
1117 | |||
1118 | return; |
||
1119 | } |
||
1120 | |||
1121 | /** |
||
1122 | * Table rows extractor, close locks and commits after dump |
||
1123 | * |
||
1124 | * @param string $tableName Name of table to export |
||
1125 | * |
||
1126 | * @return null |
||
1127 | */ |
||
1128 | public function endListValues($tableName) |
||
1129 | { |
||
1130 | if ($this->dumpSettings['disable-keys']) { |
||
1131 | $this->compressManager->write( |
||
1132 | $this->typeAdapter->end_add_disable_keys($tableName) |
||
1133 | ); |
||
1134 | } |
||
1135 | |||
1136 | if ($this->dumpSettings['add-locks']) { |
||
1137 | $this->compressManager->write( |
||
1138 | $this->typeAdapter->end_add_lock_table($tableName) |
||
1139 | ); |
||
1140 | } |
||
1141 | |||
1142 | if ($this->dumpSettings['single-transaction']) { |
||
1143 | $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
||
1144 | } |
||
1145 | |||
1146 | if ($this->dumpSettings['lock-tables']) { |
||
1147 | $this->typeAdapter->unlock_table($tableName); |
||
1148 | } |
||
1149 | |||
1150 | // Commit to enable autocommit |
||
1151 | if ($this->dumpSettings['no-autocommit']) { |
||
1152 | $this->compressManager->write( |
||
1153 | $this->typeAdapter->end_disable_autocommit() |
||
1154 | ); |
||
1155 | } |
||
1156 | |||
1157 | $this->compressManager->write(PHP_EOL); |
||
1158 | |||
1159 | return; |
||
1160 | } |
||
1161 | |||
1162 | /** |
||
1163 | * Build SQL List of all columns on current table which will be used for selecting |
||
1164 | * |
||
1165 | * @param string $tableName Name of table to get columns |
||
1166 | * |
||
1167 | * @return array SQL sentence with columns for select |
||
1168 | */ |
||
1169 | public function getColumnStmt($tableName) |
||
1170 | { |
||
1171 | $colStmt = array(); |
||
1172 | foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
||
1173 | if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
||
1174 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
||
1175 | } elseif ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
||
1176 | $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
||
1177 | } elseif ($colType['is_virtual']) { |
||
1178 | $this->dumpSettings['complete-insert'] = true; |
||
1179 | continue; |
||
1180 | } else { |
||
1181 | $colStmt[] = "`${colName}`"; |
||
1182 | } |
||
1183 | } |
||
1184 | |||
1185 | return $colStmt; |
||
1186 | } |
||
1187 | |||
1188 | /** |
||
1189 | * Build SQL List of all columns on current table which will be used for inserting |
||
1190 | * |
||
1191 | * @param string $tableName Name of table to get columns |
||
1192 | * |
||
1193 | * @return array columns for sql sentence for insert |
||
1194 | */ |
||
1195 | public function getColumnNames($tableName) |
||
1207 | } |
||
1208 | } |
||
1209 | |||
1210 | /** |
||
1211 | * Enum with all available compression methods |
||
1212 | * |
||
1213 | */ |
||
1214 | abstract class CompressMethod |
||
1215 | { |
||
1216 | public static $enums = array( |
||
1217 | "None", |
||
1218 | "Gzip", |
||
1219 | "Bzip2" |
||
1220 | ); |
||
1221 | |||
2092 |