Total Complexity | 172 |
Total Lines | 1272 |
Duplicated Lines | 0 % |
Changes | 75 | ||
Bugs | 14 | Features | 8 |
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 |
||
31 | class Mysqldump |
||
32 | { |
||
33 | |||
34 | // Same as mysqldump. |
||
35 | const MAXLINESIZE = 1000000; |
||
36 | |||
37 | // List of available compression methods as constants. |
||
38 | const GZIP = 'Gzip'; |
||
39 | const BZIP2 = 'Bzip2'; |
||
40 | const NONE = 'None'; |
||
41 | |||
42 | // List of available connection strings. |
||
43 | const UTF8 = 'utf8'; |
||
44 | const UTF8MB4 = 'utf8mb4'; |
||
45 | |||
46 | /** |
||
47 | * Database username. |
||
48 | * @var string |
||
49 | */ |
||
50 | public $user; |
||
51 | |||
52 | /** |
||
53 | * Database password. |
||
54 | * @var string |
||
55 | */ |
||
56 | public $pass; |
||
57 | |||
58 | /** |
||
59 | * Connection string for PDO. |
||
60 | * @var string |
||
61 | */ |
||
62 | public $dsn; |
||
63 | |||
64 | /** |
||
65 | * Destination filename, defaults to stdout. |
||
66 | * @var string |
||
67 | */ |
||
68 | public $fileName = 'php://stdout'; |
||
69 | |||
70 | // Internal stuff. |
||
71 | private $tables = array(); |
||
72 | private $views = array(); |
||
73 | private $triggers = array(); |
||
74 | private $procedures = array(); |
||
75 | private $functions = array(); |
||
76 | private $events = array(); |
||
77 | private $dbHandler = null; |
||
78 | private $dbType = ""; |
||
79 | private $compressManager; |
||
80 | private $typeAdapter; |
||
81 | private $dumpSettings = array(); |
||
82 | private $pdoSettings = array(); |
||
83 | private $version; |
||
84 | private $tableColumnTypes = array(); |
||
85 | private $transformColumnValueCallable; |
||
86 | |||
87 | /** |
||
88 | * Database name, parsed from dsn. |
||
89 | * @var string |
||
90 | */ |
||
91 | private $dbName; |
||
92 | |||
93 | /** |
||
94 | * Host name, parsed from dsn. |
||
95 | * @var string |
||
96 | */ |
||
97 | private $host; |
||
98 | |||
99 | /** |
||
100 | * Dsn string parsed as an array. |
||
101 | * @var array |
||
102 | */ |
||
103 | private $dsnArray = array(); |
||
104 | |||
105 | /** |
||
106 | * Keyed on table name, with the value as the conditions. |
||
107 | * e.g. - 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH' |
||
108 | * |
||
109 | * @var array |
||
110 | */ |
||
111 | private $tableWheres = array(); |
||
112 | private $tableLimits = array(); |
||
113 | |||
114 | /** |
||
115 | * Constructor of Mysqldump. Note that in the case of an SQLite database |
||
116 | * connection, the filename must be in the $db parameter. |
||
117 | * |
||
118 | * @param string $dsn PDO DSN connection string |
||
119 | * @param string $user SQL account username |
||
120 | * @param string $pass SQL account password |
||
121 | * @param array $dumpSettings SQL database settings |
||
122 | * @param array $pdoSettings PDO configured attributes |
||
123 | */ |
||
124 | public function __construct( |
||
125 | $dsn = '', |
||
126 | $user = '', |
||
127 | $pass = '', |
||
128 | $dumpSettings = array(), |
||
129 | $pdoSettings = array() |
||
130 | ) { |
||
131 | $dumpSettingsDefault = array( |
||
132 | 'include-tables' => array(), |
||
133 | 'exclude-tables' => array(), |
||
134 | 'compress' => Mysqldump::NONE, |
||
135 | 'init_commands' => array(), |
||
136 | 'no-data' => array(), |
||
137 | 'reset-auto-increment' => false, |
||
138 | 'add-drop-database' => false, |
||
139 | 'add-drop-table' => false, |
||
140 | 'add-drop-trigger' => true, |
||
141 | 'add-locks' => true, |
||
142 | 'complete-insert' => false, |
||
143 | 'databases' => false, |
||
144 | 'default-character-set' => Mysqldump::UTF8, |
||
145 | 'disable-keys' => true, |
||
146 | 'extended-insert' => true, |
||
147 | 'events' => false, |
||
148 | 'hex-blob' => true, /* faster than escaped content */ |
||
149 | 'insert-ignore' => false, |
||
150 | 'net_buffer_length' => self::MAXLINESIZE, |
||
151 | 'no-autocommit' => true, |
||
152 | 'no-create-info' => false, |
||
153 | 'lock-tables' => true, |
||
154 | 'routines' => false, |
||
155 | 'single-transaction' => true, |
||
156 | 'skip-triggers' => false, |
||
157 | 'skip-tz-utc' => false, |
||
158 | 'skip-comments' => false, |
||
159 | 'skip-dump-date' => false, |
||
160 | 'skip-definer' => false, |
||
161 | 'where' => '', |
||
162 | /* deprecated */ |
||
163 | 'disable-foreign-keys-check' => true |
||
164 | ); |
||
165 | |||
166 | $pdoSettingsDefault = array( |
||
167 | PDO::ATTR_PERSISTENT => true, |
||
168 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
169 | ); |
||
170 | |||
171 | $this->user = $user; |
||
172 | $this->pass = $pass; |
||
173 | $this->parseDsn($dsn); |
||
174 | |||
175 | // This drops MYSQL dependency, only use the constant if it's defined. |
||
176 | if ("mysql" === $this->dbType) { |
||
177 | $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
||
178 | } |
||
179 | |||
180 | $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
||
181 | $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
||
182 | $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
||
183 | |||
184 | if (false === $this->dumpSettings['skip-tz-utc']) { |
||
185 | $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
||
186 | } |
||
187 | |||
188 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
||
189 | if (count($diff) > 0) { |
||
190 | throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
||
191 | } |
||
192 | |||
193 | if (!is_array($this->dumpSettings['include-tables']) || |
||
194 | !is_array($this->dumpSettings['exclude-tables'])) { |
||
195 | throw new Exception("Include-tables and exclude-tables should be arrays"); |
||
196 | } |
||
197 | |||
198 | // Dump the same views as tables, mimic mysqldump behaviour |
||
199 | $this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
||
200 | |||
201 | // Create a new compressManager to manage compressed output |
||
202 | $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Destructor of Mysqldump. Unsets dbHandlers and database objects. |
||
207 | */ |
||
208 | public function __destruct() |
||
209 | { |
||
210 | $this->dbHandler = null; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Custom array_replace_recursive to be used if PHP < 5.3 |
||
215 | * Replaces elements from passed arrays into the first array recursively. |
||
216 | * |
||
217 | * @param array $array1 The array in which elements are replaced |
||
218 | * @param array $array2 The array from which elements will be extracted |
||
219 | * |
||
220 | * @return array Returns an array, or NULL if an error occurs. |
||
221 | */ |
||
222 | public static function array_replace_recursive($array1, $array2) |
||
223 | { |
||
224 | if (function_exists('array_replace_recursive')) { |
||
225 | return array_replace_recursive($array1, $array2); |
||
226 | } |
||
227 | |||
228 | foreach ($array2 as $key => $value) { |
||
229 | if (is_array($value)) { |
||
230 | $array1[$key] = self::array_replace_recursive($array1[$key], $value); |
||
231 | } else { |
||
232 | $array1[$key] = $value; |
||
233 | } |
||
234 | } |
||
235 | return $array1; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Keyed by table name, with the value as the conditions: |
||
240 | * e.g. 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH AND deleted=0' |
||
241 | * |
||
242 | * @param array $tableWheres |
||
243 | */ |
||
244 | public function setTableWheres(array $tableWheres) |
||
245 | { |
||
246 | $this->tableWheres = $tableWheres; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param $tableName |
||
251 | * |
||
252 | * @return boolean|mixed |
||
253 | */ |
||
254 | public function getTableWhere($tableName) |
||
255 | { |
||
256 | if (!empty($this->tableWheres[$tableName])) { |
||
257 | return $this->tableWheres[$tableName]; |
||
258 | } elseif ($this->dumpSettings['where']) { |
||
259 | return $this->dumpSettings['where']; |
||
260 | } |
||
261 | |||
262 | return false; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Keyed by table name, with the value as the numeric limit: |
||
267 | * e.g. 'users' => 3000 |
||
268 | * |
||
269 | * @param array $tableLimits |
||
270 | */ |
||
271 | public function setTableLimits(array $tableLimits) |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Returns the LIMIT for the table. Must be numeric to be returned. |
||
278 | * @param $tableName |
||
279 | * @return boolean |
||
280 | */ |
||
281 | public function getTableLimit($tableName) |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Parse DSN string and extract dbname value |
||
297 | * Several examples of a DSN string |
||
298 | * mysql:host=localhost;dbname=testdb |
||
299 | * mysql:host=localhost;port=3307;dbname=testdb |
||
300 | * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
301 | * |
||
302 | * @param string $dsn dsn string to parse |
||
303 | * @return boolean |
||
304 | */ |
||
305 | private function parseDsn($dsn) |
||
306 | { |
||
307 | if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) { |
||
308 | throw new Exception("Empty DSN string"); |
||
309 | } |
||
310 | |||
311 | $this->dsn = $dsn; |
||
312 | $this->dbType = strtolower(substr($dsn, 0, $pos)); // always returns a string |
||
313 | |||
314 | if (empty($this->dbType)) { |
||
315 | throw new Exception("Missing database type from DSN string"); |
||
316 | } |
||
317 | |||
318 | $dsn = substr($dsn, $pos + 1); |
||
319 | |||
320 | foreach (explode(";", $dsn) as $kvp) { |
||
321 | $kvpArr = explode("=", $kvp); |
||
322 | $this->dsnArray[strtolower($kvpArr[0])] = $kvpArr[1]; |
||
323 | } |
||
324 | |||
325 | if (empty($this->dsnArray['host']) && |
||
326 | empty($this->dsnArray['unix_socket'])) { |
||
327 | throw new Exception("Missing host from DSN string"); |
||
328 | } |
||
329 | $this->host = (!empty($this->dsnArray['host'])) ? |
||
330 | $this->dsnArray['host'] : $this->dsnArray['unix_socket']; |
||
331 | |||
332 | if (empty($this->dsnArray['dbname'])) { |
||
333 | throw new Exception("Missing database name from DSN string"); |
||
334 | } |
||
335 | |||
336 | $this->dbName = $this->dsnArray['dbname']; |
||
337 | |||
338 | return true; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Connect with PDO. |
||
343 | * |
||
344 | * @return null |
||
345 | */ |
||
346 | private function connect() |
||
347 | { |
||
348 | // Connecting with PDO. |
||
349 | try { |
||
350 | switch ($this->dbType) { |
||
351 | case 'sqlite': |
||
352 | $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
||
353 | break; |
||
354 | case 'mysql': |
||
355 | case 'pgsql': |
||
356 | case 'dblib': |
||
357 | $this->dbHandler = @new PDO( |
||
358 | $this->dsn, |
||
359 | $this->user, |
||
360 | $this->pass, |
||
361 | $this->pdoSettings |
||
362 | ); |
||
363 | // Execute init commands once connected |
||
364 | foreach ($this->dumpSettings['init_commands'] as $stmt) { |
||
365 | $this->dbHandler->exec($stmt); |
||
366 | } |
||
367 | // Store server version |
||
368 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
369 | break; |
||
370 | default: |
||
371 | throw new Exception("Unsupported database type (".$this->dbType.")"); |
||
372 | } |
||
373 | } catch (PDOException $e) { |
||
374 | throw new Exception( |
||
375 | "Connection to ".$this->dbType." failed with message: ". |
||
376 | $e->getMessage() |
||
377 | ); |
||
378 | } |
||
379 | |||
380 | if (is_null($this->dbHandler)) { |
||
381 | throw new Exception("Connection to ".$this->dbType."failed"); |
||
382 | } |
||
383 | |||
384 | $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
385 | $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Primary function, triggers dumping. |
||
390 | * |
||
391 | * @param string $filename Name of file to write sql dump to |
||
392 | * @return null |
||
393 | * @throws \Exception |
||
394 | */ |
||
395 | public function start($filename = '') |
||
396 | { |
||
397 | // Output file can be redefined here |
||
398 | if (!empty($filename)) { |
||
399 | $this->fileName = $filename; |
||
400 | } |
||
401 | |||
402 | // Connect to database |
||
403 | $this->connect(); |
||
404 | |||
405 | // Create output file |
||
406 | $this->compressManager->open($this->fileName); |
||
407 | |||
408 | // Write some basic info to output file |
||
409 | $this->compressManager->write($this->getDumpFileHeader()); |
||
410 | |||
411 | // Store server settings and use sanner defaults to dump |
||
412 | $this->compressManager->write( |
||
413 | $this->typeAdapter->backup_parameters() |
||
414 | ); |
||
415 | |||
416 | if ($this->dumpSettings['databases']) { |
||
417 | $this->compressManager->write( |
||
418 | $this->typeAdapter->getDatabaseHeader($this->dbName) |
||
419 | ); |
||
420 | if ($this->dumpSettings['add-drop-database']) { |
||
421 | $this->compressManager->write( |
||
422 | $this->typeAdapter->add_drop_database($this->dbName) |
||
423 | ); |
||
424 | } |
||
425 | } |
||
426 | |||
427 | // Get table, view, trigger, procedures, functions and events structures from |
||
428 | // database. |
||
429 | $this->getDatabaseStructureTables(); |
||
430 | $this->getDatabaseStructureViews(); |
||
431 | $this->getDatabaseStructureTriggers(); |
||
432 | $this->getDatabaseStructureProcedures(); |
||
433 | $this->getDatabaseStructureFunctions(); |
||
434 | $this->getDatabaseStructureEvents(); |
||
435 | |||
436 | if ($this->dumpSettings['databases']) { |
||
437 | $this->compressManager->write( |
||
438 | $this->typeAdapter->databases($this->dbName) |
||
439 | ); |
||
440 | } |
||
441 | |||
442 | // If there still are some tables/views in include-tables array, |
||
443 | // that means that some tables or views weren't found. |
||
444 | // Give proper error and exit. |
||
445 | // This check will be removed once include-tables supports regexps. |
||
446 | if (0 < count($this->dumpSettings['include-tables'])) { |
||
447 | $name = implode(",", $this->dumpSettings['include-tables']); |
||
448 | throw new Exception("Table (".$name.") not found in database"); |
||
449 | } |
||
450 | |||
451 | $this->exportTables(); |
||
452 | $this->exportTriggers(); |
||
453 | $this->exportViews(); |
||
454 | $this->exportProcedures(); |
||
455 | $this->exportFunctions(); |
||
456 | $this->exportEvents(); |
||
457 | |||
458 | // Restore saved parameters. |
||
459 | $this->compressManager->write( |
||
460 | $this->typeAdapter->restore_parameters() |
||
461 | ); |
||
462 | // Write some stats to output file. |
||
463 | $this->compressManager->write($this->getDumpFileFooter()); |
||
464 | // Close output file. |
||
465 | $this->compressManager->close(); |
||
466 | |||
467 | return; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Returns header for dump file. |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | private function getDumpFileHeader() |
||
476 | { |
||
477 | $header = ''; |
||
478 | if (!$this->dumpSettings['skip-comments']) { |
||
479 | // Some info about software, source and time |
||
480 | $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
||
481 | "--".PHP_EOL. |
||
482 | "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
||
483 | "-- ------------------------------------------------------".PHP_EOL; |
||
484 | |||
485 | if (!empty($this->version)) { |
||
486 | $header .= "-- Server version \t".$this->version.PHP_EOL; |
||
487 | } |
||
488 | |||
489 | if (!$this->dumpSettings['skip-dump-date']) { |
||
490 | $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
||
491 | } |
||
492 | } |
||
493 | return $header; |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Returns footer for dump file. |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | private function getDumpFileFooter() |
||
502 | { |
||
503 | $footer = ''; |
||
504 | if (!$this->dumpSettings['skip-comments']) { |
||
505 | $footer .= '-- Dump completed'; |
||
506 | if (!$this->dumpSettings['skip-dump-date']) { |
||
507 | $footer .= ' on: '.date('r'); |
||
508 | } |
||
509 | $footer .= PHP_EOL; |
||
510 | } |
||
511 | |||
512 | return $footer; |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Reads table names from database. |
||
517 | * Fills $this->tables array so they will be dumped later. |
||
518 | * |
||
519 | * @return null |
||
520 | */ |
||
521 | private function getDatabaseStructureTables() |
||
522 | { |
||
523 | // Listing all tables from database |
||
524 | if (empty($this->dumpSettings['include-tables'])) { |
||
525 | // include all tables for now, blacklisting happens later |
||
526 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
527 | array_push($this->tables, current($row)); |
||
528 | } |
||
529 | } else { |
||
530 | // include only the tables mentioned in include-tables |
||
531 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
532 | if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
||
533 | array_push($this->tables, current($row)); |
||
534 | $elem = array_search( |
||
535 | current($row), |
||
536 | $this->dumpSettings['include-tables'] |
||
537 | ); |
||
538 | unset($this->dumpSettings['include-tables'][$elem]); |
||
539 | } |
||
540 | } |
||
541 | } |
||
542 | return; |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Reads view names from database. |
||
547 | * Fills $this->tables array so they will be dumped later. |
||
548 | * |
||
549 | * @return null |
||
550 | */ |
||
551 | private function getDatabaseStructureViews() |
||
552 | { |
||
553 | // Listing all views from database |
||
554 | if (empty($this->dumpSettings['include-views'])) { |
||
555 | // include all views for now, blacklisting happens later |
||
556 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
557 | array_push($this->views, current($row)); |
||
558 | } |
||
559 | } else { |
||
560 | // include only the tables mentioned in include-tables |
||
561 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
562 | if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
||
563 | array_push($this->views, current($row)); |
||
564 | $elem = array_search( |
||
565 | current($row), |
||
566 | $this->dumpSettings['include-views'] |
||
567 | ); |
||
568 | unset($this->dumpSettings['include-views'][$elem]); |
||
569 | } |
||
570 | } |
||
571 | } |
||
572 | return; |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Reads trigger names from database. |
||
577 | * Fills $this->tables array so they will be dumped later. |
||
578 | * |
||
579 | * @return null |
||
580 | */ |
||
581 | private function getDatabaseStructureTriggers() |
||
582 | { |
||
583 | // Listing all triggers from database |
||
584 | if (false === $this->dumpSettings['skip-triggers']) { |
||
585 | foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { |
||
586 | array_push($this->triggers, $row['Trigger']); |
||
587 | } |
||
588 | } |
||
589 | return; |
||
590 | } |
||
591 | |||
592 | /** |
||
593 | * Reads procedure names from database. |
||
594 | * Fills $this->tables array so they will be dumped later. |
||
595 | * |
||
596 | * @return null |
||
597 | */ |
||
598 | private function getDatabaseStructureProcedures() |
||
599 | { |
||
600 | // Listing all procedures from database |
||
601 | if ($this->dumpSettings['routines']) { |
||
602 | foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { |
||
603 | array_push($this->procedures, $row['procedure_name']); |
||
604 | } |
||
605 | } |
||
606 | return; |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * Reads functions names from database. |
||
611 | * Fills $this->tables array so they will be dumped later. |
||
612 | * |
||
613 | * @return null |
||
614 | */ |
||
615 | private function getDatabaseStructureFunctions() |
||
616 | { |
||
617 | // Listing all functions from database |
||
618 | if ($this->dumpSettings['routines']) { |
||
619 | foreach ($this->dbHandler->query($this->typeAdapter->show_functions($this->dbName)) as $row) { |
||
620 | array_push($this->functions, $row['function_name']); |
||
621 | } |
||
622 | } |
||
623 | return; |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Reads event names from database. |
||
628 | * Fills $this->tables array so they will be dumped later. |
||
629 | * |
||
630 | * @return null |
||
631 | */ |
||
632 | private function getDatabaseStructureEvents() |
||
633 | { |
||
634 | // Listing all events from database |
||
635 | if ($this->dumpSettings['events']) { |
||
636 | foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { |
||
637 | array_push($this->events, $row['event_name']); |
||
638 | } |
||
639 | } |
||
640 | return; |
||
641 | } |
||
642 | |||
643 | /** |
||
644 | * Compare if $table name matches with a definition inside $arr |
||
645 | * @param $table string |
||
646 | * @param $arr array with strings or patterns |
||
647 | * @return boolean |
||
648 | */ |
||
649 | private function matches($table, $arr) |
||
650 | { |
||
651 | $match = false; |
||
652 | |||
653 | foreach ($arr as $pattern) { |
||
654 | if ('/' != $pattern[0]) { |
||
655 | continue; |
||
656 | } |
||
657 | if (1 == preg_match($pattern, $table)) { |
||
658 | $match = true; |
||
659 | } |
||
660 | } |
||
661 | |||
662 | return in_array($table, $arr) || $match; |
||
663 | } |
||
664 | |||
665 | /** |
||
666 | * Exports all the tables selected from database |
||
667 | * |
||
668 | * @return null |
||
669 | */ |
||
670 | private function exportTables() |
||
671 | { |
||
672 | // Exporting tables one by one |
||
673 | foreach ($this->tables as $table) { |
||
674 | if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { |
||
675 | continue; |
||
676 | } |
||
677 | $this->getTableStructure($table); |
||
678 | if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger |
||
679 | $this->listValues($table); |
||
680 | } elseif (true === $this->dumpSettings['no-data'] |
||
681 | || $this->matches($table, $this->dumpSettings['no-data'])) { |
||
682 | continue; |
||
683 | } else { |
||
684 | $this->listValues($table); |
||
685 | } |
||
686 | } |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * Exports all the views found in database |
||
691 | * |
||
692 | * @return null |
||
693 | */ |
||
694 | private function exportViews() |
||
695 | { |
||
696 | if (false === $this->dumpSettings['no-create-info']) { |
||
697 | // Exporting views one by one |
||
698 | foreach ($this->views as $view) { |
||
699 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
700 | continue; |
||
701 | } |
||
702 | $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); |
||
703 | $this->getViewStructureTable($view); |
||
704 | } |
||
705 | foreach ($this->views as $view) { |
||
706 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
707 | continue; |
||
708 | } |
||
709 | $this->getViewStructureView($view); |
||
710 | } |
||
711 | } |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * Exports all the triggers found in database |
||
716 | * |
||
717 | * @return null |
||
718 | */ |
||
719 | private function exportTriggers() |
||
720 | { |
||
721 | // Exporting triggers one by one |
||
722 | foreach ($this->triggers as $trigger) { |
||
723 | $this->getTriggerStructure($trigger); |
||
724 | } |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * Exports all the procedures found in database |
||
729 | * |
||
730 | * @return null |
||
731 | */ |
||
732 | private function exportProcedures() |
||
733 | { |
||
734 | // Exporting triggers one by one |
||
735 | foreach ($this->procedures as $procedure) { |
||
736 | $this->getProcedureStructure($procedure); |
||
737 | } |
||
738 | } |
||
739 | |||
740 | /** |
||
741 | * Exports all the functions found in database |
||
742 | * |
||
743 | * @return null |
||
744 | */ |
||
745 | private function exportFunctions() |
||
746 | { |
||
747 | // Exporting triggers one by one |
||
748 | foreach ($this->functions as $function) { |
||
749 | $this->getFunctionStructure($function); |
||
750 | } |
||
751 | } |
||
752 | |||
753 | /** |
||
754 | * Exports all the events found in database |
||
755 | * |
||
756 | * @return null |
||
757 | */ |
||
758 | private function exportEvents() |
||
759 | { |
||
760 | // Exporting triggers one by one |
||
761 | foreach ($this->events as $event) { |
||
762 | $this->getEventStructure($event); |
||
763 | } |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * Table structure extractor |
||
768 | * |
||
769 | * @todo move specific mysql code to typeAdapter |
||
770 | * @param string $tableName Name of table to export |
||
771 | * @return null |
||
772 | */ |
||
773 | private function getTableStructure($tableName) |
||
774 | { |
||
775 | if (!$this->dumpSettings['no-create-info']) { |
||
776 | $ret = ''; |
||
777 | if (!$this->dumpSettings['skip-comments']) { |
||
778 | $ret = "--".PHP_EOL. |
||
779 | "-- Table structure for table `$tableName`".PHP_EOL. |
||
780 | "--".PHP_EOL.PHP_EOL; |
||
781 | } |
||
782 | $stmt = $this->typeAdapter->show_create_table($tableName); |
||
783 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
784 | $this->compressManager->write($ret); |
||
785 | if ($this->dumpSettings['add-drop-table']) { |
||
786 | $this->compressManager->write( |
||
787 | $this->typeAdapter->drop_table($tableName) |
||
788 | ); |
||
789 | } |
||
790 | $this->compressManager->write( |
||
791 | $this->typeAdapter->create_table($r) |
||
792 | ); |
||
793 | break; |
||
794 | } |
||
795 | } |
||
796 | $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); |
||
797 | return; |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * Store column types to create data dumps and for Stand-In tables |
||
802 | * |
||
803 | * @param string $tableName Name of table to export |
||
804 | * @return array type column types detailed |
||
805 | */ |
||
806 | |||
807 | private function getTableColumnTypes($tableName) |
||
808 | { |
||
809 | $columnTypes = array(); |
||
810 | $columns = $this->dbHandler->query( |
||
811 | $this->typeAdapter->show_columns($tableName) |
||
812 | ); |
||
813 | $columns->setFetchMode(PDO::FETCH_ASSOC); |
||
814 | |||
815 | foreach ($columns as $key => $col) { |
||
816 | $types = $this->typeAdapter->parseColumnType($col); |
||
817 | $columnTypes[$col['Field']] = array( |
||
818 | 'is_numeric'=> $types['is_numeric'], |
||
819 | 'is_blob' => $types['is_blob'], |
||
820 | 'type' => $types['type'], |
||
821 | 'type_sql' => $col['Type'], |
||
822 | 'is_virtual' => $types['is_virtual'] |
||
823 | ); |
||
824 | } |
||
825 | |||
826 | return $columnTypes; |
||
827 | } |
||
828 | |||
829 | /** |
||
830 | * View structure extractor, create table (avoids cyclic references) |
||
831 | * |
||
832 | * @todo move mysql specific code to typeAdapter |
||
833 | * @param string $viewName Name of view to export |
||
834 | * @return null |
||
835 | */ |
||
836 | private function getViewStructureTable($viewName) |
||
837 | { |
||
838 | if (!$this->dumpSettings['skip-comments']) { |
||
839 | $ret = "--".PHP_EOL. |
||
840 | "-- Stand-In structure for view `${viewName}`".PHP_EOL. |
||
841 | "--".PHP_EOL.PHP_EOL; |
||
842 | $this->compressManager->write($ret); |
||
843 | } |
||
844 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
845 | |||
846 | // create views as tables, to resolve dependencies |
||
847 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
848 | if ($this->dumpSettings['add-drop-table']) { |
||
849 | $this->compressManager->write( |
||
850 | $this->typeAdapter->drop_view($viewName) |
||
851 | ); |
||
852 | } |
||
853 | |||
854 | $this->compressManager->write( |
||
855 | $this->createStandInTable($viewName) |
||
856 | ); |
||
857 | break; |
||
858 | } |
||
859 | } |
||
860 | |||
861 | /** |
||
862 | * Write a create table statement for the table Stand-In, show create |
||
863 | * table would return a create algorithm when used on a view |
||
864 | * |
||
865 | * @param string $viewName Name of view to export |
||
866 | * @return string create statement |
||
867 | */ |
||
868 | public function createStandInTable($viewName) |
||
869 | { |
||
870 | $ret = array(); |
||
871 | foreach ($this->tableColumnTypes[$viewName] as $k => $v) { |
||
872 | $ret[] = "`${k}` ${v['type_sql']}"; |
||
873 | } |
||
874 | $ret = implode(PHP_EOL.",", $ret); |
||
875 | |||
876 | $ret = "CREATE TABLE IF NOT EXISTS `$viewName` (". |
||
877 | PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; |
||
878 | |||
879 | return $ret; |
||
880 | } |
||
881 | |||
882 | /** |
||
883 | * View structure extractor, create view |
||
884 | * |
||
885 | * @todo move mysql specific code to typeAdapter |
||
886 | * @param string $viewName Name of view to export |
||
887 | * @return null |
||
888 | */ |
||
889 | private function getViewStructureView($viewName) |
||
890 | { |
||
891 | if (!$this->dumpSettings['skip-comments']) { |
||
892 | $ret = "--".PHP_EOL. |
||
893 | "-- View structure for view `${viewName}`".PHP_EOL. |
||
894 | "--".PHP_EOL.PHP_EOL; |
||
895 | $this->compressManager->write($ret); |
||
896 | } |
||
897 | $stmt = $this->typeAdapter->show_create_view($viewName); |
||
898 | |||
899 | // create views, to resolve dependencies |
||
900 | // replacing tables with views |
||
901 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
902 | // because we must replace table with view, we should delete it |
||
903 | $this->compressManager->write( |
||
904 | $this->typeAdapter->drop_view($viewName) |
||
905 | ); |
||
906 | $this->compressManager->write( |
||
907 | $this->typeAdapter->create_view($r) |
||
908 | ); |
||
909 | break; |
||
910 | } |
||
911 | } |
||
912 | |||
913 | /** |
||
914 | * Trigger structure extractor |
||
915 | * |
||
916 | * @param string $triggerName Name of trigger to export |
||
917 | * @return null |
||
918 | */ |
||
919 | private function getTriggerStructure($triggerName) |
||
920 | { |
||
921 | $stmt = $this->typeAdapter->show_create_trigger($triggerName); |
||
922 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
923 | if ($this->dumpSettings['add-drop-trigger']) { |
||
924 | $this->compressManager->write( |
||
925 | $this->typeAdapter->add_drop_trigger($triggerName) |
||
926 | ); |
||
927 | } |
||
928 | $this->compressManager->write( |
||
929 | $this->typeAdapter->create_trigger($r) |
||
930 | ); |
||
931 | return; |
||
932 | } |
||
933 | } |
||
934 | |||
935 | /** |
||
936 | * Procedure structure extractor |
||
937 | * |
||
938 | * @param string $procedureName Name of procedure to export |
||
939 | * @return null |
||
940 | */ |
||
941 | private function getProcedureStructure($procedureName) |
||
942 | { |
||
943 | if (!$this->dumpSettings['skip-comments']) { |
||
944 | $ret = "--".PHP_EOL. |
||
945 | "-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
||
946 | "--".PHP_EOL.PHP_EOL; |
||
947 | $this->compressManager->write($ret); |
||
948 | } |
||
949 | $stmt = $this->typeAdapter->show_create_procedure($procedureName); |
||
950 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
951 | $this->compressManager->write( |
||
952 | $this->typeAdapter->create_procedure($r) |
||
953 | ); |
||
954 | return; |
||
955 | } |
||
956 | } |
||
957 | |||
958 | /** |
||
959 | * Function structure extractor |
||
960 | * |
||
961 | * @param string $functionName Name of function to export |
||
962 | * @return null |
||
963 | */ |
||
964 | private function getFunctionStructure($functionName) |
||
978 | } |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * Event structure extractor |
||
983 | * |
||
984 | * @param string $eventName Name of event to export |
||
985 | * @return null |
||
986 | */ |
||
987 | private function getEventStructure($eventName) |
||
988 | { |
||
989 | if (!$this->dumpSettings['skip-comments']) { |
||
990 | $ret = "--".PHP_EOL. |
||
991 | "-- Dumping events for database '".$this->dbName."'".PHP_EOL. |
||
992 | "--".PHP_EOL.PHP_EOL; |
||
993 | $this->compressManager->write($ret); |
||
994 | } |
||
995 | $stmt = $this->typeAdapter->show_create_event($eventName); |
||
996 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
997 | $this->compressManager->write( |
||
998 | $this->typeAdapter->create_event($r) |
||
999 | ); |
||
1000 | return; |
||
1001 | } |
||
1002 | } |
||
1003 | |||
1004 | /** |
||
1005 | * Prepare values for output |
||
1006 | * |
||
1007 | * @param string $tableName Name of table which contains rows |
||
1008 | * @param array $row Associative array of column names and values to be |
||
1009 | * quoted |
||
1010 | * |
||
1011 | * @return array |
||
1012 | */ |
||
1013 | private function prepareColumnValues($tableName, $row) |
||
1023 | } |
||
1024 | |||
1025 | /** |
||
1026 | * Escape values with quotes when needed |
||
1027 | * |
||
1028 | * @param string $tableName Name of table which contains rows |
||
1029 | * @param array $row Associative array of column names and values to be quoted |
||
1030 | * |
||
1031 | * @return string |
||
1032 | */ |
||
1033 | private function escape($colValue, $colType) |
||
1034 | { |
||
1035 | if (is_null($colValue)) { |
||
1036 | return "NULL"; |
||
1037 | } elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) { |
||
1038 | if ($colType['type'] == 'bit' || !empty($colValue)) { |
||
1039 | return "0x${colValue}"; |
||
1040 | } else { |
||
1041 | return "''"; |
||
1042 | } |
||
1043 | } elseif ($colType['is_numeric']) { |
||
1044 | return $colValue; |
||
1045 | } |
||
1046 | |||
1047 | return $this->dbHandler->quote($colValue); |
||
1048 | } |
||
1049 | |||
1050 | /** |
||
1051 | * Set a callable that will will be used to transform column values. |
||
1052 | * |
||
1053 | * @param callable $callable |
||
1054 | * |
||
1055 | * @return void |
||
1056 | */ |
||
1057 | public function setTransformColumnValueHook($callable) |
||
1058 | { |
||
1059 | $this->transformColumnValueCallable = $callable; |
||
1060 | } |
||
1061 | |||
1062 | /** |
||
1063 | * Give extending classes an opportunity to transform column values |
||
1064 | * |
||
1065 | * @param string $tableName Name of table which contains rows |
||
1066 | * @param string $colName Name of the column in question |
||
1067 | * @param string $colValue Value of the column in question |
||
1068 | * |
||
1069 | * @return string |
||
1070 | */ |
||
1071 | protected function hookTransformColumnValue($tableName, $colName, $colValue, $row) |
||
1072 | { |
||
1073 | if (!$this->transformColumnValueCallable) { |
||
1074 | return $colValue; |
||
1075 | } |
||
1076 | |||
1077 | return call_user_func_array($this->transformColumnValueCallable, array( |
||
1078 | $tableName, |
||
1079 | $colName, |
||
1080 | $colValue, |
||
1081 | $row |
||
1082 | )); |
||
1083 | } |
||
1084 | |||
1085 | /** |
||
1086 | * Table rows extractor |
||
1087 | * |
||
1088 | * @param string $tableName Name of table to export |
||
1089 | * |
||
1090 | * @return null |
||
1091 | */ |
||
1092 | private function listValues($tableName) |
||
1159 | } |
||
1160 | |||
1161 | /** |
||
1162 | * Table rows extractor, append information prior to dump |
||
1163 | * |
||
1164 | * @param string $tableName Name of table to export |
||
1165 | * |
||
1166 | * @return null |
||
1167 | */ |
||
1168 | public function prepareListValues($tableName) |
||
1169 | { |
||
1170 | if (!$this->dumpSettings['skip-comments']) { |
||
1171 | $this->compressManager->write( |
||
1172 | "--".PHP_EOL. |
||
1173 | "-- Dumping data for table `$tableName`".PHP_EOL. |
||
1174 | "--".PHP_EOL.PHP_EOL |
||
1175 | ); |
||
1176 | } |
||
1177 | |||
1178 | if ($this->dumpSettings['single-transaction']) { |
||
1179 | $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
||
1180 | $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
||
1181 | } |
||
1182 | |||
1183 | if ($this->dumpSettings['lock-tables']) { |
||
1184 | $this->typeAdapter->lock_table($tableName); |
||
1185 | } |
||
1186 | |||
1187 | if ($this->dumpSettings['add-locks']) { |
||
1188 | $this->compressManager->write( |
||
1189 | $this->typeAdapter->start_add_lock_table($tableName) |
||
1190 | ); |
||
1191 | } |
||
1192 | |||
1193 | if ($this->dumpSettings['disable-keys']) { |
||
1194 | $this->compressManager->write( |
||
1195 | $this->typeAdapter->start_add_disable_keys($tableName) |
||
1196 | ); |
||
1197 | } |
||
1198 | |||
1199 | // Disable autocommit for faster reload |
||
1200 | if ($this->dumpSettings['no-autocommit']) { |
||
1201 | $this->compressManager->write( |
||
1202 | $this->typeAdapter->start_disable_autocommit() |
||
1203 | ); |
||
1204 | } |
||
1205 | |||
1206 | return; |
||
1207 | } |
||
1208 | |||
1209 | /** |
||
1210 | * Table rows extractor, close locks and commits after dump |
||
1211 | * |
||
1212 | * @param string $tableName Name of table to export. |
||
1213 | * @param integer $count Number of rows inserted. |
||
1214 | * |
||
1215 | * @return void |
||
1216 | */ |
||
1217 | public function endListValues($tableName, $count = 0) |
||
1218 | { |
||
1219 | if ($this->dumpSettings['disable-keys']) { |
||
1220 | $this->compressManager->write( |
||
1221 | $this->typeAdapter->end_add_disable_keys($tableName) |
||
1222 | ); |
||
1223 | } |
||
1224 | |||
1225 | if ($this->dumpSettings['add-locks']) { |
||
1226 | $this->compressManager->write( |
||
1227 | $this->typeAdapter->end_add_lock_table($tableName) |
||
1228 | ); |
||
1229 | } |
||
1230 | |||
1231 | if ($this->dumpSettings['single-transaction']) { |
||
1232 | $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
||
1233 | } |
||
1234 | |||
1235 | if ($this->dumpSettings['lock-tables']) { |
||
1236 | $this->typeAdapter->unlock_table($tableName); |
||
1237 | } |
||
1238 | |||
1239 | // Commit to enable autocommit |
||
1240 | if ($this->dumpSettings['no-autocommit']) { |
||
1241 | $this->compressManager->write( |
||
1242 | $this->typeAdapter->end_disable_autocommit() |
||
1243 | ); |
||
1244 | } |
||
1245 | |||
1246 | $this->compressManager->write(PHP_EOL); |
||
1247 | |||
1248 | if (!$this->dumpSettings['skip-comments']) { |
||
1249 | $this->compressManager->write( |
||
1250 | "-- Dumped table `".$tableName."` with $count row(s)".PHP_EOL. |
||
1251 | '--'.PHP_EOL.PHP_EOL |
||
1252 | ); |
||
1253 | } |
||
1254 | |||
1255 | return; |
||
1256 | } |
||
1257 | |||
1258 | /** |
||
1259 | * Build SQL List of all columns on current table which will be used for selecting |
||
1260 | * |
||
1261 | * @param string $tableName Name of table to get columns |
||
1262 | * |
||
1263 | * @return array SQL sentence with columns for select |
||
1264 | */ |
||
1265 | public function getColumnStmt($tableName) |
||
1266 | { |
||
1267 | $colStmt = array(); |
||
1268 | foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
||
1269 | if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
||
1270 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
||
1271 | } elseif ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
||
1272 | $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
||
1273 | } elseif ($colType['is_virtual']) { |
||
1274 | $this->dumpSettings['complete-insert'] = true; |
||
1275 | continue; |
||
1276 | } else { |
||
1277 | $colStmt[] = "`${colName}`"; |
||
1278 | } |
||
1279 | } |
||
1280 | |||
1281 | return $colStmt; |
||
1282 | } |
||
1283 | |||
1284 | /** |
||
1285 | * Build SQL List of all columns on current table which will be used for inserting |
||
1286 | * |
||
1287 | * @param string $tableName Name of table to get columns |
||
1288 | * |
||
1289 | * @return array columns for sql sentence for insert |
||
1290 | */ |
||
1291 | public function getColumnNames($tableName) |
||
1303 | } |
||
1304 | } |
||
1305 | |||
1306 | /** |
||
1307 | * Enum with all available compression methods |
||
1308 | * |
||
1309 | */ |
||
1310 | abstract class CompressMethod |
||
1311 | { |
||
1312 | public static $enums = array( |
||
1313 | Mysqldump::NONE, |
||
1314 | Mysqldump::GZIP, |
||
1315 | Mysqldump::BZIP2, |
||
1316 | ); |
||
1317 | |||
2238 |