Total Complexity | 185 |
Total Lines | 1315 |
Duplicated Lines | 0 % |
Changes | 87 | ||
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 | 'replace' => false, |
||
162 | 'routines' => false, |
||
163 | 'single-transaction' => true, |
||
164 | 'skip-triggers' => false, |
||
165 | 'skip-tz-utc' => false, |
||
166 | 'skip-comments' => false, |
||
167 | 'skip-dump-date' => false, |
||
168 | 'skip-definer' => false, |
||
169 | 'where' => '', |
||
170 | /* deprecated */ |
||
171 | 'disable-foreign-keys-check' => true |
||
172 | ); |
||
173 | |||
174 | $pdoSettingsDefault = array( |
||
175 | PDO::ATTR_PERSISTENT => true, |
||
176 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
177 | ); |
||
178 | |||
179 | $this->user = $user; |
||
180 | $this->pass = $pass; |
||
181 | $this->parseDsn($dsn); |
||
182 | |||
183 | // This drops MYSQL dependency, only use the constant if it's defined. |
||
184 | if ("mysql" === $this->dbType) { |
||
185 | $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
||
186 | } |
||
187 | |||
188 | $this->pdoSettings = array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
||
189 | $this->dumpSettings = array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
||
190 | $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
||
191 | |||
192 | if (false === $this->dumpSettings['skip-tz-utc']) { |
||
193 | $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
||
194 | } |
||
195 | |||
196 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
||
197 | if (count($diff) > 0) { |
||
198 | throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
||
199 | } |
||
200 | |||
201 | if (!is_array($this->dumpSettings['include-tables']) || |
||
202 | !is_array($this->dumpSettings['exclude-tables'])) { |
||
203 | throw new Exception("Include-tables and exclude-tables should be arrays"); |
||
204 | } |
||
205 | |||
206 | // If no include-views is passed in, dump the same views as tables, mimic mysqldump behaviour. |
||
207 | if (!isset($dumpSettings['include-views'])) { |
||
208 | $this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
||
209 | } |
||
210 | |||
211 | // Create a new compressManager to manage compressed output |
||
212 | $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Destructor of Mysqldump. Unsets dbHandlers and database objects. |
||
217 | */ |
||
218 | public function __destruct() |
||
219 | { |
||
220 | $this->dbHandler = null; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Keyed by table name, with the value as the conditions: |
||
225 | * e.g. 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH AND deleted=0' |
||
226 | * |
||
227 | * @param array $tableWheres |
||
228 | */ |
||
229 | public function setTableWheres(array $tableWheres) |
||
230 | { |
||
231 | $this->tableWheres = $tableWheres; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param $tableName |
||
236 | * |
||
237 | * @return boolean|mixed |
||
238 | */ |
||
239 | public function getTableWhere($tableName) |
||
240 | { |
||
241 | if (!empty($this->tableWheres[$tableName])) { |
||
242 | return $this->tableWheres[$tableName]; |
||
243 | } elseif ($this->dumpSettings['where']) { |
||
244 | return $this->dumpSettings['where']; |
||
245 | } |
||
246 | |||
247 | return false; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Keyed by table name, with the value as the numeric limit: |
||
252 | * e.g. 'users' => 3000 |
||
253 | * |
||
254 | * @param array $tableLimits |
||
255 | */ |
||
256 | public function setTableLimits(array $tableLimits) |
||
257 | { |
||
258 | $this->tableLimits = $tableLimits; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Returns the LIMIT for the table. Must be numeric to be returned. |
||
263 | * @param $tableName |
||
264 | * @return boolean |
||
265 | */ |
||
266 | public function getTableLimit($tableName) |
||
267 | { |
||
268 | if (!isset($this->tableLimits[$tableName])) { |
||
269 | return false; |
||
270 | } |
||
271 | |||
272 | $limit = $this->tableLimits[$tableName]; |
||
273 | if (!is_numeric($limit)) { |
||
274 | return false; |
||
275 | } |
||
276 | |||
277 | return $limit; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Parse DSN string and extract dbname value |
||
282 | * Several examples of a DSN string |
||
283 | * mysql:host=localhost;dbname=testdb |
||
284 | * mysql:host=localhost;port=3307;dbname=testdb |
||
285 | * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
||
286 | * |
||
287 | * @param string $dsn dsn string to parse |
||
288 | * @return boolean |
||
289 | */ |
||
290 | private function parseDsn($dsn) |
||
291 | { |
||
292 | if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) { |
||
293 | throw new Exception("Empty DSN string"); |
||
294 | } |
||
295 | |||
296 | $this->dsn = $dsn; |
||
297 | $this->dbType = strtolower(substr($dsn, 0, $pos)); // always returns a string |
||
298 | |||
299 | if (empty($this->dbType)) { |
||
300 | throw new Exception("Missing database type from DSN string"); |
||
301 | } |
||
302 | |||
303 | $dsn = substr($dsn, $pos + 1); |
||
304 | |||
305 | foreach (explode(";", $dsn) as $kvp) { |
||
306 | $kvpArr = explode("=", $kvp); |
||
307 | $this->dsnArray[strtolower($kvpArr[0])] = $kvpArr[1]; |
||
308 | } |
||
309 | |||
310 | if (empty($this->dsnArray['host']) && |
||
311 | empty($this->dsnArray['unix_socket'])) { |
||
312 | throw new Exception("Missing host from DSN string"); |
||
313 | } |
||
314 | $this->host = (!empty($this->dsnArray['host'])) ? |
||
315 | $this->dsnArray['host'] : $this->dsnArray['unix_socket']; |
||
316 | |||
317 | if (empty($this->dsnArray['dbname'])) { |
||
318 | throw new Exception("Missing database name from DSN string"); |
||
319 | } |
||
320 | |||
321 | $this->dbName = $this->dsnArray['dbname']; |
||
322 | |||
323 | return true; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Connect with PDO. |
||
328 | * |
||
329 | * @return null |
||
330 | */ |
||
331 | private function connect() |
||
332 | { |
||
333 | // Connecting with PDO. |
||
334 | try { |
||
335 | switch ($this->dbType) { |
||
336 | case 'sqlite': |
||
337 | $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
||
338 | break; |
||
339 | case 'mysql': |
||
340 | case 'pgsql': |
||
341 | case 'dblib': |
||
342 | $this->dbHandler = @new PDO( |
||
343 | $this->dsn, |
||
344 | $this->user, |
||
345 | $this->pass, |
||
346 | $this->pdoSettings |
||
347 | ); |
||
348 | // Execute init commands once connected |
||
349 | foreach ($this->dumpSettings['init_commands'] as $stmt) { |
||
350 | $this->dbHandler->exec($stmt); |
||
351 | } |
||
352 | // Store server version |
||
353 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
354 | break; |
||
355 | default: |
||
356 | throw new Exception("Unsupported database type (".$this->dbType.")"); |
||
357 | } |
||
358 | } catch (PDOException $e) { |
||
359 | throw new Exception( |
||
360 | "Connection to ".$this->dbType." failed with message: ". |
||
361 | $e->getMessage() |
||
362 | ); |
||
363 | } |
||
364 | |||
365 | if (is_null($this->dbHandler)) { |
||
366 | throw new Exception("Connection to ".$this->dbType."failed"); |
||
367 | } |
||
368 | |||
369 | $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
||
370 | $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Primary function, triggers dumping. |
||
375 | * |
||
376 | * @param string $filename Name of file to write sql dump to |
||
377 | * @return null |
||
378 | * @throws \Exception |
||
379 | */ |
||
380 | public function start($filename = '') |
||
381 | { |
||
382 | // Output file can be redefined here |
||
383 | if (!empty($filename)) { |
||
384 | $this->fileName = $filename; |
||
385 | } |
||
386 | |||
387 | // Connect to database |
||
388 | $this->connect(); |
||
389 | |||
390 | // Create output file |
||
391 | $this->compressManager->open($this->fileName); |
||
392 | |||
393 | // Write some basic info to output file |
||
394 | $this->compressManager->write($this->getDumpFileHeader()); |
||
395 | |||
396 | // Store server settings and use sanner defaults to dump |
||
397 | $this->compressManager->write( |
||
398 | $this->typeAdapter->backup_parameters() |
||
399 | ); |
||
400 | |||
401 | if ($this->dumpSettings['databases']) { |
||
402 | $this->compressManager->write( |
||
403 | $this->typeAdapter->getDatabaseHeader($this->dbName) |
||
404 | ); |
||
405 | if ($this->dumpSettings['add-drop-database']) { |
||
406 | $this->compressManager->write( |
||
407 | $this->typeAdapter->add_drop_database($this->dbName) |
||
408 | ); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | // Get table, view, trigger, procedures, functions and events structures from |
||
413 | // database. |
||
414 | $this->getDatabaseStructureTables(); |
||
415 | $this->getDatabaseStructureViews(); |
||
416 | $this->getDatabaseStructureTriggers(); |
||
417 | $this->getDatabaseStructureProcedures(); |
||
418 | $this->getDatabaseStructureFunctions(); |
||
419 | $this->getDatabaseStructureEvents(); |
||
420 | |||
421 | if ($this->dumpSettings['databases']) { |
||
422 | $this->compressManager->write( |
||
423 | $this->typeAdapter->databases($this->dbName) |
||
424 | ); |
||
425 | } |
||
426 | |||
427 | // If there still are some tables/views in include-tables array, |
||
428 | // that means that some tables or views weren't found. |
||
429 | // Give proper error and exit. |
||
430 | // This check will be removed once include-tables supports regexps. |
||
431 | if (0 < count($this->dumpSettings['include-tables'])) { |
||
432 | $name = implode(",", $this->dumpSettings['include-tables']); |
||
433 | throw new Exception("Table (".$name.") not found in database"); |
||
434 | } |
||
435 | |||
436 | $this->exportTables(); |
||
437 | $this->exportTriggers(); |
||
438 | $this->exportFunctions(); |
||
439 | $this->exportProcedures(); |
||
440 | $this->exportViews(); |
||
441 | $this->exportEvents(); |
||
442 | |||
443 | // Restore saved parameters. |
||
444 | $this->compressManager->write( |
||
445 | $this->typeAdapter->restore_parameters() |
||
446 | ); |
||
447 | // Write some stats to output file. |
||
448 | $this->compressManager->write($this->getDumpFileFooter()); |
||
449 | // Close output file. |
||
450 | $this->compressManager->close(); |
||
451 | |||
452 | return; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Returns header for dump file. |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | private function getDumpFileHeader() |
||
461 | { |
||
462 | $header = ''; |
||
463 | if (!$this->dumpSettings['skip-comments']) { |
||
464 | // Some info about software, source and time |
||
465 | $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
||
466 | "--".PHP_EOL. |
||
467 | "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
||
468 | "-- ------------------------------------------------------".PHP_EOL; |
||
469 | |||
470 | if (!empty($this->version)) { |
||
471 | $header .= "-- Server version \t".$this->version.PHP_EOL; |
||
472 | } |
||
473 | |||
474 | if (!$this->dumpSettings['skip-dump-date']) { |
||
475 | $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
||
476 | } |
||
477 | } |
||
478 | return $header; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Returns footer for dump file. |
||
483 | * |
||
484 | * @return string |
||
485 | */ |
||
486 | private function getDumpFileFooter() |
||
487 | { |
||
488 | $footer = ''; |
||
489 | if (!$this->dumpSettings['skip-comments']) { |
||
490 | $footer .= '-- Dump completed'; |
||
491 | if (!$this->dumpSettings['skip-dump-date']) { |
||
492 | $footer .= ' on: '.date('r'); |
||
493 | } |
||
494 | $footer .= PHP_EOL; |
||
495 | } |
||
496 | |||
497 | return $footer; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Reads table names from database. |
||
502 | * Fills $this->tables array so they will be dumped later. |
||
503 | * |
||
504 | * @return null |
||
505 | */ |
||
506 | private function getDatabaseStructureTables() |
||
507 | { |
||
508 | // Listing all tables from database |
||
509 | if (empty($this->dumpSettings['include-tables'])) { |
||
510 | // include all tables for now, blacklisting happens later |
||
511 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
512 | array_push($this->tables, current($row)); |
||
513 | } |
||
514 | } else { |
||
515 | // include only the tables mentioned in include-tables |
||
516 | foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
||
517 | if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
||
518 | array_push($this->tables, current($row)); |
||
519 | $elem = array_search( |
||
520 | current($row), |
||
521 | $this->dumpSettings['include-tables'] |
||
522 | ); |
||
523 | unset($this->dumpSettings['include-tables'][$elem]); |
||
524 | } |
||
525 | } |
||
526 | } |
||
527 | return; |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Reads view names from database. |
||
532 | * Fills $this->tables array so they will be dumped later. |
||
533 | * |
||
534 | * @return null |
||
535 | */ |
||
536 | private function getDatabaseStructureViews() |
||
537 | { |
||
538 | // Listing all views from database |
||
539 | if (empty($this->dumpSettings['include-views'])) { |
||
540 | // include all views for now, blacklisting happens later |
||
541 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
542 | array_push($this->views, current($row)); |
||
543 | } |
||
544 | } else { |
||
545 | // include only the tables mentioned in include-tables |
||
546 | foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
||
547 | if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
||
548 | array_push($this->views, current($row)); |
||
549 | $elem = array_search( |
||
550 | current($row), |
||
551 | $this->dumpSettings['include-views'] |
||
552 | ); |
||
553 | unset($this->dumpSettings['include-views'][$elem]); |
||
554 | } |
||
555 | } |
||
556 | } |
||
557 | return; |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * Reads trigger names from database. |
||
562 | * Fills $this->tables array so they will be dumped later. |
||
563 | * |
||
564 | * @return null |
||
565 | */ |
||
566 | private function getDatabaseStructureTriggers() |
||
567 | { |
||
568 | // Listing all triggers from database |
||
569 | if (false === $this->dumpSettings['skip-triggers']) { |
||
570 | foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { |
||
571 | array_push($this->triggers, $row['Trigger']); |
||
572 | } |
||
573 | } |
||
574 | return; |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Reads procedure names from database. |
||
579 | * Fills $this->tables array so they will be dumped later. |
||
580 | * |
||
581 | * @return null |
||
582 | */ |
||
583 | private function getDatabaseStructureProcedures() |
||
584 | { |
||
585 | // Listing all procedures from database |
||
586 | if ($this->dumpSettings['routines']) { |
||
587 | foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { |
||
588 | array_push($this->procedures, $row['procedure_name']); |
||
589 | } |
||
590 | } |
||
591 | return; |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * Reads functions names from database. |
||
596 | * Fills $this->tables array so they will be dumped later. |
||
597 | * |
||
598 | * @return null |
||
599 | */ |
||
600 | private function getDatabaseStructureFunctions() |
||
601 | { |
||
602 | // Listing all functions from database |
||
603 | if ($this->dumpSettings['routines']) { |
||
604 | foreach ($this->dbHandler->query($this->typeAdapter->show_functions($this->dbName)) as $row) { |
||
605 | array_push($this->functions, $row['function_name']); |
||
606 | } |
||
607 | } |
||
608 | return; |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * Reads event names from database. |
||
613 | * Fills $this->tables array so they will be dumped later. |
||
614 | * |
||
615 | * @return null |
||
616 | */ |
||
617 | private function getDatabaseStructureEvents() |
||
618 | { |
||
619 | // Listing all events from database |
||
620 | if ($this->dumpSettings['events']) { |
||
621 | foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { |
||
622 | array_push($this->events, $row['event_name']); |
||
623 | } |
||
624 | } |
||
625 | return; |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * Compare if $table name matches with a definition inside $arr |
||
630 | * @param $table string |
||
631 | * @param $arr array with strings or patterns |
||
632 | * @return boolean |
||
633 | */ |
||
634 | private function matches($table, $arr) |
||
635 | { |
||
636 | $match = false; |
||
637 | |||
638 | foreach ($arr as $pattern) { |
||
639 | if ('/' != $pattern[0]) { |
||
640 | continue; |
||
641 | } |
||
642 | if (1 == preg_match($pattern, $table)) { |
||
643 | $match = true; |
||
644 | } |
||
645 | } |
||
646 | |||
647 | return in_array($table, $arr) || $match; |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * Exports all the tables selected from database |
||
652 | * |
||
653 | * @return null |
||
654 | */ |
||
655 | private function exportTables() |
||
656 | { |
||
657 | // Exporting tables one by one |
||
658 | foreach ($this->tables as $table) { |
||
659 | if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { |
||
660 | continue; |
||
661 | } |
||
662 | $this->getTableStructure($table); |
||
663 | if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger |
||
664 | $this->listValues($table); |
||
665 | } elseif (true === $this->dumpSettings['no-data'] |
||
666 | || $this->matches($table, $this->dumpSettings['no-data'])) { |
||
667 | continue; |
||
668 | } else { |
||
669 | $this->listValues($table); |
||
670 | } |
||
671 | } |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Exports all the views found in database |
||
676 | * |
||
677 | * @return null |
||
678 | */ |
||
679 | private function exportViews() |
||
680 | { |
||
681 | if (false === $this->dumpSettings['no-create-info']) { |
||
682 | // Exporting views one by one |
||
683 | foreach ($this->views as $view) { |
||
684 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
685 | continue; |
||
686 | } |
||
687 | $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); |
||
688 | $this->getViewStructureTable($view); |
||
689 | } |
||
690 | foreach ($this->views as $view) { |
||
691 | if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
||
692 | continue; |
||
693 | } |
||
694 | $this->getViewStructureView($view); |
||
695 | } |
||
696 | } |
||
697 | } |
||
698 | |||
699 | /** |
||
700 | * Exports all the triggers found in database |
||
701 | * |
||
702 | * @return null |
||
703 | */ |
||
704 | private function exportTriggers() |
||
705 | { |
||
706 | // Exporting triggers one by one |
||
707 | foreach ($this->triggers as $trigger) { |
||
708 | $this->getTriggerStructure($trigger); |
||
709 | } |
||
710 | |||
711 | } |
||
712 | |||
713 | /** |
||
714 | * Exports all the procedures found in database |
||
715 | * |
||
716 | * @return null |
||
717 | */ |
||
718 | private function exportProcedures() |
||
719 | { |
||
720 | // Exporting triggers one by one |
||
721 | foreach ($this->procedures as $procedure) { |
||
722 | $this->getProcedureStructure($procedure); |
||
723 | } |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Exports all the functions found in database |
||
728 | * |
||
729 | * @return null |
||
730 | */ |
||
731 | private function exportFunctions() |
||
732 | { |
||
733 | // Exporting triggers one by one |
||
734 | foreach ($this->functions as $function) { |
||
735 | $this->getFunctionStructure($function); |
||
736 | } |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * Exports all the events found in database |
||
741 | * |
||
742 | * @return null |
||
743 | */ |
||
744 | private function exportEvents() |
||
745 | { |
||
746 | // Exporting triggers one by one |
||
747 | foreach ($this->events as $event) { |
||
748 | $this->getEventStructure($event); |
||
749 | } |
||
750 | } |
||
751 | |||
752 | /** |
||
753 | * Table structure extractor |
||
754 | * |
||
755 | * @todo move specific mysql code to typeAdapter |
||
756 | * @param string $tableName Name of table to export |
||
757 | * @return null |
||
758 | */ |
||
759 | private function getTableStructure($tableName) |
||
760 | { |
||
761 | if (!$this->dumpSettings['no-create-info']) { |
||
762 | $ret = ''; |
||
763 | if (!$this->dumpSettings['skip-comments']) { |
||
764 | $ret = "--".PHP_EOL. |
||
765 | "-- Table structure for table `$tableName`".PHP_EOL. |
||
766 | "--".PHP_EOL.PHP_EOL; |
||
767 | } |
||
768 | $stmt = $this->typeAdapter->show_create_table($tableName); |
||
769 | foreach ($this->dbHandler->query($stmt) as $r) { |
||
770 | $this->compressManager->write($ret); |
||
771 | if ($this->dumpSettings['add-drop-table']) { |
||
772 | $this->compressManager->write( |
||
773 | $this->typeAdapter->drop_table($tableName) |
||
774 | ); |
||
775 | } |
||
776 | $this->compressManager->write( |
||
777 | $this->typeAdapter->create_table($r) |
||
778 | ); |
||
779 | break; |
||
780 | } |
||
781 | } |
||
782 | $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); |
||
783 | return; |
||
784 | } |
||
785 | |||
786 | /** |
||
787 | * Store column types to create data dumps and for Stand-In tables |
||
788 | * |
||
789 | * @param string $tableName Name of table to export |
||
790 | * @return array type column types detailed |
||
791 | */ |
||
792 | |||
793 | private function getTableColumnTypes($tableName) |
||
794 | { |
||
795 | $columnTypes = array(); |
||
796 | $columns = $this->dbHandler->query( |
||
797 | $this->typeAdapter->show_columns($tableName) |
||
798 | ); |
||
799 | $columns->setFetchMode(PDO::FETCH_ASSOC); |
||
800 | |||
801 | foreach ($columns as $key => $col) { |
||
802 | $types = $this->typeAdapter->parseColumnType($col); |
||
803 | $columnTypes[$col['Field']] = array( |
||
804 | 'is_numeric'=> $types['is_numeric'], |
||
805 | 'is_blob' => $types['is_blob'], |
||
806 | 'type' => $types['type'], |
||
807 | 'type_sql' => $col['Type'], |
||
808 | 'is_virtual' => $types['is_virtual'], |
||
809 | 'key_sql' => $col['Key'], |
||
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) |
||
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) |
||
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) |
||
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) |
||
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 | $replace = $this->dumpSettings['replace'] && $this->getHasPrimaryKey($tableName); |
||
1123 | $strUpdates = $replace ? $this->getUpdateColumnsOnDuplicate($tableName) : ''; |
||
1124 | $extendedInsert = $this->dumpSettings['extended-insert'] && !$replace; |
||
1125 | $ignore = $this->dumpSettings['insert-ignore'] && !$replace ? ' IGNORE' : ''; |
||
1126 | |||
1127 | |||
1128 | $count = 0; |
||
1129 | foreach ($resultSet as $row) { |
||
1130 | $count++; |
||
1131 | $vals = $this->prepareColumnValues($tableName, $row); |
||
1132 | if ($onlyOnce || !$extendedInsert) { |
||
1133 | if ($this->dumpSettings['complete-insert']) { |
||
1134 | $lineSize += $this->compressManager->write( |
||
1135 | "INSERT$ignore INTO `$tableName` (". |
||
1136 | implode(", ", $colNames). |
||
1137 | ") VALUES (".implode(",", $vals).")" |
||
1138 | ); |
||
1139 | } else { |
||
1140 | $lineSize += $this->compressManager->write( |
||
1141 | "INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")" |
||
1142 | ); |
||
1143 | } |
||
1144 | if ($replace) { |
||
1145 | $lineSize += $this->compressManager->write(" ON DUPLICATE KEY UPDATE {$strUpdates}"); |
||
1146 | } |
||
1147 | $onlyOnce = false; |
||
1148 | } else { |
||
1149 | $lineSize += $this->compressManager->write(",(".implode(",", $vals).")"); |
||
1150 | } |
||
1151 | if (($lineSize > $this->dumpSettings['net_buffer_length']) || !$extendedInsert) { |
||
1152 | $onlyOnce = true; |
||
1153 | $lineSize = $this->compressManager->write(";".PHP_EOL); |
||
1154 | } |
||
1155 | } |
||
1156 | $resultSet->closeCursor(); |
||
1157 | |||
1158 | if (!$onlyOnce) { |
||
1159 | $this->compressManager->write(";".PHP_EOL); |
||
1160 | } |
||
1161 | |||
1162 | $this->endListValues($tableName, $count); |
||
1163 | |||
1164 | if ($this->infoCallable) { |
||
1165 | call_user_func($this->infoCallable, 'table', array('name' => $tableName, 'rowCount' => $count)); |
||
1166 | } |
||
1167 | } |
||
1168 | |||
1169 | /** |
||
1170 | * Does the table have a primary key |
||
1171 | * |
||
1172 | * @param string $tableName Name of table |
||
1173 | * |
||
1174 | * @return bool |
||
1175 | */ |
||
1176 | public function getHasPrimaryKey($tableName) |
||
1184 | } |
||
1185 | |||
1186 | /** |
||
1187 | * Build SQL List of all non-primary key columns on current table which will be used for update on duplicate key |
||
1188 | * |
||
1189 | * @param string $tableName Name of table to get columns |
||
1190 | * |
||
1191 | * @return string update columns for sql sentence where update on duplicate key |
||
1192 | */ |
||
1193 | public function getUpdateColumnsOnDuplicate($tableName) |
||
1202 | } |
||
1203 | |||
1204 | /** |
||
1205 | * Table rows extractor, append information prior to dump |
||
1206 | * |
||
1207 | * @param string $tableName Name of table to export |
||
1208 | * |
||
1209 | * @return null |
||
1210 | */ |
||
1211 | public function prepareListValues($tableName) |
||
1212 | { |
||
1213 | if (!$this->dumpSettings['skip-comments']) { |
||
1214 | $this->compressManager->write( |
||
1215 | "--".PHP_EOL. |
||
1216 | "-- Dumping data for table `$tableName`".PHP_EOL. |
||
1217 | "--".PHP_EOL.PHP_EOL |
||
1218 | ); |
||
1219 | } |
||
1220 | |||
1221 | if ($this->dumpSettings['single-transaction']) { |
||
1222 | $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
||
1223 | $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
||
1224 | } |
||
1225 | |||
1226 | if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { |
||
1227 | $this->typeAdapter->lock_table($tableName); |
||
1228 | } |
||
1229 | |||
1230 | if ($this->dumpSettings['add-locks']) { |
||
1231 | $this->compressManager->write( |
||
1232 | $this->typeAdapter->start_add_lock_table($tableName) |
||
1233 | ); |
||
1234 | } |
||
1235 | |||
1236 | if ($this->dumpSettings['disable-keys']) { |
||
1237 | $this->compressManager->write( |
||
1238 | $this->typeAdapter->start_add_disable_keys($tableName) |
||
1239 | ); |
||
1240 | } |
||
1241 | |||
1242 | // Disable autocommit for faster reload |
||
1243 | if ($this->dumpSettings['no-autocommit']) { |
||
1244 | $this->compressManager->write( |
||
1245 | $this->typeAdapter->start_disable_autocommit() |
||
1246 | ); |
||
1247 | } |
||
1248 | |||
1249 | return; |
||
1250 | } |
||
1251 | |||
1252 | /** |
||
1253 | * Table rows extractor, close locks and commits after dump |
||
1254 | * |
||
1255 | * @param string $tableName Name of table to export. |
||
1256 | * @param integer $count Number of rows inserted. |
||
1257 | * |
||
1258 | * @return void |
||
1259 | */ |
||
1260 | public function endListValues($tableName, $count = 0) |
||
1261 | { |
||
1262 | if ($this->dumpSettings['disable-keys']) { |
||
1263 | $this->compressManager->write( |
||
1264 | $this->typeAdapter->end_add_disable_keys($tableName) |
||
1265 | ); |
||
1266 | } |
||
1267 | |||
1268 | if ($this->dumpSettings['add-locks']) { |
||
1269 | $this->compressManager->write( |
||
1270 | $this->typeAdapter->end_add_lock_table($tableName) |
||
1271 | ); |
||
1272 | } |
||
1273 | |||
1274 | if ($this->dumpSettings['single-transaction']) { |
||
1275 | $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
||
1276 | } |
||
1277 | |||
1278 | if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { |
||
1279 | $this->typeAdapter->unlock_table($tableName); |
||
1280 | } |
||
1281 | |||
1282 | // Commit to enable autocommit |
||
1283 | if ($this->dumpSettings['no-autocommit']) { |
||
1284 | $this->compressManager->write( |
||
1285 | $this->typeAdapter->end_disable_autocommit() |
||
1286 | ); |
||
1287 | } |
||
1288 | |||
1289 | $this->compressManager->write(PHP_EOL); |
||
1290 | |||
1291 | if (!$this->dumpSettings['skip-comments']) { |
||
1292 | $this->compressManager->write( |
||
1293 | "-- Dumped table `".$tableName."` with $count row(s)".PHP_EOL. |
||
1294 | '--'.PHP_EOL.PHP_EOL |
||
1295 | ); |
||
1296 | } |
||
1297 | |||
1298 | return; |
||
1299 | } |
||
1300 | |||
1301 | /** |
||
1302 | * Build SQL List of all columns on current table which will be used for selecting |
||
1303 | * |
||
1304 | * @param string $tableName Name of table to get columns |
||
1305 | * |
||
1306 | * @return array SQL sentence with columns for select |
||
1307 | */ |
||
1308 | public function getColumnStmt($tableName) |
||
1309 | { |
||
1310 | $colStmt = array(); |
||
1311 | foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
||
1312 | if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
||
1313 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
||
1314 | } elseif ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
||
1315 | $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
||
1316 | } elseif ($colType['is_virtual']) { |
||
1317 | $this->dumpSettings['complete-insert'] = true; |
||
1318 | continue; |
||
1319 | } else { |
||
1320 | $colStmt[] = "`${colName}`"; |
||
1321 | } |
||
1322 | } |
||
1323 | |||
1324 | return $colStmt; |
||
1325 | } |
||
1326 | |||
1327 | /** |
||
1328 | * Build SQL List of all columns on current table which will be used for inserting |
||
1329 | * |
||
1330 | * @param string $tableName Name of table to get columns |
||
1331 | * |
||
1332 | * @return array columns for sql sentence for insert |
||
1333 | */ |
||
1334 | public function getColumnNames($tableName) |
||
1346 | } |
||
1347 | } |
||
1348 | |||
1349 | /** |
||
2372 |