use strict;
use base 'DBIx::Class::Schema::Loader::Generic';
-use DBI;
use Carp;
=head1 NAME
# $loader is a DBIx::Class::Schema::Loader::DB2
my $loader = DBIx::Class::Schema::Loader->new(
- dsn => "dbi:DB2:dbname",
- user => "myuser",
- password => "",
- namespace => "Data",
- schema => "MYSCHEMA",
- dropschema => 0,
+ dsn => "dbi:DB2:dbname",
+ user => "myuser",
+ password => "",
+ db_schema => "MYSCHEMA",
+ drop_schema => 1,
);
=head1 DESCRIPTION
=cut
sub _db_classes {
- return ();
+ return qw/DBIx::Class::PK::Auto::DB2/;
}
sub _tables {
my $class = shift;
my %args = @_;
- my $db_schema = uc ($args{db_schema} || '');
+ my $db_schema = uc $class->loader_data->{_db_schema};
my $dbh = $class->storage->dbh;
# this is split out to avoid version parsing errors...
SQL
$sth->execute($db_schema, $tabname) or die;
- my @cols = map { @$_ } @{$sth->fetchall_arrayref};
+ my @cols = map { lc } map { @$_ } @{$sth->fetchall_arrayref};
+
+ $sth->finish;
$sth = $dbh->prepare(<<'SQL') or die;
SELECT kcu.COLNAME
$sth->execute($db_schema, $tabname) or die;
- my @pri = map { @$_ } @{$sth->fetchall_arrayref};
+ my @pri = map { lc } map { @$_ } @{$sth->fetchall_arrayref};
+
+ $sth->finish;
return ( \@cols, \@pri );
}
+# Find and setup relationships
+sub _relationships {
+ my $class = shift;
+
+ my $dbh = $class->storage->dbh;
+
+ my $sth = $dbh->prepare(<<'SQL') or die;
+SELECT SR.COLCOUNT, SR.REFTBNAME, SR.PKCOLNAMES, SR.FKCOLNAMES
+FROM SYSIBM.SYSRELS SR WHERE SR.TBNAME = ?
+SQL
+
+ foreach my $table ( $class->tables ) {
+ if ($sth->execute(uc $table)) {
+ while(my $res = $sth->fetchrow_arrayref()) {
+ my ($colcount, $other, $other_column, $column) =
+ map { lc } @$res;
+
+ my @self_cols = split(' ',$column);
+ my @other_cols = split(' ',$other_column);
+ if(@self_cols != $colcount || @other_cols != $colcount) {
+ die "Column count discrepancy while getting rel info";
+ }
+
+ my %cond;
+ for(my $i = 0; $i < @self_cols; $i++) {
+ $cond{$other_cols[$i]} = $self_cols[$i];
+ }
+
+ eval { $class->_belongs_to_many ($table, $other, \%cond); };
+ warn qq/\# belongs_to_many failed "$@"\n\n/
+ if $@ && $class->debug_loader;
+ }
+ }
+ }
+
+ $sth->finish;
+ $dbh->disconnect;
+}
+
=head1 SEE ALSO
L<DBIx::Class::Schema::Loader>