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