--- pgsql2interbase 2000/07/31 08:55:32 1.2 +++ pgsql2interbase 2002/02/22 17:21:47 1.5 @@ -3,12 +3,13 @@ # PostgreSQL to InterBase dump file converter # # usage: -# cat pg_db_name | ./mysql2pgsql > interbase.sql +# cat pg_db_name | ./pgsql2interbase > interbase.sql # # Convert PostgresSQL database dump file to something readable by isql ! # 2000-08-30 DbP -- Dobrica Pavlinusic -# based on mysql2pgsql +# based on mysql2pgsql changes are on: +# http://cvs.linux.hr/cvsweb.cgi/sql/pgsql2interbase # # Warning: bool datatype is converted to char(1) which will break # your application if you tend to check true values with @@ -22,14 +23,24 @@ $table=""; $triggers=""; # create triggers +$|=1; + while(<>) { chomp; +# warn "- $create:$_-\n"; + + if (/CREATE\s+TABLE\s+"([^"]+)"/i) { + $table=$1 if (defined($1)); + $create++; + s/CREATE\s+TABLE\s+"[^"]+"/create table $table/i; + } + next if (/^\\connect/); if ($create) { # are we inside create table? - if (/DEFAULT nextval \( '"([^"]+)_seq"' \)/i) { + if (/DEFAULT\s+nextval\s*\(\s*['"]+([^"']+)_seq["']+\s*\)/i) { $trig=$col=$1; $col=~s/(\w+)_([^_]+)/$1.$2/; $triggers.=" @@ -41,8 +52,14 @@ end !! set term ; !! "; - s/ DEFAULT nextval \( '"[^"]+"' \)//i; + $generator{$trig}--; + + s/DEFAULT\s+nextval\s*\([^\)]+\)//i; } + + die "nextval not removed!" if (/nextval/); + + # nuke bool type definition on default s/DEFAULT bool/DEFAULT/i; # int(48...) -> int @@ -51,7 +68,7 @@ # bool -> char(1) if (/bool/i) { s/\w*bool/ char(1)/gi; - print STDERR "Warning: bool emulated by char(1)\n\n"; + warn "Warning: bool emulated by char(1)\n\n"; } # datetime -> timestamp @@ -63,6 +80,7 @@ my ($gen,$start) = ($1,$2); $sql.="create generator ${gen}_gen ;\n"; $sql.="set generator ${gen}_gen to $start ;\n"; + $generator{$gen}++; # to find unused generators next; } @@ -71,20 +89,20 @@ # you will have to re-write functions manually! if (/^CREATE FUNCTION/i) { - print STDERR "functions not supported: $_\n\n"; + warn "functions not supported: $_\n\n"; next; } # rule is usually a defined view if (/^CREATE RULE/i) { - print STDERR "rules (views...) not supported: $_\n\n"; + warn "rules (views...) not supported: $_\n\n"; next; } if (/COPY "([^"]+)" FROM stdin/i) { my $table=$1; my $line=<>; chomp $line; while($line ne "\\.") { - $sql.="insert into \"$table\" values ("; + $sql.="insert into $table values ("; undef @newarr; foreach $var (split(/\t/,$line)) { if ($var eq "\\N") { @@ -107,34 +125,50 @@ if (/(CREATE \w*\s*INDEX "[^"]+" on "[^"]+")[^(]*\(([^\)]+)\)/i) { my ($ind,$col) = ($1,$2); $col=~s/" "[^"]+"/"/g; # nuke ops_name - $sql.="$ind ( $col );\n"; + $sql.=lc($ind)." ( $col );\n"; next; } + if (/GRANT (.+) on "([^"]+)" to ([^;]+);/i) { + ($what,$table,$user) = ($1,$2,$3); + $user=~s/"//g; + if ($user =~ /group\s+(\S+)/i) { + $sql.="grant $what on $table to group $1 ;\n"; + $groups{$1}++; + } else { + $sql.="grant $what on $table to $user ;\n"; + $users{$user}++; + } + next ; + } + } + if ($create && /\);/) { $create-- } $sql.="$_\n"; - if (/CREATE TABLE "([^"]+)"/i) { - $table=$1 if (defined($1)); - $create++; - } while ($sql=~/;/) { ($dosql,$sql)=split(/;/,$sql,2); $dosql.=";"; # nuked by split, put it back! if ("$dosql" ne "") { + $dosql=~s/"([^"]+)"/$1/g; # nuke quotes print "$dosql\n"; - $create=0; } else { - print STDERR "empty sql!\n"; + warn "empty sql!\n"; } } } +$sql=~s/"([^"]+)"/$1/g; # nuke quotes print "$sql\n$triggers\n"; -print "\n"; +foreach $gen (keys %generator) { + warn "created, but overused/unused generator: $gen (ref.count: $generator{$gen})\n" if ($generator{$gen} != 0); +} + +warn "Users used: ",join(", ",keys(%users)),"\n"; +warn "Groups used: ",join(", ",keys(%groups)),"\n";