Skip to content

Commit

Permalink
Merge pull request #21 from aschosser/fix-column-names
Browse files Browse the repository at this point in the history
Fix column names
  • Loading branch information
ChrisLundquist authored Jun 22, 2018
2 parents 3f44c65 + e7a7270 commit 3efea26
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions pg2mysql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
function getfieldname($l)
{
//first check if its in nice quotes for us
if (preg_match("`(.*)`", $l, $regs)) {
if (preg_match("/`(.*)`/", $l, $regs)) {
if ($regs[1]) {
return $regs[1];
} else {
return null;
}
}
//if its not in quotes, then it should (we hope!) be the first "word" on the line, up to the first space.
elseif (preg_match("([^\ ]*)", trim($l), $regs)) {
elseif (preg_match("/([^\ ]*)/", trim($l), $regs)) {
if ($regs[1]) {
return $regs[1];
} else {
Expand Down Expand Up @@ -193,39 +193,40 @@ function pg2mysql($input, $header=true)
$line=str_replace(" bool DEFAULT true", " bool DEFAULT 1", $line);
$line=str_replace(" bool DEFAULT false", " bool DEFAULT 0", $line);
$line=str_replace("` `text`", "` text", $line); // fix because pg_dump quotes text type for some reason
if (preg_match(" character varying\(([0-9]*)\)", $line, $regs)) {
if (preg_match("/ character varying\(([0-9]*)\)/", $line, $regs)) {
$num=$regs[1];
if ($num<=255) {
$line=preg_replace(" character varying\([0-9]*\)", " varchar($num)", $line);
# Pattern delimniter "/" fails here. Use alternatively "|".
$line=preg_replace("| character varying\([0-9]*\)|", " varchar($num)", $line);
} else {
$line=preg_replace(" character varying\([0-9]*\)", " text", $line);
$line=preg_replace("/ character varying\([0-9]*\)/", " text", $line);
}
}
//character varying with no size, we will default to varchar(255)
if (preg_match(" character varying", $line)) {
$line=preg_replace(" character varying", " varchar(255)", $line);
if (preg_match("/ character varying/", $line)) {
$line=preg_replace("/ character varying/", " varchar(255)", $line);
}

if (preg_match("DEFAULT \('([0-9]*)'::int", $line, $regs) ||
preg_match("DEFAULT \('([0-9]*)'::smallint", $line, $regs) ||
preg_match("DEFAULT \('([0-9]*)'::bigint", $line, $regs)
if (preg_match("/DEFAULT \('([0-9]*)'::int/", $line, $regs) ||
preg_match("/DEFAULT \('([0-9]*)'::smallint/", $line, $regs) ||
preg_match("/DEFAULT \('([0-9]*)'::bigint/", $line, $regs)
) {
$num=$regs[1];
$line=preg_replace(" DEFAULT \('([0-9]*)'[^ ,]*", " DEFAULT $num ", $line);
$line=preg_replace("/ DEFAULT \('([0-9]*)'[^ ,]*/", " DEFAULT $num ", $line);
}
if (preg_match("DEFAULT \(([0-9\-]*)\)", $line, $regs)) {
if (preg_match("/DEFAULT \(([0-9\-]*)\)/", $line, $regs)) {
$num=$regs[1];
$line=preg_replace(" DEFAULT \(([0-9\-]*)\)", " DEFAULT $num ", $line);
$line=preg_replace("/ DEFAULT \(([0-9\-]*)\)/", " DEFAULT $num ", $line);
}
$line=preg_replace(" DEFAULT nextval\(.*\) ", " auto_increment ", $line);
$line=preg_replace("::.*,", ",", $line);
$line=preg_replace("::.*$", "\n", $line);
if (preg_match("character\(([0-9]*)\)", $line, $regs)) {
$line=preg_replace("/ DEFAULT nextval\(.*\) /", " auto_increment ", $line);
$line=preg_replace("/::.*,/", ",", $line);
$line=preg_replace("/::.*$/", "\n", $line);
if (preg_match("/character\(([0-9]*)\)/", $line, $regs)) {
$num=$regs[1];
if ($num<=255) {
$line=preg_replace(" character\([0-9]*\)", " varchar($num)", $line);
$line=preg_replace("/ character\([0-9]*\)/", " varchar($num)", $line);
} else {
$line=preg_replace(" character\([0-9]*\)", " text", $line);
$line=preg_replace("/ character\([0-9]*\)/", " text", $line);
}
}
//timestamps
Expand Down Expand Up @@ -375,15 +376,24 @@ function pg2mysql($input, $header=true)
}

if (substr($line, 0, 5) == 'COPY ') {
preg_match('/COPY (.*) FROM stdin/', $line, $matches);
$heads = str_replace('"', "`", $matches[1]);
# Wrap all table and column names in "`" to prevent clashes with reserved names in mysql.
preg_match('/COPY\s+(\S+)\s*\((.*)\)\s+FROM\s+stdin/', $line, $matches);
$table = preg_replace('/^["`]*(\S+)["`]*$/', "`$1`", $matches[1]);
$columns = '';
foreach (explode(',', $matches[2]) as $columnName) {
$columnName = preg_replace('/^\s*["`]*([^"`\s]+)["`]*\s*$/', "`$1`", $columnName);
if ($columns) {
$columns .= ', ';
}
$columns .= $columnName;
}
$values = array();
$in_insert = true;
} elseif ($in_insert) {
if ($line == "\\.\n") {
$in_insert = false;
if ($values) {
$output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n\n";
$output .= "INSERT INTO $table ($columns) VALUES\n" . implode(",\n", $values) . ";\n\n";
}
} else {
$vals = explode(' ', $line);
Expand All @@ -405,7 +415,7 @@ function pg2mysql($input, $header=true)
}
$values[] = '(' . implode(',', $vals) . ')';
if (count($values) >= 1000) {
$output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n";
$output .= "INSERT INTO $table ($columns) VALUES\n" . implode(",\n", $values) . ";\n";
$values = array();
}
}
Expand Down

0 comments on commit 3efea26

Please sign in to comment.