« blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm
↑ Summary
blib/lib/SQL/Translator/Parser/DBI/Sybase.pm »
| 1 | package SQL::Translator::Parser::DBI::DB2; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | SQL::Translator::Parser::DBI::DB2 - parser for DBD::DB2 | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | See SQL::Translator::Parser::DBI. | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Uses DBI methods to determine schema structure. DBI, of course, | ||
| 14 | delegates to DBD::DB2. | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use DBI; | ||
| 21 | use Data::Dumper; | ||
| 22 | use SQL::Translator::Parser::DB2; | ||
| 23 | use SQL::Translator::Schema::Constants; | ||
| 24 | |||
| 25 | our ($DEBUG, $VERSION, @EXPORT_OK); | ||
| 26 | |||
| 27 | # $VERSION = '1.66'; | ||
| 28 | $DEBUG = 0 unless defined $DEBUG; | ||
| 29 | |||
| 30 | sub parse { | ||
| 31 | my ($tr, $dbh) = @_; | ||
| 32 | |||
| 33 | my $schema = $tr->schema; | ||
| 34 | |||
| 35 | my ($sth, @tables, $columns); | ||
| 36 | my $stuff; | ||
| 37 | |||
| 38 | if ($dbh->{FetchHashKeyName} ne 'NAME_uc') { | ||
| 39 | $dbh->{FetchHashKeyName} = 'NAME_uc'; | ||
| 40 | } | ||
| 41 | |||
| 42 | if ($dbh->{ChopBlanks} != 1) { | ||
| 43 | $dbh->{ChopBlanks} = 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | my $tabsth = $dbh->prepare(<<SQL); | ||
| 47 | SELECT t.TABSCHEMA, | ||
| 48 | t.TABNAME, | ||
| 49 | t.TYPE, | ||
| 50 | ts.TBSPACE | ||
| 51 | FROM SYSCAT.TABLES t | ||
| 52 | JOIN SYSCAT.TABLESPACES ts ON t.TBSPACEID = ts.TBSPACEID | ||
| 53 | WHERE t.TABSCHEMA NOT LIKE 'SYS%' | ||
| 54 | ORDER BY t.TABNAME ASC | ||
| 55 | SQL | ||
| 56 | |||
| 57 | # $sth = $dbh->table_info(); | ||
| 58 | # @tables = @{$sth->fetchall_arrayref({})}; | ||
| 59 | |||
| 60 | my $colsth = $dbh->prepare(<<SQL); | ||
| 61 | SELECT c.TABSCHEMA, | ||
| 62 | c.TABNAME, | ||
| 63 | c.COLNAME, | ||
| 64 | c.TYPENAME, | ||
| 65 | c.LENGTH, | ||
| 66 | c.DEFAULT, | ||
| 67 | c.NULLS, | ||
| 68 | c.COLNO | ||
| 69 | FROM SYSCAT.COLUMNS c | ||
| 70 | WHERE c.TABSCHEMA NOT LIKE 'SYS%' AND | ||
| 71 | c.TABNAME = ? | ||
| 72 | ORDER BY COLNO | ||
| 73 | SQL | ||
| 74 | |||
| 75 | my $consth = $dbh->prepare(<<SQL); | ||
| 76 | SELECT tc.TABSCHEMA, | ||
| 77 | tc.TABNAME, | ||
| 78 | kc.CONSTNAME, | ||
| 79 | kc.COLNAME, | ||
| 80 | tc.TYPE, | ||
| 81 | tc.CHECKEXISTINGDATA | ||
| 82 | FROM SYSCAT.TABCONST tc | ||
| 83 | JOIN SYSCAT.KEYCOLUSE kc ON tc.CONSTNAME = kc.CONSTNAME AND | ||
| 84 | tc.TABSCHEMA = kc.TABSCHEMA AND | ||
| 85 | tc.TABNAME = kc.TABNAME | ||
| 86 | WHERE tc.TABSCHEMA NOT LIKE 'SYS%' AND | ||
| 87 | tc.TABNAME = ? | ||
| 88 | SQL | ||
| 89 | |||
| 90 | my $indsth = $dbh->prepare(<<SQL); | ||
| 91 | SELECT i.INDSCHEMA, | ||
| 92 | i.INDNAME, | ||
| 93 | i.TABSCHEMA, | ||
| 94 | i.TABNAME, | ||
| 95 | i.UNIQUERULE, | ||
| 96 | i.INDEXTYPE, | ||
| 97 | ic.COLNAME | ||
| 98 | FROM SYSCAT.INDEXES i | ||
| 99 | JOIN SYSCAT.INDEXCOLUSE ic ON i.INDSCHEMA = ic.INDSCHEMA AND | ||
| 100 | i.INDNAME = ic.INDNAME | ||
| 101 | WHERE i.TABSCHEMA NOT LIKE 'SYS%' AND | ||
| 102 | i.INDEXTYPE <> 'P' AND | ||
| 103 | i.TABNAME = ? | ||
| 104 | SQL | ||
| 105 | |||
| 106 | my $trigsth = $dbh->prepare(<<SQL); | ||
| 107 | SELECT t.TRIGSCHEMA, | ||
| 108 | t.TRIGNAME, | ||
| 109 | t.TABSCHEMA, | ||
| 110 | t.TRIGTIME, | ||
| 111 | t.TRIGEVENT, | ||
| 112 | t.GRANULARITY, | ||
| 113 | t.TEXT | ||
| 114 | FROM SYSCAT.TRIGGERS t | ||
| 115 | WHERE t.TABSCHEMA NOT LIKE 'SYS%' AND | ||
| 116 | t.TABNAME = ? | ||
| 117 | SQL | ||
| 118 | |||
| 119 | $tabsth->execute(); | ||
| 120 | @tables = @{ $tabsth->fetchall_arrayref({}) }; | ||
| 121 | |||
| 122 | foreach my $table_info (@tables) { | ||
| 123 | next | ||
| 124 | unless (defined($table_info->{TYPE})); | ||
| 125 | |||
| 126 | # Why are we not getting system tables, maybe a parameter should decide? | ||
| 127 | |||
| 128 | if ( $table_info->{TYPE} eq 'T' | ||
| 129 | && $table_info->{TABSCHEMA} !~ /^SYS/) { | ||
| 130 | print Dumper($table_info) if ($DEBUG); | ||
| 131 | print $table_info->{TABNAME} if ($DEBUG); | ||
| 132 | my $table = $schema->add_table( | ||
| 133 | name => $table_info->{TABNAME}, | ||
| 134 | type => 'TABLE', | ||
| 135 | ) || die $schema->error; | ||
| 136 | $table->options("TABLESPACE", $table_info->{TBSPACE}); | ||
| 137 | |||
| 138 | $colsth->execute($table_info->{TABNAME}); | ||
| 139 | my $cols = $colsth->fetchall_hashref("COLNAME"); | ||
| 140 | |||
| 141 | foreach my $c (values %{$cols}) { | ||
| 142 | print Dumper($c) if $DEBUG; | ||
| 143 | print $c->{COLNAME} if ($DEBUG); | ||
| 144 | my $f = $table->add_field( | ||
| 145 | name => $c->{COLNAME}, | ||
| 146 | default_value => $c->{DEFAULT}, | ||
| 147 | data_type => $c->{TYPENAME}, | ||
| 148 | order => $c->{COLNO}, | ||
| 149 | size => $c->{LENGTH}, | ||
| 150 | ) || die $table->error; | ||
| 151 | |||
| 152 | $f->is_nullable($c->{NULLS} eq 'Y'); | ||
| 153 | } | ||
| 154 | |||
| 155 | $consth->execute($table_info->{TABNAME}); | ||
| 156 | my $cons = $consth->fetchall_hashref("COLNAME"); | ||
| 157 | next if (!%$cons); | ||
| 158 | |||
| 159 | my @fields = map { $_->{COLNAME} } (values %{$cons}); | ||
| 160 | my $c = $cons->{ $fields[0] }; | ||
| 161 | |||
| 162 | print $c->{CONSTNAME} if ($DEBUG); | ||
| 163 | my $con = $table->add_constraint( | ||
| 164 | name => $c->{CONSTNAME}, | ||
| 165 | fields => \@fields, | ||
| 166 | type => $c->{TYPE} eq 'P' ? PRIMARY_KEY | ||
| 167 | : $c->{TYPE} eq 'F' ? FOREIGN_KEY | ||
| 168 | : UNIQUE | ||
| 169 | ) | ||
| 170 | || die $table->error; | ||
| 171 | |||
| 172 | $con->deferrable($c->{CHECKEXISTINGDATA} eq 'D'); | ||
| 173 | |||
| 174 | $indsth->execute($table_info->{TABNAME}); | ||
| 175 | my $inds = $indsth->fetchall_hashref("INDNAME"); | ||
| 176 | print Dumper($inds) if ($DEBUG); | ||
| 177 | next if (!%$inds); | ||
| 178 | |||
| 179 | foreach my $ind (keys %$inds) { | ||
| 180 | print $ind if ($DEBUG); | ||
| 181 | $indsth->execute($table_info->{TABNAME}); | ||
| 182 | my $indcols = $indsth->fetchall_hashref("COLNAME"); | ||
| 183 | next if ($inds->{$ind}{UNIQUERULE} eq 'P'); | ||
| 184 | |||
| 185 | print Dumper($indcols) if ($DEBUG); | ||
| 186 | |||
| 187 | my @fields = map { $_->{INDNAME} eq $ind ? $_->{COLNAME} : () } (values %{$indcols}); | ||
| 188 | |||
| 189 | my $index = $indcols->{ $fields[0] }; | ||
| 190 | |||
| 191 | my $inew = $table->add_index( | ||
| 192 | name => $index->{INDNAME}, | ||
| 193 | fields => \@fields, | ||
| 194 | type => $index->{UNIQUERULE} eq 'U' ? UNIQUE : NORMAL | ||
| 195 | ) || die $table->error; | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | $trigsth->execute($table_info->{TABNAME}); | ||
| 200 | my $trigs = $trigsth->fetchall_hashref("TRIGNAME"); | ||
| 201 | print Dumper($trigs); | ||
| 202 | next if (!%$trigs); | ||
| 203 | |||
| 204 | foreach my $t (values %$trigs) { | ||
| 205 | print $t->{TRIGNAME} if ($DEBUG); | ||
| 206 | my $trig = $schema->add_trigger( | ||
| 207 | name => $t->{TRIGNAME}, | ||
| 208 | |||
| 209 | # fields => \@fields, | ||
| 210 | perform_action_when => $t->{TRIGTIME} eq 'A' ? 'after' | ||
| 211 | : $t->{TRIGTIME} eq 'B' ? 'before' | ||
| 212 | : 'instead', | ||
| 213 | database_event => $t->{TRIGEVENT} eq 'I' ? 'insert' | ||
| 214 | : $t->{TRIGEVENT} eq 'D' ? 'delete' | ||
| 215 | : 'update', | ||
| 216 | action => $t->{TEXT}, | ||
| 217 | on_table => $t->{TABNAME} | ||
| 218 | ) | ||
| 219 | || die $schema->error; | ||
| 220 | |||
| 221 | # $trig->extra( reference => $def->{'reference'}, | ||
| 222 | # condition => $def->{'condition'}, | ||
| 223 | # granularity => $def->{'granularity'} ); | ||
| 224 | } | ||
| 225 | |||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | return 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; | ||
| 233 | |||
| 234 | # ------------------------------------------------------------------- | ||
| 235 | # Time is a waste of money. | ||
| 236 | # Oscar Wilde | ||
| 237 | # ------------------------------------------------------------------- | ||
| 238 | |||
| 239 | =pod | ||
| 240 | |||
| 241 | =head1 AUTHOR | ||
| 242 | |||
| 243 | Jess Robinson <lt>castaway@desert-island.m.isar.de<gt>. | ||
| 244 | |||
| 245 | =head1 SEE ALSO | ||
| 246 | |||
| 247 | SQL::Translator, DBD::DB2. | ||
| 248 | |||
| 249 | =cut |