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