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