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