Commit | Line | Data |
996be9ee |
1 | package DBIx::Class::Schema::Loader::DBI::DB2; |
2 | |
3 | use strict; |
4 | use warnings; |
41968729 |
5 | use base qw/ |
6 | DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault |
7 | DBIx::Class::Schema::Loader::DBI |
8 | /; |
fa994d3c |
9 | use Carp::Clan qw/^DBIx::Class/; |
996be9ee |
10 | use Class::C3; |
11 | |
9990e58f |
12 | our $VERSION = '0.07000'; |
32f784fc |
13 | |
996be9ee |
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 | |
59cfa251 |
23 | __PACKAGE__->loader_options( db_schema => "MYSCHEMA" ); |
996be9ee |
24 | |
25 | 1; |
26 | |
27 | =head1 DESCRIPTION |
28 | |
29 | See L<DBIx::Class::Schema::Loader::Base>. |
30 | |
31 | =cut |
32 | |
7a930e63 |
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 | |
996be9ee |
42 | sub _table_uniq_info { |
43 | my ($self, $table) = @_; |
44 | |
45 | my @uniqs; |
46 | |
47 | my $dbh = $self->schema->storage->dbh; |
48 | |
5223f24a |
49 | my $sth = $self->{_cache}->{db2_uniq} ||= $dbh->prepare( |
50 | q{SELECT kcu.COLNAME, kcu.CONSTNAME, kcu.COLSEQ |
51 | FROM SYSCAT.TABCONST as tc |
ae32aaf6 |
52 | JOIN SYSCAT.KEYCOLUSE as kcu |
53 | ON tc.CONSTNAME = kcu.CONSTNAME AND tc.TABSCHEMA = kcu.TABSCHEMA |
5223f24a |
54 | WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U'} |
4421d6a3 |
55 | ) or die $DBI::errstr; |
5223f24a |
56 | |
a168c1c4 |
57 | $sth->execute($self->db_schema, uc $table) or die $DBI::errstr; |
996be9ee |
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 | } |
4421d6a3 |
69 | |
996be9ee |
70 | $sth->finish; |
71 | |
72 | return \@uniqs; |
73 | } |
74 | |
072d5aae |
75 | # DBD::DB2 doesn't follow the DBI API for ->tables |
76 | sub _tables_list { |
bfb43060 |
77 | my ($self, $opts) = @_; |
072d5aae |
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 | |
bfb43060 |
86 | return $self->_filter_tables(\@tables, $opts); |
a168c1c4 |
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 { |
8a64178e |
107 | my $self = shift; |
108 | my ($table) = @_; |
a168c1c4 |
109 | |
8a64178e |
110 | my $result = $self->next::method(uc $table); |
772cfe65 |
111 | |
772cfe65 |
112 | my $dbh = $self->schema->storage->dbh; |
8a64178e |
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 | |
c34195d7 |
129 | if (eval { lc ${ $info->{default_value} } }||'' eq 'current timestamp') { |
8a64178e |
130 | ${ $info->{default_value} } = 'CURRENT_TIMESTAMP'; |
131 | delete $info->{size}; |
132 | } |
772cfe65 |
133 | } |
134 | |
8a64178e |
135 | return $result; |
772cfe65 |
136 | } |
137 | |
996be9ee |
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 | |
be80bba7 |
143 | =head1 AUTHOR |
144 | |
9cc8e7e1 |
145 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
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 | |
996be9ee |
152 | =cut |
153 | |
154 | 1; |
8a64178e |
155 | # vim:et sts=4 sw=4 tw=0: |