--- pgsql2interbase 2000/07/31 06:19:20 1.1 +++ pgsql2interbase 2000/10/10 08:04:40 1.4 @@ -8,20 +8,39 @@ # 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 +# if ($foo) +# and not with +# if ($foo = 't') +# In data from InterBase it will always return true which is wrong! +# $create=0; # inside create table? $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.=" @@ -33,15 +52,24 @@ 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 s/\w*int\d+/ int/gi; # bool -> char(1) - s/\w*bool/ char(1)/gi; + if (/bool/i) { + s/\w*bool/ char(1)/gi; + warn "Warning: bool emulated by char(1)\n\n"; + } # datetime -> timestamp s/datetime/timestamp/gi; @@ -52,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; } @@ -60,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") { @@ -96,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";