01fbd1c4b565b6866a0c939691746e2c49dcde37
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / DB2.pm
1 package DBIx::Class::Schema::Loader::DBI::DB2;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Carp::Clan qw/^DBIx::Class/;
7 use Class::C3;
8
9 our $VERSION = '0.04999_04';
10
11 =head1 NAME
12
13 DBIx::Class::Schema::Loader::DBI::DB2 - DBIx::Class::Schema::Loader::DBI DB2 Implementation.
14
15 =head1 SYNOPSIS
16
17   package My::Schema;
18   use base qw/DBIx::Class::Schema::Loader/;
19
20   __PACKAGE__->loader_options( db_schema => "MYSCHEMA" );
21
22   1;
23
24 =head1 DESCRIPTION
25
26 See L<DBIx::Class::Schema::Loader::Base>.
27
28 =cut
29
30 sub _table_uniq_info {
31     my ($self, $table) = @_;
32
33     my @uniqs;
34
35     my $dbh = $self->schema->storage->dbh;
36
37     my $sth = $self->{_cache}->{db2_uniq} ||= $dbh->prepare(
38         q{SELECT kcu.COLNAME, kcu.CONSTNAME, kcu.COLSEQ
39         FROM SYSCAT.TABCONST as tc
40         JOIN SYSCAT.KEYCOLUSE as kcu ON tc.CONSTNAME = kcu.CONSTNAME
41         WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U'}
42     ) or die $DBI::errstr;
43
44     $sth->execute($self->db_schema, uc $table) or die $DBI::errstr;
45
46     my %keydata;
47     while(my $row = $sth->fetchrow_arrayref) {
48         my ($col, $constname, $seq) = @$row;
49         push(@{$keydata{$constname}}, [ $seq, lc $col ]);
50     }
51     foreach my $keyname (keys %keydata) {
52         my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
53             @{$keydata{$keyname}};
54         push(@uniqs, [ $keyname => \@ordered_cols ]);
55     }
56
57     $sth->finish;
58     
59     return \@uniqs;
60 }
61
62 # DBD::DB2 doesn't follow the DBI API for ->tables
63 sub _tables_list { 
64     my $self = shift;
65     
66     my $dbh = $self->schema->storage->dbh;
67     my @tables = map { lc } $dbh->tables(
68         $self->db_schema ? { TABLE_SCHEM => $self->db_schema } : undef
69     );
70     s/\Q$self->{_quoter}\E//g for @tables;
71     s/^.*\Q$self->{_namesep}\E// for @tables;
72
73     return @tables;
74 }
75
76 sub _table_pk_info {
77     my ($self, $table) = @_;
78     return $self->next::method(uc $table);
79 }
80
81 sub _table_fk_info {
82     my ($self, $table) = @_;
83
84     my $rels = $self->next::method(uc $table);
85
86     foreach my $rel (@$rels) {
87         $rel->{remote_table} = lc $rel->{remote_table};
88     }
89
90     return $rels;
91 }
92
93 sub _columns_info_for {
94     my ($self, $table) = @_;
95     return $self->next::method(uc $table);
96 }
97
98 sub _extra_column_info {
99     my ($self, $info) = @_;
100     my %extra_info;
101
102     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
103
104     my $dbh = $self->schema->storage->dbh;
105     my $sth = $dbh->prepare_cached(
106         q{
107             SELECT COUNT(*)
108             FROM syscat.columns
109             WHERE tabschema = ? AND tabname = ? AND colname = ?
110             AND identity = 'Y' AND generated != ''
111         },
112         {}, 1);
113     $sth->execute($self->db_schema, $table, $column);
114     if ($sth->fetchrow_array) {
115         $extra_info{is_auto_increment} = 1;
116     }
117
118     return \%extra_info;
119 }
120
121 =head1 SEE ALSO
122
123 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
124 L<DBIx::Class::Schema::Loader::DBI>
125
126 =cut
127
128 1;