creating -current branch for Schema::Loader
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / DB2.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::DB2;
2
3use strict;
4use warnings;
5use base 'DBIx::Class::Schema::Loader::DBI';
fa994d3c 6use Carp::Clan qw/^DBIx::Class/;
996be9ee 7use Class::C3;
8
9=head1 NAME
10
11DBIx::Class::Schema::Loader::DBI::DB2 - DBIx::Class::Schema::Loader::DBI DB2 Implementation.
12
13=head1 SYNOPSIS
14
15 package My::Schema;
16 use base qw/DBIx::Class::Schema::Loader/;
17
18 __PACKAGE__->loader_options(
19 relationships => 1,
20 db_schema => "MYSCHEMA",
21 );
22
23 1;
24
25=head1 DESCRIPTION
26
27See L<DBIx::Class::Schema::Loader::Base>.
28
29=cut
30
31sub _table_uniq_info {
32 my ($self, $table) = @_;
33
34 my @uniqs;
35
36 my $dbh = $self->schema->storage->dbh;
37
5223f24a 38 my $sth = $self->{_cache}->{db2_uniq} ||= $dbh->prepare(
39 q{SELECT kcu.COLNAME, kcu.CONSTNAME, kcu.COLSEQ
40 FROM SYSCAT.TABCONST as tc
41 JOIN SYSCAT.KEYCOLUSE as kcu ON tc.CONSTNAME = kcu.CONSTNAME
42 WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U'}
4421d6a3 43 ) or die $DBI::errstr;
5223f24a 44
45 $sth->execute($self->db_schema, $table) or die $DBI::errstr;
996be9ee 46
47 my %keydata;
48 while(my $row = $sth->fetchrow_arrayref) {
49 my ($col, $constname, $seq) = @$row;
50 push(@{$keydata{$constname}}, [ $seq, lc $col ]);
51 }
52 foreach my $keyname (keys %keydata) {
53 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
54 @{$keydata{$keyname}};
55 push(@uniqs, [ $keyname => \@ordered_cols ]);
56 }
4421d6a3 57
996be9ee 58 $sth->finish;
59
60 return \@uniqs;
61}
62
63=head1 SEE ALSO
64
65L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
66L<DBIx::Class::Schema::Loader::DBI>
67
68=cut
69
701;