rewrite datetime defaults as CURRENT_TIMESTAMP to ease cross-deployment
[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.06001';
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
53         ON tc.CONSTNAME = kcu.CONSTNAME AND tc.TABSCHEMA = kcu.TABSCHEMA
54         WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U'}
55     ) or die $DBI::errstr;
56
57     $sth->execute($self->db_schema, uc $table) or die $DBI::errstr;
58
59     my %keydata;
60     while(my $row = $sth->fetchrow_arrayref) {
61         my ($col, $constname, $seq) = @$row;
62         push(@{$keydata{$constname}}, [ $seq, lc $col ]);
63     }
64     foreach my $keyname (keys %keydata) {
65         my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
66             @{$keydata{$keyname}};
67         push(@uniqs, [ $keyname => \@ordered_cols ]);
68     }
69
70     $sth->finish;
71     
72     return \@uniqs;
73 }
74
75 # DBD::DB2 doesn't follow the DBI API for ->tables
76 sub _tables_list { 
77     my ($self, $opts) = @_;
78     
79     my $dbh = $self->schema->storage->dbh;
80     my @tables = map { lc } $dbh->tables(
81         $self->db_schema ? { TABLE_SCHEM => $self->db_schema } : undef
82     );
83     s/\Q$self->{_quoter}\E//g for @tables;
84     s/^.*\Q$self->{_namesep}\E// for @tables;
85
86     return $self->_filter_tables(\@tables, $opts);
87 }
88
89 sub _table_pk_info {
90     my ($self, $table) = @_;
91     return $self->next::method(uc $table);
92 }
93
94 sub _table_fk_info {
95     my ($self, $table) = @_;
96
97     my $rels = $self->next::method(uc $table);
98
99     foreach my $rel (@$rels) {
100         $rel->{remote_table} = lc $rel->{remote_table};
101     }
102
103     return $rels;
104 }
105
106 sub _columns_info_for {
107     my $self = shift;
108     my ($table) = @_;
109
110     my $result = $self->next::method(uc $table);
111
112     my $dbh = $self->schema->storage->dbh;
113
114     while (my ($col, $info) = each %$result) {
115         # check for identities
116         my $sth = $dbh->prepare_cached(
117             q{
118                 SELECT COUNT(*)
119                 FROM syscat.columns
120                 WHERE tabschema = ? AND tabname = ? AND colname = ?
121                 AND identity = 'Y' AND generated != ''
122             },
123             {}, 1);
124         $sth->execute($self->db_schema, uc $table, uc $col);
125         if ($sth->fetchrow_array) {
126             $info->{is_auto_increment} = 1;
127         }
128
129         if (eval { lc ${ $info->{default_value} } }||'' eq 'CURRENT TIMESTAMP') {
130             ${ $info->{default_value} } = 'CURRENT_TIMESTAMP';
131             delete $info->{size};
132         }
133     }
134
135     return $result;
136 }
137
138 =head1 SEE ALSO
139
140 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
141 L<DBIx::Class::Schema::Loader::DBI>
142
143 =head1 AUTHOR
144
145 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
146
147 =head1 LICENSE
148
149 This library is free software; you can redistribute it and/or modify it under
150 the same terms as Perl itself.
151
152 =cut
153
154 1;
155 # vim:et sts=4 sw=4 tw=0: