Schema::Loader converted to better inheritance model, no longer pollutes user schema...
[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;
3980d69c 4use warnings;
18fca96a 5use base 'DBIx::Class::Schema::Loader::Generic';
a78e3fed 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(
38348090 17 dsn => "dbi:DB2:dbname",
18 user => "myuser",
19 password => "",
20 db_schema => "MYSCHEMA",
21 drop_schema => 1,
a78e3fed 22 );
a78e3fed 23
24=head1 DESCRIPTION
25
18fca96a 26See L<DBIx::Class::Schema::Loader>.
a78e3fed 27
28=cut
29
3980d69c 30sub _db_classes {
af96f52e 31 return qw/DBIx::Class::PK::Auto::DB2/;
a78e3fed 32}
33
3980d69c 34sub _tables {
35 my $self = shift;
a78e3fed 36 my %args = @_;
3980d69c 37 my $db_schema = uc $self->db_schema;
38 my $dbh = $self->schema->storage->dbh;
b005807a 39 my $quoter = $dbh->get_info(29) || q{"};
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?
b005807a 48 s/$quoter//g for @tables;
a78e3fed 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
3980d69c 54sub _table_info {
55 my ( $self, $table ) = @_;
a78e3fed 56# $|=1;
3980d69c 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)
3980d69c 62 my $dbh = $self->schema->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
66742793 74 undef $sth;
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
a78e3fed 87 return ( \@cols, \@pri );
88}
89
af96f52e 90# Find and setup relationships
3980d69c 91sub _load_relationships {
92 my $self = shift;
af96f52e 93
3980d69c 94 my $dbh = $self->schema->storage->dbh;
af96f52e 95
96 my $sth = $dbh->prepare(<<'SQL') or die;
97SELECT SR.COLCOUNT, SR.REFTBNAME, SR.PKCOLNAMES, SR.FKCOLNAMES
98FROM SYSIBM.SYSRELS SR WHERE SR.TBNAME = ?
99SQL
100
3980d69c 101 foreach my $table ( $self->tables ) {
66742793 102 next if ! $sth->execute(uc $table);
103 while(my $res = $sth->fetchrow_arrayref()) {
104 my ($colcount, $other, $other_column, $column) =
105 map { lc } @$res;
106
107 my @self_cols = split(' ',$column);
108 my @other_cols = split(' ',$other_column);
109 if(@self_cols != $colcount || @other_cols != $colcount) {
110 die "Column count discrepancy while getting rel info";
af96f52e 111 }
66742793 112
113 my %cond;
114 for(my $i = 0; $i < @self_cols; $i++) {
115 $cond{$other_cols[$i]} = $self_cols[$i];
116 }
117
3980d69c 118 eval { $self->_make_cond_rel ($table, $other, \%cond); };
66742793 119 warn qq/\# belongs_to_many failed "$@"\n\n/
3980d69c 120 if $@ && $self->debug;
af96f52e 121 }
122 }
123
124 $sth->finish;
125 $dbh->disconnect;
126}
127
a78e3fed 128=head1 SEE ALSO
129
18fca96a 130L<DBIx::Class::Schema::Loader>
a78e3fed 131
132=cut
133
1341;