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