finish preserve_case support
[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;
41968729 5use base qw/
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
8/;
fa994d3c 9use Carp::Clan qw/^DBIx::Class/;
996be9ee 10use Class::C3;
11
9990e58f 12our $VERSION = '0.07000';
32f784fc 13
996be9ee 14=head1 NAME
15
16DBIx::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
59cfa251 23 __PACKAGE__->loader_options( db_schema => "MYSCHEMA" );
996be9ee 24
25 1;
26
27=head1 DESCRIPTION
28
29See L<DBIx::Class::Schema::Loader::Base>.
30
31=cut
32
7a930e63 33sub _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)', {});
bc1cb85e 40
41 if (not defined $self->preserve_case) {
42 $self->preserve_case(0);
43 }
b511f36e 44 elsif ($self->preserve_case) {
45 $self->schema->storage->sql_maker->quote_char('"');
46 $self->schema->storage->sql_maker->name_sep('.');
47 }
7a930e63 48}
49
996be9ee 50sub _table_uniq_info {
51 my ($self, $table) = @_;
52
53 my @uniqs;
54
55 my $dbh = $self->schema->storage->dbh;
56
5223f24a 57 my $sth = $self->{_cache}->{db2_uniq} ||= $dbh->prepare(
58 q{SELECT kcu.COLNAME, kcu.CONSTNAME, kcu.COLSEQ
59 FROM SYSCAT.TABCONST as tc
ae32aaf6 60 JOIN SYSCAT.KEYCOLUSE as kcu
61 ON tc.CONSTNAME = kcu.CONSTNAME AND tc.TABSCHEMA = kcu.TABSCHEMA
5223f24a 62 WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U'}
4421d6a3 63 ) or die $DBI::errstr;
5223f24a 64
b511f36e 65 $sth->execute($self->db_schema, $self->_uc($table)) or die $DBI::errstr;
996be9ee 66
67 my %keydata;
68 while(my $row = $sth->fetchrow_arrayref) {
69 my ($col, $constname, $seq) = @$row;
b511f36e 70 push(@{$keydata{$constname}}, [ $seq, $self->_lc($col) ]);
996be9ee 71 }
72 foreach my $keyname (keys %keydata) {
73 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
74 @{$keydata{$keyname}};
75 push(@uniqs, [ $keyname => \@ordered_cols ]);
76 }
4421d6a3 77
996be9ee 78 $sth->finish;
79
80 return \@uniqs;
81}
82
072d5aae 83# DBD::DB2 doesn't follow the DBI API for ->tables
84sub _tables_list {
bfb43060 85 my ($self, $opts) = @_;
072d5aae 86
87 my $dbh = $self->schema->storage->dbh;
b511f36e 88 my @tables = map $self->_lc($_), $dbh->tables(
072d5aae 89 $self->db_schema ? { TABLE_SCHEM => $self->db_schema } : undef
90 );
91 s/\Q$self->{_quoter}\E//g for @tables;
92 s/^.*\Q$self->{_namesep}\E// for @tables;
93
bfb43060 94 return $self->_filter_tables(\@tables, $opts);
a168c1c4 95}
96
97sub _table_pk_info {
98 my ($self, $table) = @_;
b511f36e 99 return $self->next::method($self->_uc($table));
a168c1c4 100}
101
102sub _table_fk_info {
103 my ($self, $table) = @_;
104
b511f36e 105 my $rels = $self->next::method($self->_uc($table));
a168c1c4 106
107 foreach my $rel (@$rels) {
b511f36e 108 $rel->{remote_table} = $self->_lc($rel->{remote_table});
a168c1c4 109 }
110
111 return $rels;
112}
113
114sub _columns_info_for {
8a64178e 115 my $self = shift;
116 my ($table) = @_;
a168c1c4 117
b511f36e 118 my $result = $self->next::method($self->_uc($table));
772cfe65 119
772cfe65 120 my $dbh = $self->schema->storage->dbh;
8a64178e 121
122 while (my ($col, $info) = each %$result) {
123 # check for identities
124 my $sth = $dbh->prepare_cached(
125 q{
126 SELECT COUNT(*)
127 FROM syscat.columns
128 WHERE tabschema = ? AND tabname = ? AND colname = ?
129 AND identity = 'Y' AND generated != ''
130 },
131 {}, 1);
b511f36e 132 $sth->execute($self->db_schema, $self->_uc($table), $self->_uc($col));
8a64178e 133 if ($sth->fetchrow_array) {
134 $info->{is_auto_increment} = 1;
135 }
136
268cc246 137 if ((eval { lc ${ $info->{default_value} } }||'') eq 'current timestamp') {
6e566cc4 138 ${ $info->{default_value} } = 'current_timestamp';
8a64178e 139 delete $info->{size};
701cd3e3 140
141 my $orig_deflt = 'current timestamp';
142 $info->{original}{default_value} = \$orig_deflt;
8a64178e 143 }
772cfe65 144 }
145
8a64178e 146 return $result;
772cfe65 147}
148
996be9ee 149=head1 SEE ALSO
150
151L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
152L<DBIx::Class::Schema::Loader::DBI>
153
be80bba7 154=head1 AUTHOR
155
9cc8e7e1 156See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
be80bba7 157
158=head1 LICENSE
159
160This library is free software; you can redistribute it and/or modify it under
161the same terms as Perl itself.
162
996be9ee 163=cut
164
1651;
8a64178e 166# vim:et sts=4 sw=4 tw=0: