Total Complexity | 154 |
Total Lines | 1118 |
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 | /** |
||
86 | * database name, parsed from dsn |
||
87 | * @var string |
||
88 | */ |
||
89 | private $dbName; |
||
90 | /** |
||
91 | * host name, parsed from dsn |
||
92 | * @var string |
||
93 | */ |
||
94 | private $host; |
||
95 | /** |
||
96 | * dsn string parsed as an array |
||
97 | * @var array |
||
98 | */ |
||
99 | private $dsnArray = array(); |
||
100 | |||
101 | /** |
||
102 | * Constructor of Mysqldump. Note that in the case of an SQLite database |
||
103 | * connection, the filename must be in the $db parameter. |
||
104 | * |
||
105 | * @param string $dsn PDO DSN connection string |
||
106 | * @param string $user SQL account username |
||
107 | * @param string $pass SQL account password |
||
108 | * @param array $dumpSettings SQL database settings |
||
109 | * @param array $pdoSettings PDO configured attributes |
||
110 | */ |
||
111 | public function __construct( |
||
112 | $dsn = '', |
||
113 | $user = '', |
||
114 | $pass = '', |
||
115 | $dumpSettings = array(), |
||
116 | $pdoSettings = array() |
||
117 | ) { |
||
118 | $dumpSettingsDefault = array( |
||
119 | 'include-tables' => array(), |
||
120 | 'exclude-tables' => array(), |
||
121 | 'compress' => Mysqldump::NONE, |
||
122 | 'init_commands' => array(), |
||
123 | 'no-data' => array(), |
||
124 | 'reset-auto-increment' => false, |
||
125 | 'add-drop-database' => false, |
||
126 | 'add-drop-table' => false, |
||
127 | 'add-drop-trigger' => true, |
||
128 | 'add-locks' => true, |
||
129 | 'complete-insert' => false, |
||
130 | 'databases' => false, |
||
131 | 'default-character-set' => Mysqldump::UTF8, |
||
132 | 'disable-keys' => true, |
||
133 | 'extended-insert' => true, |
||
134 | 'events' => false, |
||
135 | 'hex-blob' => true, /* faster than escaped content */ |
||
136 | 'insert-ignore' => false, |
||
137 | 'net_buffer_length' => self::MAXLINESIZE, |
||
138 | 'no-autocommit' => true, |
||
139 | 'no-create-info' => false, |
||
140 | 'lock-tables' => true, |
||
141 | 'routines' => false, |
||
142 | 'single-transaction' => true, |
||
143 | 'skip-triggers' => false, |
||
144 | 'skip-tz-utc' => false, |
||
145 | 'skip-comments' => false, |
||
146 | 'skip-dump-date' => false, |
||
147 | 'skip-definer' => false, |
||
148 | 'where' => '', |
||
149 | /* deprecated */ |
||
150 | 'disable-foreign-keys-check' => true |
||
151 | ); |
||
152 | |||
153 | $pdoSettingsDefault = array( |
||
154 | PDO::ATTR_PERSISTENT => true, |
||
155 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
156 | ); |
||
157 | |||
158 | $this->user = $user; |
||
159 | $this->pass = $pass; |
||
160 | $this->parseDsn($dsn); |
||
161 | |||
162 | // this drops MYSQL dependency, only use the constant if it's defined |
||
163 | if ("mysql" === $this->dbType) { |
||
|
|||
164 | $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
||
165 | } |
||
166 | |||
167 | $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
||
168 | $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
||
169 | $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
||
170 | |||
171 | if (false === $this->dumpSettings['skip-tz-utc']) { |
||
172 | $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
||
173 | } |
||
174 | |||
175 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
||
176 | if (count($diff) > 0) { |
||
177 | throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
||
178 | } |
||
179 | |||
180 | if (!is_array($this->dumpSettings['include-tables']) || |
||
181 | !is_array($this->dumpSettings['exclude-tables'])) { |
||
182 | throw new Exception("Include-tables and exclude-tables should be arrays"); |
||
183 | } |
||
184 | |||
185 | // Dump the same views as tables, mimic mysqldump behaviour |
||
186 | $this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
||
187 | |||
188 | // Create a new compressManager to manage compressed output |
||
189 | $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Destructor of Mysqldump. Unsets dbHandlers and database objects. |
||
194 | * |
||
195 | */ |
||
196 | public function __destruct() |
||
197 | { |
||
198 | $this->dbHandler = null; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Custom array_replace_recursive to be used if PHP < 5.3 |
||
203 | * Replaces elements from passed arrays into the first array recursively |
||
204 | * |
||
205 | * @param array $array1 The array in which elements are replaced |
||
206 | * @param array $array2 The array from which elements will be extracted |
||
207 | * |
||
208 | * @return array Returns an array, or NULL if an error occurs. |
||
209 | */ |
||
210 | public static function array_replace_recursive($array1, $array2) |
||
211 | { |
||
212 | if (function_exists('array_replace_recursive')) { |
||
213 | return array_replace_recursive($array1, $array2); |
||
214 | } |
||
215 | |||
216 | foreach ($array2 as $key => $value) { |
||
217 | if (is_array($value)) { |
||
218 | $array1[$key] = self::array_replace_recursive($array1[$key], $value); |
||
219 | } else { |
||
220 | $array1[$key] = $value; |
||
221 | } |
||
222 | } |
||
223 | return $array1; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Parse DSN string and extract dbname value |
||
228 | * Several examples of a DSN string |
||
229 | * mysql:host=localhost;dbname=testdb |
||
230 | * mysql:host=localhost;port=3307;dbname=testdb |
||
231 | * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
232 | * |
||
233 | * @param string $dsn dsn string to parse |
||
234 | */ |
||
235 | private function parseDsn($dsn) |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Connect with PDO |
||
273 | * |
||
274 | * @return null |
||
275 | */ |
||
276 | private function connect() |
||
277 | { |
||
278 | // Connecting with PDO |
||
279 | try { |
||
280 | switch ($this->dbType) { |
||
281 | case 'sqlite': |
||
282 | $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
||
283 | break; |
||
284 | case 'mysql': |
||
285 | case 'pgsql': |
||
286 | case 'dblib': |
||
287 | $this->dbHandler = @new PDO( |
||
288 | $this->dsn, |
||
289 | $this->user, |
||
290 | $this->pass, |
||
291 | $this->pdoSettings |
||
292 | ); |
||
293 | // Execute init commands once connected |
||
294 | foreach ($this->dumpSettings['init_commands'] as $stmt) { |
||
295 | $this->dbHandler->exec($stmt); |
||
296 | } |
||
297 | // Store server version |
||
298 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
299 | break; |
||
300 | default: |
||
301 | throw new Exception("Unsupported database type (".$this->dbType.")"); |
||
302 | } |
||
303 | } catch (PDOException $e) { |
||
304 | throw new Exception( |
||
305 | "Connection to ".$this->dbType." failed with message: ". |
||
306 | $e->getMessage() |
||
307 | ); |
||
308 | } |
||
309 | |||
310 | if (is_null($this->dbHandler)) { |
||
311 | throw new Exception("Connection to ".$this->dbType."failed"); |
||
312 | } |
||
313 | |||
314 | $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
315 | $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Main call |
||
320 | * |
||
321 | * @param string $filename Name of file to write sql dump to |
||
322 | * @return null |
||
323 | */ |
||
324 | public function start($filename = '') |
||
325 | { |
||
326 | // Output file can be redefined here |
||
327 | if (!empty($filename)) { |
||
328 | $this->fileName = $filename; |
||
329 | } |
||
330 | |||
331 | // Connect to database |
||
332 | $this->connect(); |
||
333 | |||
334 | // Create output file |
||
335 | $this->compressManager->open($this->fileName); |
||
336 | |||
337 | // Write some basic info to output file |
||
338 | $this->compressManager->write($this->getDumpFileHeader()); |
||
339 | |||
340 | // Store server settings and use sanner defaults to dump |
||
341 | $this->compressManager->write( |
||
342 | $this->typeAdapter->backup_parameters() |
||
343 | ); |
||
344 | |||
345 | if ($this->dumpSettings['databases']) { |
||
346 | $this->compressManager->write( |
||
347 | $this->typeAdapter->getDatabaseHeader($this->dbName) |
||
348 | ); |
||
349 | if ($this->dumpSettings['add-drop-database']) { |
||
350 | $this->compressManager->write( |
||
351 | $this->typeAdapter->add_drop_database($this->dbName) |
||
352 | ); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | // Get table, view, trigger, procedures and events |
||
357 | // structures from database |
||
358 | $this->getDatabaseStructureTables(); |
||
359 | $this->getDatabaseStructureViews(); |
||
360 | $this->getDatabaseStructureTriggers(); |
||
361 | $this->getDatabaseStructureProcedures(); |
||
362 | $this->getDatabaseStructureEvents(); |
||
363 | |||
364 | if ($this->dumpSettings['databases']) { |
||
365 | $this->compressManager->write( |
||
366 | $this->typeAdapter->databases($this->dbName) |
||
367 | ); |
||
368 | } |
||
369 | |||
370 | // If there still are some tables/views in include-tables array, |
||
371 | // that means that some tables or views weren't found. |
||
372 | // Give proper error and exit. |
||
373 | // This check will be removed once include-tables supports regexps |
||
374 | if (0 < count($this->dumpSettings['include-tables'])) { |
||
375 | $name = implode(",", $this->dumpSettings['include-tables']); |
||
376 | throw new Exception("Table (".$name.") not found in database"); |
||
377 | } |
||
378 | |||
379 | $this->exportTables(); |
||
380 | $this->exportTriggers(); |
||
381 | $this->exportViews(); |
||
382 | $this->exportProcedures(); |
||
383 | $this->exportEvents(); |
||
384 | |||
385 | // Restore saved parameters |
||
386 | $this->compressManager->write( |
||
387 | $this->typeAdapter->restore_parameters() |
||
388 | ); |
||
389 | // Write some stats to output file |
||
390 | $this->compressManager->write($this->getDumpFileFooter()); |
||
391 | // Close output file |
||
392 | $this->compressManager->close(); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Returns header for dump file |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | private function getDumpFileHeader() |
||
401 | { |
||
402 | $header = ''; |
||
403 | if (!$this->dumpSettings['skip-comments']) { |
||
404 | // Some info about software, source and time |
||
405 | $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
||
406 | "--".PHP_EOL. |
||
407 | "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
||
408 | "-- ------------------------------------------------------".PHP_EOL; |
||
409 | |||
410 | if (!empty($this->version)) { |
||
411 | $header .= "-- Server version \t".$this->version.PHP_EOL; |
||
412 | } |
||
413 | |||
414 | if (!$this->dumpSettings['skip-dump-date']) { |
||
415 | $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
||
416 | } |
||
417 | } |
||
418 | return $header; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Returns footer for dump file |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | private function getDumpFileFooter() |
||
427 | { |
||
428 | $footer = ''; |
||
429 | if (!$this->dumpSettings['skip-comments']) { |
||
430 | $footer .= '-- Dump completed'; |
||
431 | if (!$this->dumpSettings['skip-dump-date']) { |
||
432 | $footer .= ' on: '.date('r'); |
||
433 | } |
||
434 | $footer .= PHP_EOL; |
||
435 | } |
||
436 | |||
437 | return $footer; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Reads table names from database. |
||
442 | * Fills $this->tables array so they will be dumped later. |
||
443 | * |
||
444 | * @return null |
||
445 | */ |
||
446 | private function getDatabaseStructureTables() |
||
447 | { |
||
448 | // Listing all tables from database |
||
449 | if (empty($this->dumpSettings['include-tables'])) { |
||
450 | // include all tables for now, blacklisting happens later |
||
451 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
452 | array_push($this->tables, current($row)); |
||
453 | } |
||
454 | } else { |
||
455 | // include only the tables mentioned in include-tables |
||
456 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
457 | if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
||
458 | array_push($this->tables, current($row)); |
||
459 | $elem = array_search( |
||
460 | current($row), |
||
461 | $this->dumpSettings['include-tables'] |
||
462 | ); |
||
463 | unset($this->dumpSettings['include-tables'][$elem]); |
||
464 | } |
||
465 | } |
||
466 | } |
||
467 | return; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Reads view names from database. |
||
472 | * Fills $this->tables array so they will be dumped later. |
||
473 | * |
||
474 | * @return null |
||
475 | */ |
||
476 | private function getDatabaseStructureViews() |
||
477 | { |
||
478 | // Listing all views from database |
||
479 | if (empty($this->dumpSettings['include-views'])) { |
||
480 | // include all views for now, blacklisting happens later |
||
481 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
482 | array_push($this->views, current($row)); |
||
483 | } |
||
484 | } else { |
||
485 | // include only the tables mentioned in include-tables |
||
486 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
487 | if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
||
488 | array_push($this->views, current($row)); |
||
489 | $elem = array_search( |
||
490 | current($row), |
||
491 | $this->dumpSettings['include-views'] |
||
492 | ); |
||
493 | unset($this->dumpSettings['include-views'][$elem]); |
||
494 | } |
||
495 | } |
||
496 | } |
||
497 | return; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Reads trigger names from database. |
||
502 | * Fills $this->tables array so they will be dumped later. |
||
503 | * |
||
504 | * @return null |
||
505 | */ |
||
506 | private 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 | private 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 | private 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 | * Set a callable that will will be used to transform column values. |
||
921 | * |
||
922 | * @param callable $callable |
||
923 | * |
||
924 | * @return void |
||
925 | */ |
||
926 | public function setTransformColumnValueHook($callable) |
||
927 | { |
||
928 | $this->transformColumnValueCallable = $callable; |
||
929 | } |
||
930 | |||
931 | /** |
||
932 | * Give extending classes an opportunity to transform column values |
||
933 | * |
||
934 | * @param string $tableName Name of table which contains rows |
||
935 | * @param string $colName Name of the column in question |
||
936 | * @param string $colValue Value of the column in question |
||
937 | * |
||
938 | * @return string |
||
939 | */ |
||
940 | protected function hookTransformColumnValue($tableName, $colName, $colValue) |
||
941 | { |
||
942 | if (! $this->transformColumnValueCallable) { |
||
943 | return $colValue; |
||
944 | } |
||
945 | |||
946 | return call_user_func_array($this->transformColumnValueCallable, array( |
||
947 | $tableName, |
||
948 | $colName, |
||
949 | $colValue, |
||
950 | )); |
||
951 | } |
||
952 | |||
953 | /** |
||
954 | * Table rows extractor |
||
955 | * |
||
956 | * @param string $tableName Name of table to export |
||
957 | * |
||
958 | * @return null |
||
959 | */ |
||
960 | private function listValues($tableName) |
||
1016 | } |
||
1017 | |||
1018 | /** |
||
1019 | * Table rows extractor, append information prior to dump |
||
1020 | * |
||
1021 | * @param string $tableName Name of table to export |
||
1022 | * |
||
1023 | * @return null |
||
1024 | */ |
||
1025 | function prepareListValues($tableName) |
||
1026 | { |
||
1027 | if (!$this->dumpSettings['skip-comments']) { |
||
1028 | $this->compressManager->write( |
||
1029 | "--".PHP_EOL. |
||
1030 | "-- Dumping data for table `$tableName`".PHP_EOL. |
||
1031 | "--".PHP_EOL.PHP_EOL |
||
1032 | ); |
||
1033 | } |
||
1034 | |||
1035 | if ($this->dumpSettings['single-transaction']) { |
||
1036 | $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
||
1037 | $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
||
1038 | } |
||
1039 | |||
1040 | if ($this->dumpSettings['lock-tables']) { |
||
1041 | $this->typeAdapter->lock_table($tableName); |
||
1042 | } |
||
1043 | |||
1044 | if ($this->dumpSettings['add-locks']) { |
||
1045 | $this->compressManager->write( |
||
1046 | $this->typeAdapter->start_add_lock_table($tableName) |
||
1047 | ); |
||
1048 | } |
||
1049 | |||
1050 | if ($this->dumpSettings['disable-keys']) { |
||
1051 | $this->compressManager->write( |
||
1052 | $this->typeAdapter->start_add_disable_keys($tableName) |
||
1053 | ); |
||
1054 | } |
||
1055 | |||
1056 | // Disable autocommit for faster reload |
||
1057 | if ($this->dumpSettings['no-autocommit']) { |
||
1058 | $this->compressManager->write( |
||
1059 | $this->typeAdapter->start_disable_autocommit() |
||
1060 | ); |
||
1061 | } |
||
1062 | |||
1063 | return; |
||
1064 | } |
||
1065 | |||
1066 | /** |
||
1067 | * Table rows extractor, close locks and commits after dump |
||
1068 | * |
||
1069 | * @param string $tableName Name of table to export |
||
1070 | * |
||
1071 | * @return null |
||
1072 | */ |
||
1073 | function endListValues($tableName) |
||
1074 | { |
||
1075 | if ($this->dumpSettings['disable-keys']) { |
||
1076 | $this->compressManager->write( |
||
1077 | $this->typeAdapter->end_add_disable_keys($tableName) |
||
1078 | ); |
||
1079 | } |
||
1080 | |||
1081 | if ($this->dumpSettings['add-locks']) { |
||
1082 | $this->compressManager->write( |
||
1083 | $this->typeAdapter->end_add_lock_table($tableName) |
||
1084 | ); |
||
1085 | } |
||
1086 | |||
1087 | if ($this->dumpSettings['single-transaction']) { |
||
1088 | $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
||
1089 | } |
||
1090 | |||
1091 | if ($this->dumpSettings['lock-tables']) { |
||
1092 | $this->typeAdapter->unlock_table($tableName); |
||
1093 | } |
||
1094 | |||
1095 | // Commit to enable autocommit |
||
1096 | if ($this->dumpSettings['no-autocommit']) { |
||
1097 | $this->compressManager->write( |
||
1098 | $this->typeAdapter->end_disable_autocommit() |
||
1099 | ); |
||
1100 | } |
||
1101 | |||
1102 | $this->compressManager->write(PHP_EOL); |
||
1103 | |||
1104 | return; |
||
1105 | } |
||
1106 | |||
1107 | /** |
||
1108 | * Build SQL List of all columns on current table which will be used for selecting |
||
1109 | * |
||
1110 | * @param string $tableName Name of table to get columns |
||
1111 | * |
||
1112 | * @return array SQL sentence with columns for select |
||
1113 | */ |
||
1114 | function getColumnStmt($tableName) |
||
1115 | { |
||
1116 | $colStmt = array(); |
||
1117 | foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
||
1118 | if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
||
1119 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
||
1120 | } else if ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
||
1121 | $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
||
1122 | } else if ($colType['is_virtual']) { |
||
1123 | $this->dumpSettings['complete-insert'] = true; |
||
1124 | continue; |
||
1125 | } else { |
||
1126 | $colStmt[] = "`${colName}`"; |
||
1127 | } |
||
1128 | } |
||
1129 | |||
1130 | return $colStmt; |
||
1131 | } |
||
1132 | |||
1133 | /** |
||
1134 | * Build SQL List of all columns on current table which will be used for inserting |
||
1135 | * |
||
1136 | * @param string $tableName Name of table to get columns |
||
1137 | * |
||
1138 | * @return array columns for sql sentence for insert |
||
1139 | */ |
||
1140 | function getColumnNames($tableName) |
||
1152 | } |
||
1153 | } |
||
1154 | |||
1155 | /** |
||
1156 | * Enum with all available compression methods |
||
1157 | * |
||
1158 | */ |
||
1159 | abstract class CompressMethod |
||
1160 | { |
||
1161 | public static $enums = array( |
||
1162 | "None", |
||
1163 | "Gzip", |
||
1164 | "Bzip2" |
||
1165 | ); |
||
1166 | |||
2039 |