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