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