From 40a5805271780aa124c90a844da746558f31b7e7 Mon Sep 17 00:00:00 2001 From: John Supplee Date: Mon, 3 Mar 2014 15:39:26 +0200 Subject: [PATCH 1/9] add UNIQUE constraints --- pg2mysql.inc.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index efe8a28..255bc87 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -335,8 +335,8 @@ function pg2mysql(&$input, $header=true) $linenumber++; $line=$lines[$linenumber]; - if(strstr($line," PRIMARY KEY ") && substr($line,-3,-1)==");") { - //looks like we have a single line PRIMARY KEY definition, lets go ahead and add it + if(preg_match('/(PRIMARY KEY|UNIQUE).*\);\s*$/', $line)) { + //looks like we have a single line PRIMARY KEY or UNIQUE definition, lets go ahead and add it $output.=$pkey; //the postgres and mysql syntax for this is (at least, in the example im looking at) //identical, so we can just add it as is. From 6d925dbbc385aefd126c81abb0f241105866845f Mon Sep 17 00:00:00 2001 From: John Supplee Date: Mon, 3 Mar 2014 16:03:35 +0200 Subject: [PATCH 2/9] Add support for STDIN, STDOUT and command line options --- pg2mysql.inc.php | 37 +++++++++++--- pg2mysql_cli.php | 123 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 137 insertions(+), 23 deletions(-) mode change 100644 => 100755 pg2mysql_cli.php diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 255bc87..918e6aa 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -28,6 +28,16 @@ //this is the default, it can be overridden here, or specified as the third parameter on the command line $config['engine']="MyISAM"; +$config['verbose'] = false; + + +function write_debug($message) { + global $config; + if ($config['verbose']) { + fwrite(STDERR, "$message\n"); + } +} + function getfieldname($l) { @@ -62,8 +72,17 @@ function formatsize($s) { function pg2mysql_large($infilename,$outfilename) { - $fs=filesize($infilename); + $from_stdin = 0; + if ($infilename == '-') { + $infilename = 'php://STDIN'; + $from_stdin = 1; + } else { + $fs=filesize($infilename); + } $infp=fopen($infilename,"rt"); + if ($outfilename == '-') { + $outfilename = 'php://STDOUT'; + } $outfp=fopen($outfilename,"wt"); //we read until we get a semicolon followed by a newline (;\n); @@ -72,7 +91,9 @@ function pg2mysql_large($infilename,$outfilename) { $linenum=0; $inquotes=false; $first=true; - echo "Filesize: ".formatsize($fs)."\n"; + if (!$from_stdin) { + fprintf(STDERR, "Filesize: ".formatsize($fs)."\n"); + } while($instr=fgets($infp)) { $linenum++; @@ -88,20 +109,20 @@ function pg2mysql_large($infilename,$outfilename) { $inquotes=true; } - if( $linenum%10000 == 0) { + if( !$from_stdin && $linenum%10000 == 0) { $currentpos=ftell($infp); $percent=round($currentpos/$fs*100); $position=formatsize($currentpos); - printf("Reading progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r",$percent,$position,$linenum,$chunkcount,$memusage); + fprintf(STDERR, "Reading progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r",$percent,$position,$linenum,$chunkcount,$memusage); } if(strlen($instr)>3 && ( $instr[$len-3]==")" && $instr[$len-2]==";" && $instr[$len-1]=="\n") && $inquotes==false) { $chunkcount++; - if($linenum%10000==0) { + if(!$from_stdin && $linenum%10000==0) { $currentpos=ftell($infp); $percent=round($currentpos/$fs*100); $position=formatsize($currentpos); - printf("Processing progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r",$percent,$position,$linenum,$chunkcount,$memusage); + fprintf(STDERR, "Processing progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r",$percent,$position,$linenum,$chunkcount,$memusage); } /* echo "sending chunk:\n"; @@ -118,8 +139,8 @@ function pg2mysql_large($infilename,$outfilename) { $mysqlchunk=""; } } - echo "\n\n"; - printf("Completed! %9d lines %9d sql chunks\n\n",$linenum,$chunkcount); + fwrite(STDERR, "\n\n"); + fprintf(STDERR, "Completed! %9d lines %9d sql chunks\n\n",$linenum,$chunkcount); fclose($infp); fclose($outfp); diff --git a/pg2mysql_cli.php b/pg2mysql_cli.php old mode 100644 new mode 100755 index 4863bc3..757b329 --- a/pg2mysql_cli.php +++ b/pg2mysql_cli.php @@ -1,3 +1,4 @@ +#!/usr/bin/php [engine]\n"; - exit; -} -else { - if(isset($argv[3])) $config['engine']=$argv[3]; - pg2mysql_large($argv[1],$argv[2]); - - -echo << []] + +DESCRIPTION + + name of the input file or a dash (-) for STDIN. Defaults to STDIN + + + name of the output file or a dash (-) for STDOUT. Defaults to STDOUT + + --engine + name of the MySQL database engine to be used by converted + tables. Defaults to {$config['engine']} + + --verbose + print extra processing information to STDERR. + + --help + print this help information + +VERSION + {$constants['VERSION']} -XHTML; +COPYRIGHT + {$constants['COPYRIGHT']} + +USAGE + ); + print_notes(); } + +// remove the program name +array_shift($argv); + +$input_file = NULL; +$output_file = NULL; +$engine = NULL; + +while (count($argv)) { + $arg = array_shift($argv); + if (preg_match('/^--/', $arg)) { + if ($arg == '--help') { + print_help(); + exit; + + } elseif ($arg == '--verbose') { + $config['verbose'] = true; + + } elseif ($arg == '--engine') { + $config['engine'] = array_shift($argv); + + } else { + fwrite(STDERR, "ERROR: option '$arg' is not recognized.\n\n"); + print_help(); + exit(1); + } + + } elseif (!$input_file) { + $input_file = $arg; + + } elseif (!$output_file) { + $output_file = $arg; + + } elseif (!$engine) { + $config['engine'] = $arg; + $engine = 1; + } +} + +// default to STDIN +if (!$input_file) { + $input_file = '-'; +} +write_debug("Input File: $input_file"); + +// default to STDOUT +if (!$output_file) { + $output_file = '-'; +} +write_debug("Output File: $output_file"); + +pg2mysql_large($input_file, $output_file); + +print_notes(); From 44fb8fd5d2e0a995b863e29ec077e048fe771390 Mon Sep 17 00:00:00 2001 From: John Supplee Date: Mon, 3 Mar 2014 20:02:18 +0200 Subject: [PATCH 3/9] Add support for PostgreSQL domains --- pg2mysql.inc.php | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ pg2mysql_cli.php | 19 +++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 918e6aa..3e248d8 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -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; @@ -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); @@ -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); @@ -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); @@ -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]; + } + } + } +} diff --git a/pg2mysql_cli.php b/pg2mysql_cli.php index 757b329..c1641a2 100755 --- a/pg2mysql_cli.php +++ b/pg2mysql_cli.php @@ -68,6 +68,9 @@ function print_help() name of the MySQL database engine to be used by converted tables. Defaults to {$config['engine']} + --domains + name of the file that contains domain definitions for the conversion + --verbose print extra processing information to STDERR. @@ -89,6 +92,7 @@ function print_help() // remove the program name array_shift($argv); +$domain_file = NULL; $input_file = NULL; $output_file = NULL; $engine = NULL; @@ -96,7 +100,10 @@ function print_help() 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; @@ -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(); + From 4887cc96860c2c8d08b37627aef179ddd1fa949c Mon Sep 17 00:00:00 2001 From: John Supplee Date: Mon, 3 Mar 2014 20:04:17 +0200 Subject: [PATCH 4/9] convert README to Markdown and add make some additions --- README | 81 ----------------------------------- README.md | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 81 deletions(-) delete mode 100644 README create mode 100644 README.md diff --git a/README b/README deleted file mode 100644 index 55b2b87..0000000 --- a/README +++ /dev/null @@ -1,81 +0,0 @@ -Pg2MySQL Converter -Lightbox Technolgoies -http://www.lightbox.org -Author: James Grant - -INSTALLATION/USAGE - -To use, simply unzip into your website somewhere, and point your browser at - pg2mysql.php -If you want to use your own interface, simply include the pg2mysql.inc.php -into your own code and call the 'pg2mysql' function. It accepts one input -(the postgres code to be converted) and returns the mysql code. - -eg: $mysql=pg2mysql($postgres); - - -COMMAND LINE VERSION - -php pg2mysql_cli.php [engine] - - - inputfilename.sql will not be modified - - outputfilename.sql will be overwritten if already exists - - engine is optional, defaults if not specified is MyISAM - - -DONATIONS APPRECIATED - - Paypal: paypal@lightbox.org - Bitcoin: 1Gt6D2HcwnoLCeDMkP6xKMC4P65g42TiVn - Cheque/money order: - Lightbox Technologies Inc - 240 Catherine St. Suite 312 - Ottawa, ON - K2P 2G8 CANADA - -CHANGES - -VERSION 1.0 (I guess) -- This is the first public release. Up until now it has been web-access only. -- Allow "character varying" without a size. defaults to varchar(255) - -VERSION 1.1 -- fix detection of field names that dont have `` around them when looking up a primary key - -VERSION 1.2 (Thanks to John Puster) -- fix character varying 255 -> varchar(255) instead of text (<= vs <) -- add conversions for 'time with(out) time zone' to 'time' field -- correct 'timestamp with(out) time zone' to convert to 'timestamp' instead of 'datetime' and allow default CURRENT_TIMESTAMP - -VERSION 1.3 -- add command line version that can process large files -- add conversions for many new data types -- add checks for multi-line INSERTs that contain quoted strings across multiple rows that contain "end of sql" look-alike line endings -- filter CONSTRAINT lines properly - -VERSION 1.4 -- add config for specifying the engine to output as (default MyISAM), with cli version engine can be specified as the third argument -- fix " conversion on INSERT lines - only replace " with ` on fieldnames (before "VALUES"), not in the actual data itself - -VERSION 1.5 -- add detection for "E"scaped string literals, like INSERT INTO table VALUES (E'string\\n', E'another string\\r\\n'); - -VERSION 1.6 -- also catch "E"scaped string literals when they are preceeded by a non string (12345, E'a string\\r\\n') - -VERSION 1.7 -- Add conversion for ALTER TABLE to add PRIMARY KEYs -- Add conversion for CREATE INDEX to add INDEXes (via ALTER TABLE) - -VERSION 1.8 (Thanks to Vladimir Pilny ) -- Explicitly set error reporting -- Add SQL_MODE=NO_AUTO_VALUE_ON_ZERO and SET time_zone in start of SQL output -- Add conversion for DROP DATABASE -- Add conversion for CREATE DATABASE -- Add conversion for \connect/USE -- Add conversion for COPY table FROM stdin - -VERSION 1.9 -- Introduce commented header at the top of converted file -- Fix SQL_MODE and time_zone so it doesn't repeat when using CLI version -- Move config['engine'] to common file instead of only CLI version diff --git a/README.md b/README.md new file mode 100644 index 0000000..1fa8d2c --- /dev/null +++ b/README.md @@ -0,0 +1,124 @@ +AUTHOR +====== + +Pg2MySQL Converter + +Lightbox Technolgoies + +http://www.lightbox.org + +James Grant + + +INSTALLATION/USAGE +================== + +To use, simply unzip into your website somewhere, and point your browser at: + + pg2mysql.php + +If you want to use your own interface, simply include the pg2mysql.inc.php into +your own code and call the 'pg2mysql' function. It accepts one input (the +postgres code to be converted) and returns the mysql code. eg: + + $mysql = pg2mysql($postgres); + + +COMMAND LINE VERSION +==================== + +Command line syntax and options can be viewed using: + + php pg2mysql_cli.php --help + + +DOMAIN SUPPORT +-------------- + +PostgreSQL domains contained in a differenct schema than the one being processed +can be loaded using the `--domains` command-line option. The domain definitions +should be exported using `pg_dump`'s `--schema-only` option to create a file with +just definitions. If the domain definitions are in the same schema as the data +being exported this option is not necessary. + + +DONATIONS APPRECIATED +===================== + +Paypal: paypal@lightbox.org + +Bitcoin: 1Gt6D2HcwnoLCeDMkP6xKMC4P65g42TiVn + +Cheque/money order: + + Lightbox Technologies Inc + 240 Catherine St. Suite 312 + Ottawa, ON + K2P 2G8 CANADA + + +CHANGES +======= + +VERSION 1.0 (I guess) + +- This is the first public release. Up until now it has been web-access only. +- Allow "character varying" without a size. defaults to varchar(255) + +VERSION 1.1 + +- fix detection of field names that dont have `` around them when looking up a + primary key + +VERSION 1.2 (Thanks to John Puster) + +- fix character varying 255 -> varchar(255) instead of text (<= vs <) +- add conversions for 'time with(out) time zone' to 'time' field +- correct 'timestamp with(out) time zone' to convert to 'timestamp' instead of + 'datetime' and allow default CURRENT_TIMESTAMP + +VERSION 1.3 + +- add command line version that can process large files +- add conversions for many new data types +- add checks for multi-line INSERTs that contain quoted strings across multiple + rows that contain "end of sql" look-alike line endings +- filter CONSTRAINT lines properly + +VERSION 1.4 + +- add config for specifying the engine to output as (default MyISAM), with cli + version engine can be specified as the third argument +- fix " conversion on INSERT lines - only replace " with ` on fieldnames (before + "VALUES"), not in the actual data itself + +VERSION 1.5 + +- add detection for "E"scaped string literals, like INSERT INTO table VALUES + (E'string\\n', E'another string\\r\\n'); + +VERSION 1.6 + +- also catch "E"scaped string literals when they are preceeded by a non string + (12345, E'a string\\r\\n') + +VERSION 1.7 + +- Add conversion for ALTER TABLE to add PRIMARY KEYs +- Add conversion for CREATE INDEX to add INDEXes (via ALTER TABLE) + +VERSION 1.8 (Thanks to Vladimir Pilny ) + +- Explicitly set error reporting +- Add SQL_MODE=NO_AUTO_VALUE_ON_ZERO and SET time_zone in start of SQL output +- Add conversion for DROP DATABASE +- Add conversion for CREATE DATABASE +- Add conversion for \connect/USE +- Add conversion for COPY table FROM stdin + +VERSION 1.9 + +- Introduce commented header at the top of converted file +- Fix SQL_MODE and time_zone so it doesn't repeat when using CLI version +- Move config['engine'] to common file instead of only CLI version + From abb1854342e49ca46ae08e5639115a061c42b4b2 Mon Sep 17 00:00:00 2001 From: John Supplee Date: Mon, 3 Mar 2014 20:04:39 +0200 Subject: [PATCH 5/9] Ignore *.sql files and backup files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a194b5d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +*.sql + From 02ecba3b4dc6c41058e63bf3deb2c80ee02869fb Mon Sep 17 00:00:00 2001 From: John Supplee Date: Mon, 20 Oct 2014 14:05:17 +0200 Subject: [PATCH 6/9] remove NOT NULL from domain definitions --- pg2mysql.inc.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 3e248d8..5aca627 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -413,7 +413,7 @@ function pg2mysql(&$input, $header=true) } if (preg_match('/CREATE DOMAIN\s+([a-zA-Z0-9_\.]+)\s+AS\s+(.+?)(\s*;|\s+CONSTRAINT)/', $def, $matches)) { - $config['domains'][$matches[1]] = $matches[2]; + $config['domains'][$matches[1]] = str_replace('NOT NULL', '', $matches[2]); } } @@ -478,7 +478,8 @@ function read_domains(&$input) } if (preg_match('/CREATE DOMAIN\s+([a-zA-Z0-9_\.]+)\s+AS\s+(.+?)(\s*;|\s+CONSTRAINT)/', $def, $matches)) { - $config['domains'][$matches[1]] = $matches[2]; + // take NOT NULL out of domain def since it also appears on the column definition + $config['domains'][$matches[1]] = str_replace('NOT NULL', '', $matches[2]); } } } From 6041b59c8c610ec9bbc7a2a2bccf74971c9c0709 Mon Sep 17 00:00:00 2001 From: John Supplee Date: Sun, 15 Feb 2015 16:52:57 +0200 Subject: [PATCH 7/9] fixes --- pg2mysql.inc.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 5aca627..87713c5 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -153,7 +153,7 @@ function pg2mysql(&$input, $header=true) { global $config; - if(is_array(&$input)) { + if(is_array($input)) { $lines=$input; } else { $lines=split("\n",$input); @@ -453,11 +453,11 @@ function pg2mysql(&$input, $header=true) -function read_domains(&$input) +function read_domains($input) { global $config; - if(is_array(&$input)) { + if(is_array($input)) { $lines=$input; } else { $lines=split("\n",$input); From 501fd36975d976d1b57812dc12d2ebf368b89cf1 Mon Sep 17 00:00:00 2001 From: John Supplee Date: Thu, 25 Jan 2018 01:09:10 +0200 Subject: [PATCH 8/9] Fixes for deprecated PHP functions * replaced deprecated functions `ereg` and `ereg_replace` * added some functionality to support SQLite output. Still broken because of indices --- pg2mysql.inc.php | 64 ++++++++++++++++++++++++++---------------------- pg2mysql_cli.php | 9 ++++++- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 87713c5..50cdb64 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -31,6 +31,8 @@ $config['domains'] = array(); $config['domainschema'] = null; $config['verbose'] = false; +$config['sqlite'] = false; +$config['no-header'] = false; function write_debug($message) { @@ -44,21 +46,19 @@ function write_debug($message) { function getfieldname($l) { //first check if its in nice quotes for us - if(ereg("`(.*)`",$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. - else if(ereg("([^\ ]*)",trim($l),$regs)) + else if(preg_match("/([^\ ]*)/",trim($l),$regs)) { if($regs[1]) return $regs[1]; - else - return null; } + + return null; } function formatsize($s) { @@ -74,7 +74,10 @@ function formatsize($s) { function pg2mysql_large($infilename,$outfilename) { + global $config; + $from_stdin = 0; + $fs = null; if ($infilename == '-') { $infilename = 'php://STDIN'; $from_stdin = 1; @@ -92,7 +95,7 @@ function pg2mysql_large($infilename,$outfilename) { $chunkcount=1; $linenum=0; $inquotes=false; - $first=true; + $first = !$config['no-header']; if (!$from_stdin) { fprintf(STDERR, "Filesize: ".formatsize($fs)."\n"); } @@ -156,13 +159,13 @@ function pg2mysql(&$input, $header=true) if(is_array($input)) { $lines=$input; } else { - $lines=split("\n",$input); + $lines = explode("\n", $input); } if($header) { - $output = "# Converted with ".PRODUCT."-".VERSION."\n"; - $output.= "# Converted on ".date("r")."\n"; - $output.= "# ".COPYRIGHT."\n\n"; + $output = "-- Converted with ".PRODUCT."-".VERSION."\n"; + $output.= "-- Converted on ".date("r")."\n"; + $output.= "-- ".COPYRIGHT."\n\n"; $output.="SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\nSET time_zone=\"+00:00\";\n\n"; } else @@ -172,6 +175,9 @@ function pg2mysql(&$input, $header=true) $linenumber=0; $tbl_extra=""; + $values = array(); + $heads = null; + while(isset($lines[$linenumber])) { $line=$lines[$linenumber]; @@ -190,7 +196,7 @@ function pg2mysql(&$input, $header=true) if(substr($line,0,2)==");" && $in_create_table) { $in_create_table=false; - $line=") ENGINE={$config['engine']};\n\n"; + $line=")" . ($config['sqlite'] ? "" : " ENGINE={$config['engine']}") . ";\n\n"; $output.=$tbl_extra; $output.=$line; @@ -222,38 +228,38 @@ function pg2mysql(&$input, $header=true) $line=str_replace(" boolean"," bool",$line); $line=str_replace(" bool DEFAULT true"," bool DEFAULT 1",$line); $line=str_replace(" bool DEFAULT false"," bool DEFAULT 0",$line); - if(ereg(" character varying\(([0-9]*)\)",$line,$regs)) { + if(preg_match("/ character varying\(([0-9]*)\)/",$line,$regs)) { $num=$regs[1]; if($num<=255) - $line=ereg_replace(" character varying\([0-9]*\)"," varchar($num)",$line); + $line=preg_replace("/ character varying\([0-9]*\)/"," varchar($num)",$line); else - $line=ereg_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(ereg(" character varying",$line)) { - $line=ereg_replace(" character varying"," varchar(255)",$line); + if(preg_match("/ character varying/",$line)) { + $line=preg_replace("/ character varying/"," varchar(255)",$line); } - if( ereg("DEFAULT \('([0-9]*)'::int",$line,$regs) || - ereg("DEFAULT \('([0-9]*)'::smallint",$line,$regs) || - ereg("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=ereg_replace(" DEFAULT \('([0-9]*)'[^ ,]*"," DEFAULT $num ",$line); + $line=preg_replace("/ DEFAULT \('([0-9]*)'[^ ,]*/"," DEFAULT $num ",$line); } - if(ereg("DEFAULT \(([0-9\-]*)\)",$line,$regs)) { + if(preg_match("/DEFAULT \(([0-9\-]*)\)/",$line,$regs)) { $num=$regs[1]; - $line=ereg_replace(" DEFAULT \(([0-9\-]*)\)"," DEFAULT $num ",$line); + $line=preg_replace("/ DEFAULT \(([0-9\-]*)\)/"," DEFAULT $num ",$line); } - $line=ereg_replace(" DEFAULT nextval\(.*\) "," auto_increment ",$line); - $line=ereg_replace("::.*,",",",$line); - $line=ereg_replace("::.*$","\n",$line); - if(ereg("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=ereg_replace(" character\([0-9]*\)"," varchar($num)",$line); + $line=preg_replace("/ character\([0-9]*\)/"," varchar($num)",$line); else - $line=ereg_replace(" character\([0-9]*\)"," text",$line); + $line=preg_replace("/ character\([0-9]*\)/"," text",$line); } //timestamps $line=str_replace(" timestamp with time zone"," timestamp",$line); diff --git a/pg2mysql_cli.php b/pg2mysql_cli.php index c1641a2..4dc22c0 100755 --- a/pg2mysql_cli.php +++ b/pg2mysql_cli.php @@ -110,7 +110,14 @@ function print_help() } elseif ($arg == '--verbose') { $config['verbose'] = true; - } elseif ($arg == '--engine') { + } elseif ($arg == '--no-header') { + $config['no-header'] = true; + + } elseif ($arg == '--sqlite') { + $config['sqlite'] = true; + $config['no-header'] = true; + + } elseif ($arg == '--engine') { $config['engine'] = array_shift($argv); } else { From 3ea1dadafee3de1e4736272c909ad824119e8b72 Mon Sep 17 00:00:00 2001 From: "John R. Supplee" Date: Mon, 10 Jan 2022 10:22:31 +0200 Subject: [PATCH 9/9] replace ereg calls with preg --- pg2mysql.inc.php | 52 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/pg2mysql.inc.php b/pg2mysql.inc.php index 87713c5..a681d56 100644 --- a/pg2mysql.inc.php +++ b/pg2mysql.inc.php @@ -44,7 +44,7 @@ function write_debug($message) { function getfieldname($l) { //first check if its in nice quotes for us - if(ereg("`(.*)`",$l,$regs)) + if(preg_match("/`(.*)`/",$l,$regs)) { if($regs[1]) return $regs[1]; @@ -52,7 +52,7 @@ function getfieldname($l) return null; } //if its not in quotes, then it should (we hope!) be the first "word" on the line, up to the first space. - else if(ereg("([^\ ]*)",trim($l),$regs)) + else if(preg_match("/([^\ ]*)/",trim($l),$regs)) { if($regs[1]) return $regs[1]; @@ -174,7 +174,7 @@ 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']); @@ -208,7 +208,7 @@ function pg2mysql(&$input, $header=true) if ($config['domainschema']) { $line = preg_replace('/\b' . $config['domainschema'] . '.' . $dom . '\b/', $def, $line); } - + $line = preg_replace('/\b' . $dom . '\b/', $def, $line); } @@ -222,38 +222,38 @@ function pg2mysql(&$input, $header=true) $line=str_replace(" boolean"," bool",$line); $line=str_replace(" bool DEFAULT true"," bool DEFAULT 1",$line); $line=str_replace(" bool DEFAULT false"," bool DEFAULT 0",$line); - if(ereg(" character varying\(([0-9]*)\)",$line,$regs)) { + if(preg_match("/ character varying\((\d*)\)/",$line,$regs)) { $num=$regs[1]; if($num<=255) - $line=ereg_replace(" character varying\([0-9]*\)"," varchar($num)",$line); + $line=preg_replace("/ character varying\(\d*\)/"," varchar($num)",$line); else - $line=ereg_replace(" character varying\([0-9]*\)"," text",$line); + $line=preg_replace("/ character varying\(\d*\)/"," text",$line); } //character varying with no size, we will default to varchar(255) - if(ereg(" character varying",$line)) { - $line=ereg_replace(" character varying"," varchar(255)",$line); + if(preg_match("/ character varying/",$line)) { + $line=preg_replace("/ character varying/"," varchar(255)",$line); } - if( ereg("DEFAULT \('([0-9]*)'::int",$line,$regs) || - ereg("DEFAULT \('([0-9]*)'::smallint",$line,$regs) || - ereg("DEFAULT \('([0-9]*)'::bigint",$line,$regs) + if( preg_match("/DEFAULT \('(\d*)'::int/",$line,$regs) || + preg_match("/DEFAULT \('(\d*)'::smallint/",$line,$regs) || + preg_match("/DEFAULT \('(\d*)'::bigint/",$line,$regs) ) { $num=$regs[1]; - $line=ereg_replace(" DEFAULT \('([0-9]*)'[^ ,]*"," DEFAULT $num ",$line); + $line=preg_replace("/ DEFAULT \('(\d*)'[^ ,]*/"," DEFAULT $num ",$line); } - if(ereg("DEFAULT \(([0-9\-]*)\)",$line,$regs)) { + if(preg_match("/DEFAULT \(([0-9\-]*)\)/",$line,$regs)) { $num=$regs[1]; - $line=ereg_replace(" DEFAULT \(([0-9\-]*)\)"," DEFAULT $num ",$line); + $line=preg_replace("/ DEFAULT \(([\d\-]*)\)/"," DEFAULT $num ",$line); } - $line=ereg_replace(" DEFAULT nextval\(.*\) "," auto_increment ",$line); - $line=ereg_replace("::.*,",",",$line); - $line=ereg_replace("::.*$","\n",$line); - if(ereg("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\((\d*)\)/",$line,$regs)) { $num=$regs[1]; if($num<=255) - $line=ereg_replace(" character\([0-9]*\)"," varchar($num)",$line); + $line=preg_replace("/ character\(\d*\)/"," varchar($num)",$line); else - $line=ereg_replace(" character\([0-9]*\)"," text",$line); + $line=preg_replace("/ character\(\d*\)/"," text",$line); } //timestamps $line=str_replace(" timestamp with time zone"," timestamp",$line); @@ -403,7 +403,7 @@ 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])) { @@ -411,7 +411,7 @@ function pg2mysql(&$input, $header=true) $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]] = str_replace('NOT NULL', '', $matches[2]); } @@ -465,18 +465,18 @@ function read_domains($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)) { // take NOT NULL out of domain def since it also appears on the column definition $config['domains'][$matches[1]] = str_replace('NOT NULL', '', $matches[2]);