diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index dd88fa14..7727722b 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -525,25 +525,24 @@ private function parseColumnType($colType) */ private function getViewStructure($viewName) { - $stmt = $this->typeAdapter->show_create_view($viewName); - foreach ($this->dbHandler->query($stmt) as $r) { - if (isset($r['Create View'])) { - if (!$this->dumpSettings['no-create-info']) { + if (!$this->dumpSettings['no-create-info']) { + $ret = "--" . PHP_EOL . + "-- Table structure for view `${viewName}`" . PHP_EOL . + "--" . PHP_EOL . PHP_EOL; + $this->compressManager->write($ret); + + $stmt = $this->typeAdapter->show_create_view($viewName); + foreach ($this->dbHandler->query($stmt) as $r) { + if ($this->dumpSettings['add-drop-table']) { $this->compressManager->write( - "--" . PHP_EOL . - "-- Table structure for view `$viewName`" . PHP_EOL . - "--" . PHP_EOL . PHP_EOL + $this->typeAdapter->drop_view($viewName) ); - if ($this->dumpSettings['add-drop-table']) { - $this->compressManager->write( - $this->typeAdapter->drop_view($viewName) - ); - } - $this->compressManager->write($r['Create View'] . ";" . PHP_EOL . PHP_EOL); } - return; + $this->compressManager->write( + $this->typeAdapter->create_view($r) + ); + break; } - throw new Exception("Error getting view structure, unknown output"); } } @@ -588,12 +587,12 @@ private function escape($tableName, $row) $ret[] = "NULL"; } elseif ($this->dumpSettings['hex-blob'] && $columnTypes[$colName]['is_blob']) { if ($columnTypes[$colName]['type'] == 'bit') { - $ret[] = '0x' . bin2hex(chr($colValue)); + $ret[] = '0x' . strtoupper(bin2hex(chr($colValue))); } else { if (empty($colValue)) { $ret[] = "''"; } else { - $ret[] = '0x' . bin2hex($colValue); + $ret[] = '0x' . strtoupper(bin2hex($colValue)); } } } elseif ($columnTypes[$colName]['is_numeric']) { @@ -907,6 +906,15 @@ public function show_create_view($viewName) "WHERE type='view' AND tbl_name='$viewName'"; } + /** + * function create_view Get view creation code from database + * @todo make it do something with sqlite + */ + public function create_view($row) + { + return ""; + } + /** * function show_create_trigger Get trigger creation code from database * @todo make it do something with sqlite @@ -1095,10 +1103,43 @@ public function create_table($row) if (isset($row['Create Table'])) { $ret = $row['Create Table'] . ";" . PHP_EOL . PHP_EOL; } else { - throw new Exception("Error getting trigger code, unknown output"); + throw new Exception("Error getting table code, unknown output"); } return $ret; + } + public function create_view($row) + { + $ret = ""; + if (isset($row['Create View'])) { + $triggerStmt = $row['Create View']; + $triggerStmtReplaced1 = str_replace( + "CREATE ALGORITHM", + "/*!50001 CREATE ALGORITHM", + $triggerStmt + ); + $triggerStmtReplaced2 = str_replace( + " DEFINER=", + " */" . PHP_EOL . "/*!50013 DEFINER=", + $triggerStmtReplaced1 + ); + $triggerStmtReplaced3 = str_replace( + " VIEW ", + " */" . PHP_EOL . "/*!50001 VIEW ", + $triggerStmtReplaced2 + ); + if (false === $triggerStmtReplaced1 || + false === $triggerStmtReplaced2 || + false === $triggerStmtReplaced3) { + $triggerStmtReplaced = $triggerStmt; + } else { + $triggerStmtReplaced = $triggerStmtReplaced3 . " */;"; + } + $ret .= $triggerStmtReplaced . PHP_EOL . PHP_EOL; + return $ret; + } else { + throw new Exception("Error getting view structure, unknown output"); + } } public function create_trigger($row) diff --git a/tests/original.sql b/tests/original.sql index 607dbb24..1fa54369 100644 --- a/tests/original.sql +++ b/tests/original.sql @@ -5,7 +5,7 @@ USE `test001`; DROP TABLE IF EXISTS `test000`; CREATE TABLE `test000` ( `id` int, - `col01` bit(1) DEFAULT NULL, + `col01` bit(6) DEFAULT NULL, `col02` tinyint(4) DEFAULT NULL, `col03` tinyint(4) UNSIGNED DEFAULT NULL, `col10` bigint DEFAULT NULL, @@ -13,7 +13,7 @@ CREATE TABLE `test000` ( `col15` double DEFAULT NULL, `col27` varchar(6) DEFAULT NULL ); -INSERT INTO `test000` VALUES (1,0x01,-128,255,-9223372036854775808,18446744073709551615,-2.2250738585072014e-308,'0abcde'); +INSERT INTO `test000` VALUES (1,0x21,-128,255,-9223372036854775808,18446744073709551615,-2.2250738585072014e-308,'0abcde'); DROP TABLE IF EXISTS `test001`; CREATE TABLE `test001` (