Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for STDIN, STDOUT, and domains #3

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add support for PostgreSQL domains
  • Loading branch information
jrsupplee committed Mar 3, 2014
commit 44fb8fd5d2e0a995b863e29ec077e048fe771390
65 changes: 65 additions & 0 deletions pg2mysql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

//this is the default, it can be overridden here, or specified as the third parameter on the command line
$config['engine']="MyISAM";
$config['domains'] = array();
$config['domainschema'] = null;
$config['verbose'] = false;


Expand Down Expand Up @@ -172,6 +174,12 @@ function pg2mysql(&$input, $header=true)
$tbl_extra="";
while(isset($lines[$linenumber])) {
$line=$lines[$linenumber];

if (!$config['domainschema'] && preg_match('/SET\s+search_path\s*=\s*([^,\s]+)/', $line, $matches)) {
$config['domainschema'] = $matches[1];
write_debug("Schema: " . $config['domainschema']);
}

if(substr($line,0,12)=="CREATE TABLE") {
$in_create_table=true;
$line=str_replace("\"","`",$line);
Expand All @@ -193,6 +201,17 @@ function pg2mysql(&$input, $header=true)
}

if($in_create_table) {
/*
Replace domains with their PostgreSQL definitions
*/
foreach ($config['domains'] AS $dom => $def) {
if ($config['domainschema']) {
$line = preg_replace('/\b' . $config['domainschema'] . '.' . $dom . '\b/', $def, $line);
}

$line = preg_replace('/\b' . $dom . '\b/', $def, $line);
}

$line=str_replace("\"","`",$line);
$line=str_replace(" integer"," int(11)",$line);
$line=str_replace(" int_unsigned"," int(11) UNSIGNED",$line);
Expand Down Expand Up @@ -384,6 +403,19 @@ function pg2mysql(&$input, $header=true)
preg_match('/CREATE DATABASE ([a-zA-Z0-9_]*) .* ENCODING = \'(.*)\'/', $line, $matches);
$output .= "CREATE DATABASE `$matches[1]` DEFAULT CHARACTER SET $matches[2];\n\n";
}

if (preg_match('/^\s*CREATE DOMAIN/', $line)) {
$def = $line;
while (!preg_match('/;\s*$/', $line) && isset($lines[$linenumber+1])) {
$linenumber++;
$line = $lines[$linenumber];
$def .= $line;
}

if (preg_match('/CREATE DOMAIN\s+([a-zA-Z0-9_\.]+)\s+AS\s+(.+?)(\s*;|\s+CONSTRAINT)/', $def, $matches)) {
$config['domains'][$matches[1]] = $matches[2];
}
}

if(substr($line, 0, 8) == '\\connect') {
preg_match('/connect ([a-zA-Z0-9_]*)/', $line, $matches);
Expand Down Expand Up @@ -418,3 +450,36 @@ function pg2mysql(&$input, $header=true)
}
return $output;
}



function read_domains(&$input)
{
global $config;

if(is_array(&$input)) {
$lines=$input;
} else {
$lines=split("\n",$input);
}

while(count($lines)) {
$line = array_shift($lines);

if (preg_match('/SET\s+search_path\s*=\s*([^,\s]+)/', $line, $matches)) {
$config['domainschema'] = $matches[1];
write_debug("Schema: " . $config['domainschema']);
}

if (preg_match('/^\s*CREATE DOMAIN/', $line)) {
$def = $line;
while (!preg_match('/;\s*$/', $def) && count($lines)) {
$def .= array_shift($lines);
}

if (preg_match('/CREATE DOMAIN\s+([a-zA-Z0-9_\.]+)\s+AS\s+(.+?)(\s*;|\s+CONSTRAINT)/', $def, $matches)) {
$config['domains'][$matches[1]] = $matches[2];
}
}
}
}
19 changes: 18 additions & 1 deletion pg2mysql_cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ function print_help()
name of the MySQL database engine to be used by converted
tables. Defaults to {$config['engine']}

--domains <filename>
name of the file that contains domain definitions for the conversion

--verbose
print extra processing information to STDERR.

Expand All @@ -89,14 +92,18 @@ function print_help()
// remove the program name
array_shift($argv);

$domain_file = NULL;
$input_file = NULL;
$output_file = NULL;
$engine = NULL;

while (count($argv)) {
$arg = array_shift($argv);
if (preg_match('/^--/', $arg)) {
if ($arg == '--help') {
if ($arg == '--domains') {
$domain_file = array_shift($argv);

} elseif ($arg == '--help') {
print_help();
exit;

Expand Down Expand Up @@ -136,6 +143,16 @@ function print_help()
}
write_debug("Output File: $output_file");

if ($domain_file) {
write_debug("Domain definitions file: $domain_file");
read_domains(file($domain_file));
foreach ($config['domains'] as $name => $def) {
write_debug(" $name --> $def");
}
write_debug("");
}

pg2mysql_large($input_file, $output_file);

print_notes();