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