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