| version 1.4, 2005/04/26 08:49:29 |
version 1.5, 2005/04/26 10:43:59 |
|
|
| # - in vim editor use this regexp: /\('\([^'\\]\|\(\\.\)\|\(\\\\\)\)*'\) |
# - in vim editor use this regexp: /\('\([^'\\]\|\(\\.\)\|\(\\\\\)\)*'\) |
| # |
# |
| |
|
| # $Platon: scripts/perl/mysql/mysqldump-convert.pl,v 1.3 2004/10/14 13:28:35 rajo Exp $ |
# $Platon: scripts/perl/mysql/mysqldump-convert.pl,v 1.4 2005/04/26 08:49:29 rajo Exp $ |
| |
|
| use strict; |
use strict; |
| |
|
| my ($insert_t, $column_t, $value_t) = (1, 1, 1); |
my ($insert_t, $create_t, $column_t, $value_t) = (1, 1, 1, 1); |
| |
|
| $| = 1; |
$| = 1; |
| |
|
| |
my $f_column_names = 0; |
| |
|
| |
$f_column_names = 1 if (scalar(@ARGV) > 0 and $ARGV[0] eq '--add-column-names'); |
| |
|
| my $input = ''; |
my $input = ''; |
| |
my @column_names = ( ); |
| |
|
| while (my $line = <STDIN>) { |
while (my $line = <STDIN>) { |
| $input .= $line; # read and add next line into buffer |
$input .= $line; # read and add next line into buffer |
| |
|
| #print "INPUT: $input"; |
#print "INPUT: $input"; |
| if ($input =~ s/^(\s*INSERT\s+INTO\s\S+\s+.*VALUES\s+)//mg) { |
|
| |
if ($f_column_names and $input =~ s/^(\s*CREATE\s+TABLE\s\S+\s+\(\s*)/(/mi) { # {{{ |
| |
# first separate begin of insert command |
| |
print $1; |
| |
$create_t = 0; |
| |
$column_t = 0; |
| |
my $values = ''; |
| |
while ($create_t == 0 && $input =~ s/^\(//m) { |
| |
while ($column_t == 0) { |
| |
if ($input =~ s/^(\s*(?:PRIMARY\s+)?KEY\s+\([^\)]+\).*)//mi) { |
| |
$column_t = 0; |
| |
$create_t = 0; |
| |
print $1; |
| |
} |
| |
elsif ($input =~ s/^(\s*\)[^;]*;)\s*//m) { # end of create table |
| |
$column_t = 1; |
| |
$create_t = 1; |
| |
print $1; |
| |
} |
| |
elsif ($input =~ s/^(\s*)([^`,)]+|`(?:[^`\\]|(?:\\.)|(?:\\\\))*`)(\s*)//m) { |
| |
print $1, $2, $3; |
| |
push @column_names, $2; |
| |
} |
| |
if ($input =~ s/^([^,]*),(\s*)//m) { |
| |
$column_t = 0; |
| |
$create_t = 0; |
| |
print "$1,$2"; |
| |
} |
| |
else { # line end but columns/values list continues on the next line |
| |
# following line is fix for endless loop, DON'T REMOVE them !!! |
| |
$input .= <STDIN>; |
| |
} |
| |
} |
| |
} |
| |
$column_t = 1; |
| |
# get first column |
| |
} # }}} |
| |
elsif ($input =~ s/^(\s*INSERT\s+INTO\s\S+\s+.*VALUES\s+)//mi) { # {{{ |
| # first separate begin of insert command |
# first separate begin of insert command |
| my $insert_command = $1; # save sql insert |
my $insert_command = $1; # save sql insert |
| |
|
| |
#use Data::Dumper; |
| |
#print Dumper(\@column_names); |
| |
|
| $insert_t = 0; |
$insert_t = 0; |
| $column_t = 0; |
$column_t = 0; |
| |
my $column_count = 0; |
| my $values = ''; |
my $values = ''; |
| while ($insert_t == 0 && $input =~ s/^\(//m) { |
while ($insert_t == 0 && $input =~ s/^\(//m) { |
| while ($column_t == 0) { |
while ($column_t == 0) { |
| if ($input =~ s/^\s*([^',)]+|'(?:[^'\\]|(?:\\.)|(?:\\\\))*')\s*//m) { |
if ($input =~ s/^\s*([^',)]+|'(?:[^'\\]|(?:\\.)|(?:\\\\))*')\s*//m) { |
| my $value = $1; |
my $value = $1; |
| $values .= $value; |
$values .= $f_column_names ? "\n/* $column_names[$column_count] */\t$value" : $value; |
| #print "value = \"$value\"\n"; |
$column_count++; |
| } |
} |
| if ($input =~ s/^,\s*//m) { |
if ($input =~ s/^,\s*//m) { |
| $column_t = 0; |
$column_t = 0; |
| Line 57 while (my $line = <STDIN>) { |
|
| Line 105 while (my $line = <STDIN>) { |
|
| $insert_t = 0; |
$insert_t = 0; |
| print "$insert_command($values);\n"; |
print "$insert_command($values);\n"; |
| $values = ''; |
$values = ''; |
| |
$column_count = 0; |
| } |
} |
| elsif ($input =~ s/^\s*\)\s*;\s*//m) { # end of insert |
elsif ($input =~ s/^\s*\)\s*;\s*//m) { # end of insert |
| $column_t = 1; |
$column_t = 1; |
| Line 72 while (my $line = <STDIN>) { |
|
| Line 121 while (my $line = <STDIN>) { |
|
| } |
} |
| $column_t = 1; |
$column_t = 1; |
| # get first column |
# get first column |
| } |
} # }}} |
| else { # print and clear input buffer |
else { # print and clear input buffer |
| print $input; |
print $input; |
| $input = ''; |
$input = ''; |