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