1 package DBIx::Class::Schema::Loader::DB2;
4 use base 'DBIx::Class::Schema::Loader::Generic';
9 DBIx::Class::Schema::Loader::DB2 - DBIx::Class::Schema::Loader DB2 Implementation.
13 use DBIx::Schema::Class::Loader;
15 # $loader is a DBIx::Class::Schema::Loader::DB2
16 my $loader = DBIx::Class::Schema::Loader->new(
17 dsn => "dbi:DB2:dbname",
20 db_schema => "MYSCHEMA",
26 See L<DBIx::Class::Schema::Loader>.
30 sub _loader_db_classes {
31 return qw/DBIx::Class::PK::Auto::DB2/;
37 my $db_schema = uc $class->_loader_data->{db_schema};
38 my $dbh = $class->storage->dbh;
39 my $quoter = $dbh->get_info(29) || q{"};
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' } )
46 # People who use table or schema names that aren't identifiers deserve
47 # what they get. Still, FIXME?
48 s/$quoter//g for @tables;
49 @tables = grep {!/^SYSIBM\./ and !/^SYSCAT\./ and !/^SYSSTAT\./} @tables;
50 @tables = grep {/^$db_schema\./} @tables if($db_schema);
54 sub _loader_table_info {
55 my ( $class, $table ) = @_;
57 # print "_loader_table_info($table)\n";
58 my ($db_schema, $tabname) = split /\./, $table, 2;
59 # print "DB_Schema: $db_schema, Table: $tabname\n";
61 # FIXME: Horribly inefficient and just plain evil. (JMM)
62 my $dbh = $class->storage->dbh;
63 $dbh->{RaiseError} = 1;
65 my $sth = $dbh->prepare(<<'SQL') or die;
67 FROM SYSCAT.COLUMNS as c
68 WHERE c.TABSCHEMA = ? and c.TABNAME = ?
71 $sth->execute($db_schema, $tabname) or die;
72 my @cols = map { lc } map { @$_ } @{$sth->fetchall_arrayref};
76 $sth = $dbh->prepare(<<'SQL') or die;
78 FROM SYSCAT.TABCONST as tc
79 JOIN SYSCAT.KEYCOLUSE as kcu ON tc.constname = kcu.constname
80 WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'P'
83 $sth->execute($db_schema, $tabname) or die;
85 my @pri = map { lc } map { @$_ } @{$sth->fetchall_arrayref};
87 return ( \@cols, \@pri );
90 # Find and setup relationships
91 sub _loader_relationships {
94 my $dbh = $class->storage->dbh;
96 my $sth = $dbh->prepare(<<'SQL') or die;
97 SELECT SR.COLCOUNT, SR.REFTBNAME, SR.PKCOLNAMES, SR.FKCOLNAMES
98 FROM SYSIBM.SYSRELS SR WHERE SR.TBNAME = ?
101 foreach my $table ( $class->tables ) {
102 next if ! $sth->execute(uc $table);
103 while(my $res = $sth->fetchrow_arrayref()) {
104 my ($colcount, $other, $other_column, $column) =
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";
114 for(my $i = 0; $i < @self_cols; $i++) {
115 $cond{$other_cols[$i]} = $self_cols[$i];
118 eval { $class->_loader_make_relations ($table, $other, \%cond); };
119 warn qq/\# belongs_to_many failed "$@"\n\n/
120 if $@ && $class->_loader_debug;
130 L<DBIx::Class::Schema::Loader>