Fix phpcs
This commit is contained in:
parent
34cfa0c6af
commit
5533526331
@ -1,7 +1,8 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
/* Copyright (C) 2005-2011 James Grant <james@lightbox.org> Lightbox Technologies Inc.
|
/*
|
||||||
* Copyright (C) 2020 Rodolphe Quiedeville <rodolphe@quiedeville.org>
|
* Copyright (C) 2005-2011 James Grant <james@lightbox.org> Lightbox Technologies Inc.
|
||||||
|
* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@ -10,189 +11,197 @@
|
|||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* ATTENTION DE PAS EXECUTER CE SCRIPT SUR UNE INSTALLATION DE PRODUCTION
|
* This file is base on pg2mysql provided as Open source by lightbox.org.
|
||||||
|
* It was enhanced and updated by the Dolibarr team.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \file dev/tools/dolibarr-postgres2mysql.php
|
* \file dev/tools/dolibarr-postgres2mysql.php
|
||||||
* \brief Script to migrate a postgresql dump into a mysql dump
|
* \brief Script to migrate a postgresql dump into a mysql dump
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
$sapi_type = php_sapi_name();
|
$sapi_type = php_sapi_name();
|
||||||
$script_file = basename(__FILE__);
|
$script_file = basename(__FILE__);
|
||||||
$path=dirname(__FILE__).'/';
|
$path = dirname(__FILE__) . '/';
|
||||||
|
|
||||||
// Test si mode batch
|
// Test si mode batch
|
||||||
$sapi_type = php_sapi_name();
|
$sapi_type = php_sapi_name();
|
||||||
if (substr($sapi_type, 0, 3) == 'cgi') {
|
if (substr($sapi_type, 0, 3) == 'cgi') {
|
||||||
echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
|
echo "Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode.\n";
|
||||||
exit;
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
error_reporting(E_ALL & ~ E_DEPRECATED);
|
||||||
|
define('PRODUCT', "pg2mysql");
|
||||||
|
define('VERSION', "2.0");
|
||||||
|
|
||||||
error_reporting(E_ALL & ~E_DEPRECATED);
|
// this is the default, it can be overridden here, or specified as the third parameter on the command line
|
||||||
define ('PRODUCT',"pg2mysql");
|
$config['engine'] = "InnoDB";
|
||||||
define ('VERSION',"2.0");
|
|
||||||
|
|
||||||
|
if (! ($argv[1] && $argv[2])) {
|
||||||
//this is the default, it can be overridden here, or specified as the third parameter on the command line
|
|
||||||
$config['engine']="InnoDB";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(! ($argv[1] && $argv[2]) ) {
|
|
||||||
echo "Usage: php pg2mysql_cli.php <inputfilename> <outputfilename> [engine]\n";
|
echo "Usage: php pg2mysql_cli.php <inputfilename> <outputfilename> [engine]\n";
|
||||||
exit;
|
exit();
|
||||||
}
|
} else {
|
||||||
else {
|
if (isset($argv[3]))
|
||||||
if(isset($argv[3])) $config['engine']=$argv[3];
|
$config['engine'] = $argv[3];
|
||||||
pg2mysql_large($argv[1], $argv[2]);
|
pg2mysql_large($argv[1], $argv[2]);
|
||||||
|
|
||||||
|
|
||||||
echo <<<XHTML
|
echo <<<XHTML
|
||||||
Notes:
|
Notes:
|
||||||
- No its not perfect
|
- No its not perfect
|
||||||
- Yes it discards ALL stored procedures
|
- Yes it discards ALL stored procedures
|
||||||
- Yes it discards ALL queries except for CREATE TABLE and INSERT INTO
|
- Yes it discards ALL queries except for CREATE TABLE and INSERT INTO
|
||||||
- Yes you can email us suggestsions: info[AT]lightbox.org
|
|
||||||
- In emails, please include the Postgres code, and the expected MySQL code
|
|
||||||
- If you're having problems creating your postgres dump, make sure you use "--format p --inserts"
|
- If you're having problems creating your postgres dump, make sure you use "--format p --inserts"
|
||||||
- Default output engine if not specified is InnoDB
|
- Default output engine if not specified is InnoDB
|
||||||
|
|
||||||
XHTML;
|
XHTML;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getfieldname
|
||||||
|
*
|
||||||
|
* @param string $l String
|
||||||
|
* @return string|null Field name
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
function getfieldname($l)
|
function getfieldname($l)
|
||||||
{
|
{
|
||||||
//first check if its in nice quotes for us
|
// first check if its in nice quotes for us
|
||||||
$regs = array();
|
$regs = array();
|
||||||
if(preg_match("/`(.*)`/",$l,$regs))
|
if (preg_match("/`(.*)`/", $l, $regs)) {
|
||||||
{
|
if ($regs[1])
|
||||||
if($regs[1])
|
|
||||||
return $regs[1];
|
return $regs[1];
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
} // if its not in quotes, then it should (we hope!) be the first "word" on the line, up to the first space.
|
||||||
//if its not in quotes, then it should (we hope!) be the first "word" on the line, up to the first space.
|
else if (preg_match("/([^\ ]*)/", trim($l), $regs)) {
|
||||||
else if(preg_match("/([^\ ]*)/",trim($l),$regs))
|
if ($regs[1])
|
||||||
{
|
|
||||||
if($regs[1])
|
|
||||||
return $regs[1];
|
return $regs[1];
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatsize($s) {
|
|
||||||
if($s<pow(2,14))
|
/**
|
||||||
|
* formatsize
|
||||||
|
*
|
||||||
|
* @param string $s Size to format
|
||||||
|
* @return string Formated size
|
||||||
|
*/
|
||||||
|
function formatsize($s)
|
||||||
|
{
|
||||||
|
if ($s < pow(2, 14))
|
||||||
return "{$s}B";
|
return "{$s}B";
|
||||||
else if($s<pow(2,20))
|
else if ($s < pow(2, 20))
|
||||||
return sprintf("%.1f",round($s/1024,1))."K";
|
return sprintf("%.1f", round($s / 1024, 1)) . "K";
|
||||||
else if($s<pow(2,30))
|
else if ($s < pow(2, 30))
|
||||||
return sprintf("%.1f",round($s/1024/1024,1))."M";
|
return sprintf("%.1f", round($s / 1024 / 1024, 1)) . "M";
|
||||||
else
|
else
|
||||||
return sprintf("%.1f",round($s/1024/1024/1024,1))."G";
|
return sprintf("%.1f", round($s / 1024 / 1024 / 1024, 1)) . "G";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
function pg2mysql_large($infilename,$outfilename) {
|
* pg2mysql_large
|
||||||
$fs=filesize($infilename);
|
*
|
||||||
$infp=fopen($infilename,"rt");
|
* @param string $infilename Input filename
|
||||||
$outfp=fopen($outfilename,"wt");
|
* @param string $outfilename Output filename
|
||||||
|
* @return int <0 if KO, >=0 if OK
|
||||||
|
*/
|
||||||
|
function pg2mysql_large($infilename, $outfilename)
|
||||||
|
{
|
||||||
|
$infp = fopen($infilename, "rt");
|
||||||
|
$outfp = fopen($outfilename, "wt");
|
||||||
|
|
||||||
$outputatend = '';
|
$outputatend = '';
|
||||||
$arrayofprimaryalreadyintabledef = array();
|
$arrayofprimaryalreadyintabledef = array();
|
||||||
|
|
||||||
//we read until we get a semicolon followed by a newline (;\n);
|
// we read until we get a semicolon followed by a newline (;\n);
|
||||||
$pgsqlchunk=array();
|
$pgsqlchunk = array();
|
||||||
$chunkcount=1;
|
$chunkcount = 1;
|
||||||
$linenum=0;
|
$linenum = 0;
|
||||||
$inquotes=false;
|
$inquotes = false;
|
||||||
$first=true;
|
$first = true;
|
||||||
echo "Filesize: ".formatsize($fs)."\n";
|
|
||||||
|
|
||||||
while($instr=fgets($infp)) {
|
if (empty($infp)) {
|
||||||
$linenum++;
|
print 'Failed to open file '.$infilename."\n";
|
||||||
$memusage=round(memory_get_usage(true)/1024/1024);
|
return -1;
|
||||||
$len=strlen($instr);
|
}
|
||||||
$pgsqlchunk[]=$instr;
|
|
||||||
$c=substr_count($instr,"'");
|
$fs = filesize($infilename);
|
||||||
//we have an odd number of ' marks
|
echo "Filesize: " . formatsize($fs) . "\n";
|
||||||
if($c%2!=0) {
|
|
||||||
if($inquotes)
|
while ($instr = fgets($infp)) {
|
||||||
$inquotes=false;
|
$linenum ++;
|
||||||
|
$memusage = round(memory_get_usage(true) / 1024 / 1024);
|
||||||
|
$len = strlen($instr);
|
||||||
|
$pgsqlchunk[] = $instr;
|
||||||
|
$c = substr_count($instr, "'");
|
||||||
|
// we have an odd number of ' marks
|
||||||
|
if ($c % 2 != 0) {
|
||||||
|
if ($inquotes)
|
||||||
|
$inquotes = false;
|
||||||
else
|
else
|
||||||
$inquotes=true;
|
$inquotes = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $linenum%10000 == 0) {
|
if ($linenum % 10000 == 0) {
|
||||||
$currentpos=ftell($infp);
|
$currentpos = ftell($infp);
|
||||||
$percent=round($currentpos/$fs*100);
|
$percent = round($currentpos / $fs * 100);
|
||||||
$position=formatsize($currentpos);
|
$position = formatsize($currentpos);
|
||||||
printf("Reading progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r",$percent,$position,$linenum,$chunkcount,$memusage);
|
printf("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) {
|
if (strlen($instr) > 3 && ($instr[$len - 3] == ")" && $instr[$len - 2] == ";" && $instr[$len - 1] == "\n") && $inquotes == false) {
|
||||||
$chunkcount++;
|
$chunkcount ++;
|
||||||
|
|
||||||
if ($linenum % 10000 == 0) {
|
if ($linenum % 10000 == 0) {
|
||||||
$currentpos=ftell($infp);
|
$currentpos = ftell($infp);
|
||||||
$percent=round($currentpos/$fs*100);
|
$percent = round($currentpos / $fs * 100);
|
||||||
$position=formatsize($currentpos);
|
$position = formatsize($currentpos);
|
||||||
printf("Processing progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r",$percent,$position,$linenum,$chunkcount,$memusage);
|
printf("Processing progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r", $percent, $position, $linenum, $chunkcount, $memusage);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
echo "sending chunk:\n";
|
* echo "sending chunk:\n";
|
||||||
echo "=======================\n";
|
* echo "=======================\n";
|
||||||
print_r($pgsqlchunk);
|
* print_r($pgsqlchunk);
|
||||||
echo "=======================\n";
|
* echo "=======================\n";
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
foreach ($pgsqlchunk as $aaa) {
|
* foreach ($pgsqlchunk as $aaa) {
|
||||||
if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) {
|
* if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) {
|
||||||
var_dump($pgsqlchunk);
|
* var_dump($pgsqlchunk);
|
||||||
}
|
* }
|
||||||
}*/
|
* }
|
||||||
|
*/
|
||||||
|
|
||||||
$mysqlchunk=pg2mysql($pgsqlchunk, $arrayofprimaryalreadyintabledef, $first);
|
$mysqlchunk = pg2mysql($pgsqlchunk, $arrayofprimaryalreadyintabledef, $first);
|
||||||
fputs($outfp, $mysqlchunk['output']);
|
fputs($outfp, $mysqlchunk['output']);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
$break = false;
|
* $break = false;
|
||||||
foreach ($pgsqlchunk as $aaa) {
|
* foreach ($pgsqlchunk as $aaa) {
|
||||||
if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) {
|
* if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) {
|
||||||
var_dump($mysqlchunk);
|
* var_dump($mysqlchunk);
|
||||||
}
|
* }
|
||||||
if (preg_match('/MAIN_MAIL_SMTP_SE/', $aaa)) {
|
* if (preg_match('/MAIN_MAIL_SMTP_SE/', $aaa)) {
|
||||||
$break = true;
|
* $break = true;
|
||||||
}
|
* }
|
||||||
}
|
* }
|
||||||
if ($break) break;
|
* if ($break) break;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$outputatend.=$mysqlchunk['outputatend'];
|
$outputatend .= $mysqlchunk['outputatend'];
|
||||||
|
|
||||||
$first=false;
|
$first = false;
|
||||||
$pgsqlchunk=array();
|
$pgsqlchunk = array();
|
||||||
$mysqlchunk="";
|
$mysqlchunk = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
echo "\n\n";
|
echo "\n\n";
|
||||||
@ -201,366 +210,369 @@ function pg2mysql_large($infilename,$outfilename) {
|
|||||||
|
|
||||||
fputs($outfp, "\n");
|
fputs($outfp, "\n");
|
||||||
|
|
||||||
fputs($outfp, '/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;'."\n");
|
fputs($outfp, '/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;' . "\n");
|
||||||
fputs($outfp, '/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;'."\n");
|
fputs($outfp, '/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;' . "\n");
|
||||||
fputs($outfp, '/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;'."\n");
|
fputs($outfp, '/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;' . "\n");
|
||||||
fputs($outfp, '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;'."\n");
|
fputs($outfp, '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;' . "\n");
|
||||||
fputs($outfp, '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;'."\n");
|
fputs($outfp, '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;' . "\n");
|
||||||
fputs($outfp, '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;'."\n");
|
fputs($outfp, '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;' . "\n");
|
||||||
fputs($outfp, '/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;'."\n");;
|
fputs($outfp, '/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;' . "\n");
|
||||||
|
|
||||||
printf("Completed! %9d lines %9d sql chunks\n\n",$linenum,$chunkcount);
|
printf("Completed! %9d lines %9d sql chunks\n\n", $linenum, $chunkcount);
|
||||||
|
|
||||||
fclose($infp);
|
fclose($infp);
|
||||||
fclose($outfp);
|
fclose($outfp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
|
/**
|
||||||
|
* pg2mysql
|
||||||
|
*
|
||||||
|
* @param array $input Array of input
|
||||||
|
* @param array $arrayofprimaryalreadyintabledef Array of table already output with a primary key set into definition
|
||||||
|
* @param boolean $header Boolean
|
||||||
|
* @return string[] Array of output
|
||||||
|
*/
|
||||||
|
function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header = true)
|
||||||
{
|
{
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
if(is_array($input)) {
|
if (is_array($input)) {
|
||||||
$lines=$input;
|
$lines = $input;
|
||||||
} else {
|
} else {
|
||||||
$lines=split("\n",$input);
|
$lines = split("\n", $input);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($header) {
|
if ($header) {
|
||||||
$output = "-- Converted with ".PRODUCT."-".VERSION."\n";
|
$output = "-- Converted with " . PRODUCT . "-" . VERSION . "\n";
|
||||||
$output.= "-- Converted on ".date("r")."\n";
|
$output .= "-- Converted on " . date("r") . "\n";
|
||||||
$output.= "\n";
|
$output .= "\n";
|
||||||
|
|
||||||
$output.="/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n";
|
$output .= "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n";
|
||||||
$output.="/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n";
|
$output .= "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n";
|
||||||
$output.="/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n";
|
$output .= "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n";
|
||||||
$output.="/*!40101 SET NAMES utf8 */;\n";
|
$output .= "/*!40101 SET NAMES utf8 */;\n";
|
||||||
$output.="/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n";
|
$output .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n";
|
||||||
$output.="/*!40103 SET TIME_ZONE='+00:00' */;\n";
|
$output .= "/*!40103 SET TIME_ZONE='+00:00' */;\n";
|
||||||
$output.="/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n";
|
$output .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n";
|
||||||
$output.="/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n";
|
$output .= "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n";
|
||||||
$output.="/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n";
|
$output .= "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n";
|
||||||
$output.="/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n";
|
$output .= "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n";
|
||||||
$output.="\n";
|
$output .= "\n";
|
||||||
|
|
||||||
$outputatend="";
|
$outputatend = "";
|
||||||
}
|
} else {
|
||||||
else {
|
$output = "";
|
||||||
$output="";
|
$outputatend = "";
|
||||||
$outputatend="";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$in_create_table = $in_insert = FALSE;
|
$in_create_table = $in_insert = FALSE;
|
||||||
|
|
||||||
$linenumber=0;
|
$linenumber = 0;
|
||||||
$tbl_extra="";
|
$tbl_extra = "";
|
||||||
while(isset($lines[$linenumber])) {
|
while (isset($lines[$linenumber])) {
|
||||||
$line=$lines[$linenumber];
|
$line = $lines[$linenumber];
|
||||||
//$line =str_replace('ALTER TABLE public\.', '', $line);
|
// $line =str_replace('ALTER TABLE public\.', '', $line);
|
||||||
|
|
||||||
$reg = array();
|
$reg = array();
|
||||||
if(preg_match('/CREATE SEQUENCE (?:public\.)(.*)_(id|rowid|id_comment)_seq/', $line, $reg)) {
|
if (preg_match('/CREATE SEQUENCE (?:public\.)(.*)_(id|rowid|id_comment)_seq/', $line, $reg)) {
|
||||||
$outputatend.='-- Make field '.$reg[2].' auto_increment for table '.$reg[1]."\n";
|
$outputatend .= '-- Make field ' . $reg[2] . ' auto_increment for table ' . $reg[1] . "\n";
|
||||||
$outputatend.='ALTER TABLE '.$reg[1].' CHANGE COLUMN '.$reg[2].' '.$reg[2].' INTEGER NOT NULL AUTO_INCREMENT;'."\n\n";
|
$outputatend .= 'ALTER TABLE ' . $reg[1] . ' CHANGE COLUMN ' . $reg[2] . ' ' . $reg[2] . ' INTEGER NOT NULL AUTO_INCREMENT;' . "\n\n";
|
||||||
//var_dump($outputatend);
|
// var_dump($outputatend);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(substr($line,0,12)=="CREATE TABLE") {
|
if (substr($line, 0, 12) == "CREATE TABLE") {
|
||||||
$in_create_table=true;
|
$in_create_table = true;
|
||||||
$line=str_replace("\"", "`", $line);
|
$line = str_replace("\"", "`", $line);
|
||||||
$line=str_replace('public.', '', $line);
|
$line = str_replace('public.', '', $line);
|
||||||
|
|
||||||
$reg2= array();
|
$reg2 = array();
|
||||||
if (preg_match('/CREATE TABLE ([^\s]+)/', $line, $reg2)) {
|
if (preg_match('/CREATE TABLE ([^\s]+)/', $line, $reg2)) {
|
||||||
$in_create_table = $reg2[1];
|
$in_create_table = $reg2[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
$reg2= array();
|
$reg2 = array();
|
||||||
if (preg_match('/CREATE TABLE ([^\s]+)/', $line, $reg2)) {
|
if (preg_match('/CREATE TABLE ([^\s]+)/', $line, $reg2)) {
|
||||||
$output.='DROP TABLE IF EXISTS `'.$reg2[1].'`;'."\n";
|
$output .= 'DROP TABLE IF EXISTS `' . $reg2[1] . '`;' . "\n";
|
||||||
}
|
}
|
||||||
$output.=$line;
|
$output .= $line;
|
||||||
$linenumber++;
|
$linenumber ++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(substr($line,0,2)==");" && $in_create_table) {
|
if (substr($line, 0, 2) == ");" && $in_create_table) {
|
||||||
$in_create_table=false;
|
$in_create_table = false;
|
||||||
$line=") ENGINE={$config['engine']};\n\n";
|
$line = ") ENGINE={$config['engine']};\n\n";
|
||||||
|
|
||||||
$output.=$tbl_extra;
|
$output .= $tbl_extra;
|
||||||
$output.=$line;
|
$output .= $line;
|
||||||
|
|
||||||
$linenumber++;
|
$linenumber ++;
|
||||||
$tbl_extra="";
|
$tbl_extra = "";
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($in_create_table) {
|
if ($in_create_table) {
|
||||||
$regs = array();
|
$regs = array();
|
||||||
$line=str_replace("\"","`",$line);
|
$line = str_replace("\"", "`", $line);
|
||||||
$line=str_replace(" integer"," int(11)",$line);
|
$line = str_replace(" integer", " int(11)", $line);
|
||||||
$line=str_replace(" int_unsigned"," int(11) UNSIGNED",$line);
|
$line = str_replace(" int_unsigned", " int(11) UNSIGNED", $line);
|
||||||
$line=str_replace(" smallint_unsigned"," smallint UNSIGNED",$line);
|
$line = str_replace(" smallint_unsigned", " smallint UNSIGNED", $line);
|
||||||
$line=str_replace(" bigint_unsigned"," bigint UNSIGNED",$line);
|
$line = str_replace(" bigint_unsigned", " bigint UNSIGNED", $line);
|
||||||
$line=str_replace(" serial "," int(11) auto_increment ",$line);
|
$line = str_replace(" serial ", " int(11) auto_increment ", $line);
|
||||||
$line=str_replace(" bytea"," BLOB",$line);
|
$line = str_replace(" bytea", " BLOB", $line);
|
||||||
$line=str_replace(" boolean"," bool",$line);
|
$line = str_replace(" boolean", " bool", $line);
|
||||||
$line=str_replace(" bool DEFAULT true"," bool DEFAULT 1",$line);
|
$line = str_replace(" bool DEFAULT true", " bool DEFAULT 1", $line);
|
||||||
$line=str_replace(" bool DEFAULT false"," bool DEFAULT 0",$line);
|
$line = str_replace(" bool DEFAULT false", " bool DEFAULT 0", $line);
|
||||||
if(preg_match("/ character varying\(([0-9]*)\)/",$line,$regs)) {
|
if (preg_match("/ character varying\(([0-9]*)\)/", $line, $regs)) {
|
||||||
$num=$regs[1];
|
$num = $regs[1];
|
||||||
if($num<=255)
|
if ($num <= 255)
|
||||||
$line=preg_replace("/ character varying\([0-9]*\)/"," varchar($num)",$line);
|
$line = preg_replace("/ character varying\([0-9]*\)/", " varchar($num)", $line);
|
||||||
else
|
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)
|
// character varying with no size, we will default to varchar(255)
|
||||||
if(preg_match("/ character varying/",$line)) {
|
if (preg_match("/ character varying/", $line)) {
|
||||||
$line=preg_replace("/ character varying/"," varchar(255)",$line);
|
$line = preg_replace("/ character varying/", " varchar(255)", $line);
|
||||||
}
|
}
|
||||||
|
|
||||||
if( preg_match("/ DEFAULT \('([0-9]*)'::int/",$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)) {
|
||||||
preg_match("/ DEFAULT \('([0-9]*)'::smallint/",$line,$regs) ||
|
$num = $regs[1];
|
||||||
preg_match("/ DEFAULT \('([0-9]*)'::bigint/",$line,$regs)
|
$line = preg_replace("/ DEFAULT \('([0-9]*)'[^ ,]*/", " DEFAULT $num ", $line);
|
||||||
) {
|
|
||||||
$num=$regs[1];
|
|
||||||
$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];
|
$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("/ DEFAULT nextval\(.*\) /", " auto_increment ", $line);
|
||||||
$line=preg_replace("/::.*,/",",",$line);
|
$line = preg_replace("/::.*,/", ",", $line);
|
||||||
$line=preg_replace("/::.*$/","\n",$line);
|
$line = preg_replace("/::.*$/", "\n", $line);
|
||||||
if(preg_match("/character\(([0-9]*)\)/",$line,$regs)) {
|
if (preg_match("/character\(([0-9]*)\)/", $line, $regs)) {
|
||||||
$num=$regs[1];
|
$num = $regs[1];
|
||||||
if($num<=255)
|
if ($num <= 255)
|
||||||
$line=preg_replace("/ character\([0-9]*\)/"," varchar($num)",$line);
|
$line = preg_replace("/ character\([0-9]*\)/", " varchar($num)", $line);
|
||||||
else
|
else
|
||||||
$line=preg_replace("/ character\([0-9]*\)/"," text",$line);
|
$line = preg_replace("/ character\([0-9]*\)/", " text", $line);
|
||||||
}
|
}
|
||||||
//timestamps
|
// timestamps
|
||||||
$line=str_replace(" timestamp with time zone"," datetime",$line);
|
$line = str_replace(" timestamp with time zone", " datetime", $line);
|
||||||
$line=str_replace(" timestamp without time zone"," datetime",$line);
|
$line = str_replace(" timestamp without time zone", " datetime", $line);
|
||||||
|
|
||||||
//time
|
// time
|
||||||
$line=str_replace(" time with time zone"," time",$line);
|
$line = str_replace(" time with time zone", " time", $line);
|
||||||
$line=str_replace(" time without time zone"," time",$line);
|
$line = str_replace(" time without time zone", " time", $line);
|
||||||
|
|
||||||
$line=str_replace(" timestamp DEFAULT now()"," timestamp DEFAULT CURRENT_TIMESTAMP",$line);
|
$line = str_replace(" timestamp DEFAULT now()", " timestamp DEFAULT CURRENT_TIMESTAMP", $line);
|
||||||
$line=str_replace(" timestamp without time zone DEFAULT now()"," timestamp DEFAULT CURRENT_TIMESTAMP",$line);
|
$line = str_replace(" timestamp without time zone DEFAULT now()", " timestamp DEFAULT CURRENT_TIMESTAMP", $line);
|
||||||
|
|
||||||
if (strstr($line, "auto_increment") || preg_match('/ rowid int/', $line) || preg_match('/ id int/', $line)) {
|
if (strstr($line, "auto_increment") || preg_match('/ rowid int/', $line) || preg_match('/ id int/', $line)) {
|
||||||
$field=getfieldname($line);
|
$field = getfieldname($line);
|
||||||
$tbl_extra.=", PRIMARY KEY(`$field`)\n";
|
$tbl_extra .= ", PRIMARY KEY(`$field`)\n";
|
||||||
$arrayofprimaryalreadyintabledef[$in_create_table]=$in_create_table;
|
$arrayofprimaryalreadyintabledef[$in_create_table] = $in_create_table;
|
||||||
}
|
}
|
||||||
|
|
||||||
$specialfields=array("repeat","status","type","call");
|
$specialfields = array("repeat","status","type","call");
|
||||||
|
|
||||||
$field=getfieldname($line);
|
$field = getfieldname($line);
|
||||||
if(in_array($field,$specialfields)) {
|
if (in_array($field, $specialfields)) {
|
||||||
$line=str_replace("$field ","`$field` ",$line);
|
$line = str_replace("$field ", "`$field` ", $line);
|
||||||
}
|
}
|
||||||
|
|
||||||
//text/blob fields are not allowed to have a default, so if we find a text DEFAULT, change it to varchar(255) DEFAULT
|
// text/blob fields are not allowed to have a default, so if we find a text DEFAULT, change it to varchar(255) DEFAULT
|
||||||
if(strstr($line,"text DEFAULT")) {
|
if (strstr($line, "text DEFAULT")) {
|
||||||
$line=str_replace(" text DEFAULT "," varchar(255) DEFAULT ",$line);
|
$line = str_replace(" text DEFAULT ", " varchar(255) DEFAULT ", $line);
|
||||||
}
|
}
|
||||||
|
|
||||||
//just skip a CONSTRAINT line
|
// just skip a CONSTRAINT line
|
||||||
if(strstr($line," CONSTRAINT ")) {
|
if (strstr($line, " CONSTRAINT ")) {
|
||||||
$line="";
|
$line = "";
|
||||||
//and if the previous output ended with a , remove the ,
|
// and if the previous output ended with a , remove the ,
|
||||||
$lastchr=substr($output,-2,1);
|
$lastchr = substr($output, - 2, 1);
|
||||||
// echo "lastchr=$lastchr";
|
// echo "lastchr=$lastchr";
|
||||||
if($lastchr==",") {
|
if ($lastchr == ",") {
|
||||||
$output=substr($output,0,-2)."\n";
|
$output = substr($output, 0, - 2) . "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$output.=$line;
|
$output .= $line;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(substr($line,0,11)=="INSERT INTO") {
|
if (substr($line, 0, 11) == "INSERT INTO") {
|
||||||
$line = str_replace('public.', '', $line);
|
$line = str_replace('public.', '', $line);
|
||||||
|
|
||||||
if(substr($line,-3,-1)==");") {
|
if (substr($line, - 3, - 1) == ");") {
|
||||||
//we have a complete insert on one line
|
// we have a complete insert on one line
|
||||||
list($before,$after)=explode(" VALUES ", $line, 2);
|
list ($before, $after) = explode(" VALUES ", $line, 2);
|
||||||
//we only replace the " with ` in what comes BEFORE the VALUES
|
// we only replace the " with ` in what comes BEFORE the VALUES
|
||||||
//(ie, field names, like INSERT INTO table ("bla","bla2") VALUES ('s:4:"test"','bladata2');
|
// (ie, field names, like INSERT INTO table ("bla","bla2") VALUES ('s:4:"test"','bladata2');
|
||||||
//should convert to INSERT INTO table (`bla`,`bla2`) VALUES ('s:4:"test"','bladata2');
|
// should convert to INSERT INTO table (`bla`,`bla2`) VALUES ('s:4:"test"','bladata2');
|
||||||
|
|
||||||
$before=str_replace("\"","`",$before);
|
$before = str_replace("\"", "`", $before);
|
||||||
|
|
||||||
//in after, we need to watch out for escape format strings, ie (E'escaped \r in a string'), and ('bla',E'escaped \r in a string'), but could also be (number, E'string'); so we cant search for the previoous '
|
// in after, we need to watch out for escape format strings, ie (E'escaped \r in a string'), and ('bla',E'escaped \r in a string'), but could also be (number, E'string'); so we cant search for the previoous '
|
||||||
//ugh i guess its possible these strings could exist IN the data as well, but the only way to solve that is to process these lines one character
|
// ugh i guess its possible these strings could exist IN the data as well, but the only way to solve that is to process these lines one character
|
||||||
//at a time, and thats just stupid, so lets just hope this doesnt appear anywhere in the actual data
|
// at a time, and thats just stupid, so lets just hope this doesnt appear anywhere in the actual data
|
||||||
$after=str_replace(" (E'"," ('",$after);
|
$after = str_replace(" (E'", " ('", $after);
|
||||||
$after=str_replace(", E'",", '",$after);
|
$after = str_replace(", E'", ", '", $after);
|
||||||
|
|
||||||
$output.=$before." VALUES ".$after;
|
$output .= $before . " VALUES " . $after;
|
||||||
$linenumber++;
|
$linenumber ++;
|
||||||
continue;
|
continue;
|
||||||
}
|
} else {
|
||||||
else {
|
// this insert spans multiple lines, so keep dumping the lines until we reach a line
|
||||||
//this insert spans multiple lines, so keep dumping the lines until we reach a line
|
// that ends with ");"
|
||||||
//that ends with ");"
|
|
||||||
|
|
||||||
list($before,$after)=explode(" VALUES ", $line, 2);
|
list ($before, $after) = explode(" VALUES ", $line, 2);
|
||||||
//we only replace the " with ` in what comes BEFORE the VALUES
|
// we only replace the " with ` in what comes BEFORE the VALUES
|
||||||
//(ie, field names, like INSERT INTO table ("bla","bla2") VALUES ('s:4:"test"','bladata2');
|
// (ie, field names, like INSERT INTO table ("bla","bla2") VALUES ('s:4:"test"','bladata2');
|
||||||
//should convert to INSERT INTO table (`bla`,`bla2`) VALUES ('s:4:"test"','bladata2');
|
// should convert to INSERT INTO table (`bla`,`bla2`) VALUES ('s:4:"test"','bladata2');
|
||||||
|
|
||||||
$before=str_replace("\"","`",$before);
|
$before = str_replace("\"", "`", $before);
|
||||||
|
|
||||||
//in after, we need to watch out for escape format strings, ie (E'escaped \r in a string'), and ('bla',E'escaped \r in a string')
|
// in after, we need to watch out for escape format strings, ie (E'escaped \r in a string'), and ('bla',E'escaped \r in a string')
|
||||||
//ugh i guess its possible these strings could exist IN the data as well, but the only way to solve that is to process these lines one character
|
// ugh i guess its possible these strings could exist IN the data as well, but the only way to solve that is to process these lines one character
|
||||||
//at a time, and thats just stupid, so lets just hope this doesnt appear anywhere in the actual data
|
// at a time, and thats just stupid, so lets just hope this doesnt appear anywhere in the actual data
|
||||||
$after=str_replace(" (E'"," ('",$after);
|
$after = str_replace(" (E'", " ('", $after);
|
||||||
$after=str_replace(", E'",", '",$after);
|
$after = str_replace(", E'", ", '", $after);
|
||||||
|
|
||||||
$c=substr_count($line,"'");
|
$c = substr_count($line, "'");
|
||||||
//we have an odd number of ' marks
|
// we have an odd number of ' marks
|
||||||
if($c%2!=0) {
|
if ($c % 2 != 0) {
|
||||||
$inquotes=true;
|
$inquotes = true;
|
||||||
|
} else {
|
||||||
|
$inquotes = false;
|
||||||
}
|
}
|
||||||
else $inquotes=false;
|
|
||||||
|
|
||||||
$output.=$before." VALUES ".$after;
|
$output .= $before . " VALUES " . $after;
|
||||||
do{
|
do {
|
||||||
$linenumber++;
|
$linenumber ++;
|
||||||
|
|
||||||
//in after, we need to watch out for escape format strings, ie (E'escaped \r in a string'), and ('bla',E'escaped \r in a string')
|
// in after, we need to watch out for escape format strings, ie (E'escaped \r in a string'), and ('bla',E'escaped \r in a string')
|
||||||
//ugh i guess its possible these strings could exist IN the data as well, but the only way to solve that is to process these lines one character
|
// ugh i guess its possible these strings could exist IN the data as well, but the only way to solve that is to process these lines one character
|
||||||
//at a time, and thats just stupid, so lets just hope this doesnt appear anywhere in the actual data
|
// at a time, and thats just stupid, so lets just hope this doesnt appear anywhere in the actual data
|
||||||
|
|
||||||
//after the first line, we only need to check for it in the middle, not at the beginning of an insert (becuase the beginning will be on the first line)
|
// after the first line, we only need to check for it in the middle, not at the beginning of an insert (becuase the beginning will be on the first line)
|
||||||
//$after=str_replace(" (E'","' ('",$after);
|
// $after=str_replace(" (E'","' ('",$after);
|
||||||
$line=$lines[$linenumber];
|
$line = $lines[$linenumber];
|
||||||
$line=str_replace("', E'","', '",$line);
|
$line = str_replace("', E'", "', '", $line);
|
||||||
$output.=$line;
|
$output .= $line;
|
||||||
|
|
||||||
// printf("inquotes: %d linenumber: %4d line: %s\n",$inquotes,$linenumber,$lines[$linenumber]);
|
// printf("inquotes: %d linenumber: %4d line: %s\n",$inquotes,$linenumber,$lines[$linenumber]);
|
||||||
|
|
||||||
$c=substr_count($line,"'");
|
$c = substr_count($line, "'");
|
||||||
//we have an odd number of ' marks
|
// we have an odd number of ' marks
|
||||||
if($c%2!=0) {
|
if ($c % 2 != 0) {
|
||||||
if($inquotes) $inquotes=false;
|
if ($inquotes)
|
||||||
else $inquotes=true;
|
$inquotes = false;
|
||||||
// echo "inquotes=$inquotes\n";
|
else
|
||||||
|
$inquotes = true;
|
||||||
|
// echo "inquotes=$inquotes\n";
|
||||||
}
|
}
|
||||||
|
} while (substr($lines[$linenumber], - 3, - 1) != ");" || $inquotes);
|
||||||
} while(substr($lines[$linenumber],-3,-1)!=");" || $inquotes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(substr($line,0,16)=="ALTER TABLE ONLY") {
|
if (substr($line, 0, 16) == "ALTER TABLE ONLY") {
|
||||||
$line=preg_replace('/ ONLY/', '', $line);
|
$line = preg_replace('/ ONLY/', '', $line);
|
||||||
$line=str_replace("\"", "`", $line);
|
$line = str_replace("\"", "`", $line);
|
||||||
$line=str_replace("public.", "", $line);
|
$line = str_replace("public.", "", $line);
|
||||||
$pkey=$line;
|
$pkey = $line;
|
||||||
|
|
||||||
$linenumber++;
|
$linenumber ++;
|
||||||
if (! empty($lines[$linenumber])) {
|
if (! empty($lines[$linenumber])) {
|
||||||
$line = $lines[$linenumber];
|
$line = $lines[$linenumber];
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$line = '';
|
$line = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if(strstr($line," PRIMARY KEY ") && substr($line,-3,-1)==");") {
|
if (strstr($line, " PRIMARY KEY ") && substr($line, - 3, - 1) == ");") {
|
||||||
$reg2 = array();
|
$reg2 = array();
|
||||||
if (preg_match('/ALTER TABLE ([^\s]+)/', $pkey, $reg2)) {
|
if (preg_match('/ALTER TABLE ([^\s]+)/', $pkey, $reg2)) {
|
||||||
if (empty($arrayofprimaryalreadyintabledef[$reg2[1]])) {
|
if (empty($arrayofprimaryalreadyintabledef[$reg2[1]])) {
|
||||||
//looks like we have a single line PRIMARY KEY definition, lets go ahead and add it
|
// looks like we have a single line PRIMARY KEY definition, lets go ahead and add it
|
||||||
$output.=str_replace("\n", "", $pkey);
|
$output .= str_replace("\n", "", $pkey);
|
||||||
//the postgres and mysql syntax for this is (at least, in the example im looking at)
|
// 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.
|
// identical, so we can just add it as is.
|
||||||
$output.=$line."\n";
|
$output .= $line . "\n";
|
||||||
} else {
|
} else {
|
||||||
$output.='-- '.str_replace("\n", "", $pkey);
|
$output .= '-- ' . str_replace("\n", "", $pkey);
|
||||||
$output.='-- '.$line."\n";
|
$output .= '-- ' . $line . "\n";
|
||||||
}
|
}
|
||||||
} else
|
} else {
|
||||||
{
|
$output .= '-- ' . str_replace("\n", "", $pkey);
|
||||||
$output.='-- '.str_replace("\n", "", $pkey);
|
$output .= '-- ' . $line . "\n";
|
||||||
$output.='-- '.$line."\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//while we're here, we might as well catch CREATE INDEX as well
|
// while we're here, we might as well catch CREATE INDEX as well
|
||||||
if(substr($line,0,12)=="CREATE INDEX") {
|
if (substr($line, 0, 12) == "CREATE INDEX") {
|
||||||
$matches = array();
|
$matches = array();
|
||||||
preg_match('/CREATE INDEX "?([a-zA-Z0-9_]*)"? ON "?([a-zA-Z0-9_\.]*)"? USING btree \((.*)\);/',$line,$matches);
|
preg_match('/CREATE INDEX "?([a-zA-Z0-9_]*)"? ON "?([a-zA-Z0-9_\.]*)"? USING btree \((.*)\);/', $line, $matches);
|
||||||
if (! empty($matches[3])) {
|
if (! empty($matches[3])) {
|
||||||
$indexname=$matches[1];
|
$indexname = $matches[1];
|
||||||
$tablename=str_replace('public.', '', $matches[2]);
|
$tablename = str_replace('public.', '', $matches[2]);
|
||||||
$columns=$matches[3];
|
$columns = $matches[3];
|
||||||
if($tablename && $columns) {
|
if ($tablename && $columns) {
|
||||||
$output.="ALTER TABLE `".$tablename."` ADD INDEX ".$indexname."( {$columns} ) ;\n";
|
$output .= "ALTER TABLE `" . $tablename . "` ADD INDEX " . $indexname . "( {$columns} ) ;\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(substr($line,0,19)=="CREATE UNIQUE INDEX") {
|
if (substr($line, 0, 19) == "CREATE UNIQUE INDEX") {
|
||||||
$matches = array();
|
$matches = array();
|
||||||
preg_match('/CREATE UNIQUE INDEX "?([a-zA-Z0-9_]*)"? ON "?([a-zA-Z0-9_\.]*)"? USING btree \((.*)\);/',$line,$matches);
|
preg_match('/CREATE UNIQUE INDEX "?([a-zA-Z0-9_]*)"? ON "?([a-zA-Z0-9_\.]*)"? USING btree \((.*)\);/', $line, $matches);
|
||||||
if (! empty($matches[3])) {
|
if (! empty($matches[3])) {
|
||||||
$indexname=$matches[1];
|
$indexname = $matches[1];
|
||||||
$tablename=str_replace('public.', '', $matches[2]);
|
$tablename = str_replace('public.', '', $matches[2]);
|
||||||
$columns=str_replace('"', '', $matches[3]);
|
$columns = str_replace('"', '', $matches[3]);
|
||||||
if($tablename && $columns) {
|
if ($tablename && $columns) {
|
||||||
$output.="ALTER TABLE `".$tablename."` ADD UNIQUE INDEX ".$indexname." ( {$columns} ) ;\n";
|
$output .= "ALTER TABLE `" . $tablename . "` ADD UNIQUE INDEX " . $indexname . " ( {$columns} ) ;\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(substr($line, 0, 13) == 'DROP DATABASE')
|
if (substr($line, 0, 13) == 'DROP DATABASE')
|
||||||
$output .= $line;
|
$output .= $line;
|
||||||
|
|
||||||
if(substr($line, 0, 15) == 'CREATE DATABASE') {
|
if (substr($line, 0, 15) == 'CREATE DATABASE') {
|
||||||
$matches = array();
|
$matches = array();
|
||||||
preg_match('/CREATE DATABASE ([a-zA-Z0-9_]*) .* ENCODING = \'(.*)\'/', $line, $matches);
|
preg_match('/CREATE DATABASE ([a-zA-Z0-9_]*) .* ENCODING = \'(.*)\'/', $line, $matches);
|
||||||
$output .= "CREATE DATABASE `$matches[1]` DEFAULT CHARACTER SET $matches[2];\n\n";
|
$output .= "CREATE DATABASE `$matches[1]` DEFAULT CHARACTER SET $matches[2];\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(substr($line, 0, 8) == '\\connect') {
|
if (substr($line, 0, 8) == '\\connect') {
|
||||||
$matches = array();
|
$matches = array();
|
||||||
preg_match('/connect ([a-zA-Z0-9_]*)/', $line, $matches);
|
preg_match('/connect ([a-zA-Z0-9_]*)/', $line, $matches);
|
||||||
$output .= "USE `$matches[1]`;\n\n";
|
$output .= "USE `$matches[1]`;\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if(substr($line, 0, 5) == 'COPY ') {
|
if (substr($line, 0, 5) == 'COPY ') {
|
||||||
$matches = array();
|
$matches = array();
|
||||||
preg_match('/COPY (.*) FROM stdin/', $line, $matches);
|
preg_match('/COPY (.*) FROM stdin/', $line, $matches);
|
||||||
$heads = str_replace('"', "`", $matches[1]);
|
$heads = str_replace('"', "`", $matches[1]);
|
||||||
$values = array();
|
$values = array();
|
||||||
$in_insert = TRUE;
|
$in_insert = TRUE;
|
||||||
} elseif($in_insert) {
|
} elseif ($in_insert) {
|
||||||
if($line == "\\.\n") {
|
if ($line == "\\.\n") {
|
||||||
$in_insert = FALSE;
|
$in_insert = FALSE;
|
||||||
if($values) $output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n\n";
|
if ($values) {
|
||||||
|
$output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n\n";
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$vals = explode(' ', $line);
|
$vals = explode(' ', $line);
|
||||||
foreach($vals as $i => $val) {
|
foreach ($vals as $i => $val) {
|
||||||
$vals[$i] = ($val == '\\N')
|
$vals[$i] = ($val == '\\N') ? 'NULL' : "'" . str_replace("'", "\\'", trim($val)) . "'";
|
||||||
? 'NULL'
|
|
||||||
: "'" . str_replace("'", "\\'", trim($val)) . "'";
|
|
||||||
}
|
}
|
||||||
$values[] = '(' . implode(',', $vals) . ')';
|
$values[] = '(' . implode(',', $vals) . ')';
|
||||||
if(count($values) >= 1000) {
|
if (count($values) >= 1000) {
|
||||||
$output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n";
|
$output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n";
|
||||||
$values = array();
|
$values = array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$linenumber++;
|
$linenumber ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array('output' => $output, 'outputatend' => $outputatend);
|
return array('output' => $output,'outputatend' => $outputatend);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user