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)', {}); |
bc1cb85e |
40 | |
41 | if (not defined $self->preserve_case) { |
42 | $self->preserve_case(0); |
43 | } |
7a930e63 |
44 | } |
45 | |
996be9ee |
46 | sub _table_uniq_info { |
47 | my ($self, $table) = @_; |
48 | |
49 | my @uniqs; |
50 | |
51 | my $dbh = $self->schema->storage->dbh; |
52 | |
5223f24a |
53 | my $sth = $self->{_cache}->{db2_uniq} ||= $dbh->prepare( |
54 | q{SELECT kcu.COLNAME, kcu.CONSTNAME, kcu.COLSEQ |
55 | FROM SYSCAT.TABCONST as tc |
ae32aaf6 |
56 | JOIN SYSCAT.KEYCOLUSE as kcu |
57 | ON tc.CONSTNAME = kcu.CONSTNAME AND tc.TABSCHEMA = kcu.TABSCHEMA |
5223f24a |
58 | WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U'} |
4421d6a3 |
59 | ) or die $DBI::errstr; |
5223f24a |
60 | |
a168c1c4 |
61 | $sth->execute($self->db_schema, uc $table) or die $DBI::errstr; |
996be9ee |
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 | } |
4421d6a3 |
73 | |
996be9ee |
74 | $sth->finish; |
75 | |
76 | return \@uniqs; |
77 | } |
78 | |
072d5aae |
79 | # DBD::DB2 doesn't follow the DBI API for ->tables |
80 | sub _tables_list { |
bfb43060 |
81 | my ($self, $opts) = @_; |
072d5aae |
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 | |
bfb43060 |
90 | return $self->_filter_tables(\@tables, $opts); |
a168c1c4 |
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 { |
8a64178e |
111 | my $self = shift; |
112 | my ($table) = @_; |
a168c1c4 |
113 | |
8a64178e |
114 | my $result = $self->next::method(uc $table); |
772cfe65 |
115 | |
772cfe65 |
116 | my $dbh = $self->schema->storage->dbh; |
8a64178e |
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 | |
c34195d7 |
133 | if (eval { lc ${ $info->{default_value} } }||'' eq 'current timestamp') { |
6e566cc4 |
134 | ${ $info->{default_value} } = 'current_timestamp'; |
8a64178e |
135 | delete $info->{size}; |
136 | } |
772cfe65 |
137 | } |
138 | |
8a64178e |
139 | return $result; |
772cfe65 |
140 | } |
141 | |
996be9ee |
142 | =head1 SEE ALSO |
143 | |
144 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
145 | L<DBIx::Class::Schema::Loader::DBI> |
146 | |
be80bba7 |
147 | =head1 AUTHOR |
148 | |
9cc8e7e1 |
149 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
150 | |
151 | =head1 LICENSE |
152 | |
153 | This library is free software; you can redistribute it and/or modify it under |
154 | the same terms as Perl itself. |
155 | |
996be9ee |
156 | =cut |
157 | |
158 | 1; |
8a64178e |
159 | # vim:et sts=4 sw=4 tw=0: |