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