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