ported db2, pg schema, and test updates from non-schema loader to schema loader
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DB2.pm
CommitLineData
18fca96a 1package DBIx::Class::Schema::Loader::DB2;
a78e3fed 2
3use strict;
18fca96a 4use base 'DBIx::Class::Schema::Loader::Generic';
a78e3fed 5use Carp;
6
7=head1 NAME
8
18fca96a 9DBIx::Class::Schema::Loader::DB2 - DBIx::Class::Schema::Loader DB2 Implementation.
a78e3fed 10
11=head1 SYNOPSIS
12
18fca96a 13 use DBIx::Schema::Class::Loader;
a78e3fed 14
18fca96a 15 # $loader is a DBIx::Class::Schema::Loader::DB2
16 my $loader = DBIx::Class::Schema::Loader->new(
a78e3fed 17 dsn => "dbi:DB2:dbname",
18 user => "myuser",
19 password => "",
20 namespace => "Data",
21 schema => "MYSCHEMA",
22 dropschema => 0,
23 );
a78e3fed 24
25=head1 DESCRIPTION
26
18fca96a 27See L<DBIx::Class::Schema::Loader>.
a78e3fed 28
29=cut
30
31sub _db_classes {
af96f52e 32 return qw/DBIx::Class::PK::Auto::DB2/;
a78e3fed 33}
34
35sub _tables {
a4a19f3c 36 my $class = shift;
a78e3fed 37 my %args = @_;
af96f52e 38 my $db_schema = uc $class->loader_data->{_db_schema};
a4a19f3c 39 my $dbh = $class->storage->dbh;
a78e3fed 40
41 # this is split out to avoid version parsing errors...
42 my $is_dbd_db2_gte_114 = ( $DBD::DB2::VERSION >= 1.14 );
43 my @tables = $is_dbd_db2_gte_114 ?
44 $dbh->tables( { TABLE_SCHEM => '%', TABLE_TYPE => 'TABLE,VIEW' } )
45 : $dbh->tables;
a78e3fed 46 # People who use table or schema names that aren't identifiers deserve
47 # what they get. Still, FIXME?
48 s/\"//g for @tables;
49 @tables = grep {!/^SYSIBM\./ and !/^SYSCAT\./ and !/^SYSSTAT\./} @tables;
af6c2665 50 @tables = grep {/^$db_schema\./} @tables if($db_schema);
a78e3fed 51 return @tables;
52}
53
54sub _table_info {
a4a19f3c 55 my ( $class, $table ) = @_;
a78e3fed 56# $|=1;
57# print "_table_info($table)\n";
af6c2665 58 my ($db_schema, $tabname) = split /\./, $table, 2;
59 # print "DB_Schema: $db_schema, Table: $tabname\n";
a78e3fed 60
61 # FIXME: Horribly inefficient and just plain evil. (JMM)
a4a19f3c 62 my $dbh = $class->storage->dbh;
a78e3fed 63 $dbh->{RaiseError} = 1;
64
65 my $sth = $dbh->prepare(<<'SQL') or die;
66SELECT c.COLNAME
67FROM SYSCAT.COLUMNS as c
68WHERE c.TABSCHEMA = ? and c.TABNAME = ?
69SQL
70
af6c2665 71 $sth->execute($db_schema, $tabname) or die;
af96f52e 72 my @cols = map { lc } map { @$_ } @{$sth->fetchall_arrayref};
73
74 $sth->finish;
a78e3fed 75
76 $sth = $dbh->prepare(<<'SQL') or die;
77SELECT kcu.COLNAME
78FROM SYSCAT.TABCONST as tc
79JOIN SYSCAT.KEYCOLUSE as kcu ON tc.constname = kcu.constname
80WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'P'
81SQL
82
af6c2665 83 $sth->execute($db_schema, $tabname) or die;
a78e3fed 84
af96f52e 85 my @pri = map { lc } map { @$_ } @{$sth->fetchall_arrayref};
86
87 $sth->finish;
a78e3fed 88
89 return ( \@cols, \@pri );
90}
91
af96f52e 92# Find and setup relationships
93sub _relationships {
94 my $class = shift;
95
96 my $dbh = $class->storage->dbh;
97
98 my $sth = $dbh->prepare(<<'SQL') or die;
99SELECT SR.COLCOUNT, SR.REFTBNAME, SR.PKCOLNAMES, SR.FKCOLNAMES
100FROM SYSIBM.SYSRELS SR WHERE SR.TBNAME = ?
101SQL
102
103 foreach my $table ( $class->tables ) {
104 if ($sth->execute(uc $table)) {
105 while(my $res = $sth->fetchrow_arrayref()) {
106 my ($colcount, $other, $other_column, $column) =
107 map { $_=lc; s/^\s+//; s/\s+$//; $_; } @$res;
108 next if $colcount != 1; # XXX no multi-col FK support yet
109 eval { $class->_belongs_to_many( $table, $column, $other,
110 $other_column ) };
111 warn qq/\# belongs_to_many failed "$@"\n\n/
112 if $@ && $class->debug_loader;
113 }
114 }
115 }
116
117 $sth->finish;
118 $dbh->disconnect;
119}
120
a78e3fed 121=head1 SEE ALSO
122
18fca96a 123L<DBIx::Class::Schema::Loader>
a78e3fed 124
125=cut
126
1271;