Skip to content

Commit

Permalink
fix create view
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsnop committed Sep 12, 2014
1 parent ea1c826 commit 719da0a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
77 changes: 59 additions & 18 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/original.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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,
`col11` bigint UNSIGNED DEFAULT NULL,
`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` (
Expand Down

0 comments on commit 719da0a

Please sign in to comment.