1 package DBIx::Class::Schema::Loader::DBI::DB2;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
11 use DBIx::Class::Schema::Loader::Table ();
14 our $VERSION = '0.07047';
18 DBIx::Class::Schema::Loader::DBI::DB2 - DBIx::Class::Schema::Loader::DBI DB2 Implementation.
22 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
29 return ($self->next::method(@_), qw/
30 SYSCAT SYSIBM SYSIBMADM SYSPUBLIC SYSSTAT SYSTOOLS
37 $self->next::method(@_);
39 my $ns = $self->name_sep;
41 $self->db_schema([ $self->dbh->selectrow_array(<<"EOF", {}) ]) unless $self->db_schema;
42 SELECT CURRENT_SCHEMA FROM sysibm${ns}sysdummy1
45 if (not defined $self->preserve_case) {
46 $self->preserve_case(0);
48 elsif ($self->preserve_case) {
49 $self->schema->storage->sql_maker->quote_char('"');
50 $self->schema->storage->sql_maker->name_sep($ns);
54 sub _table_uniq_info {
55 my ($self, $table) = @_;
59 my $sth = $self->{_cache}->{db2_uniq} ||= $self->dbh->prepare(<<'EOF');
60 SELECT kcu.colname, kcu.constname, kcu.colseq
61 FROM syscat.tabconst as tc
62 JOIN syscat.keycoluse as kcu
63 ON tc.constname = kcu.constname
64 AND tc.tabschema = kcu.tabschema
65 AND tc.tabname = kcu.tabname
66 WHERE tc.tabschema = ? and tc.tabname = ? and tc.type = 'U'
69 $sth->execute($table->schema, $table->name);
72 while(my $row = $sth->fetchrow_arrayref) {
73 my ($col, $constname, $seq) = @$row;
74 push(@{$keydata{$constname}}, [ $seq, $self->_lc($col) ]);
76 foreach my $keyname (sort keys %keydata) {
77 my @ordered_cols = map { $_->[1] } sort { $a->[0] <=> $b->[0] }
78 @{$keydata{$keyname}};
79 push(@uniqs, [ $keyname => \@ordered_cols ]);
88 my ($self, $table) = @_;
90 my $sth = $self->{_cache}->{db2_fk} ||= $self->dbh->prepare(<<'EOF');
91 SELECT tc.constname, sr.reftabschema, sr.reftabname,
92 kcu.colname, rkcu.colname, kcu.colseq,
93 sr.deleterule, sr.updaterule
94 FROM syscat.tabconst tc
95 JOIN syscat.keycoluse kcu
96 ON tc.constname = kcu.constname
97 AND tc.tabschema = kcu.tabschema
98 AND tc.tabname = kcu.tabname
99 JOIN syscat.references sr
100 ON tc.constname = sr.constname
101 AND tc.tabschema = sr.tabschema
102 AND tc.tabname = sr.tabname
103 JOIN syscat.keycoluse rkcu
104 ON sr.refkeyname = rkcu.constname
105 AND sr.reftabschema = rkcu.tabschema
106 AND sr.reftabname = rkcu.tabname
107 AND kcu.colseq = rkcu.colseq
108 WHERE tc.tabschema = ?
112 $sth->execute($table->schema, $table->name);
123 COLS: while (my @row = $sth->fetchrow_array) {
124 my ($fk, $remote_schema, $remote_table, $local_col, $remote_col,
125 $colseq, $delete_rule, $update_rule) = @row;
127 if (not exists $rels{$fk}) {
128 if ($self->db_schema && $self->db_schema->[0] ne '%'
129 && (not any { $_ eq $remote_schema } @{ $self->db_schema })) {
134 $rels{$fk}{remote_table} = DBIx::Class::Schema::Loader::Table->new(
136 name => $remote_table,
137 schema => $remote_schema,
141 $rels{$fk}{local_columns}[$colseq-1] = $self->_lc($local_col);
142 $rels{$fk}{remote_columns}[$colseq-1] = $self->_lc($remote_col);
144 $rels{$fk}{attrs} ||= {
145 on_delete => $rules{$delete_rule},
146 on_update => $rules{$update_rule},
147 is_deferrable => 1, # DB2 has no deferrable constraints
151 return [ values %rels ];
155 # DBD::DB2 doesn't follow the DBI API for ->tables (pre 1.85), but since its
156 # backwards compatible we don't change it.
157 # DBD::DB2 1.85 and beyond default TABLE_NAME to '', previously defaulted to
158 # '%'. so we supply it.
160 my ($self, $schema) = @_;
162 return $self->dbh->tables($schema ? { TABLE_SCHEM => $schema, TABLE_NAME => '%' } : undef);
165 sub _dbh_table_info {
168 local $^W = 0; # shut up undef warning from DBD::DB2
170 $self->next::method(@_);
173 sub _columns_info_for {
177 my $result = $self->next::method(@_);
179 while (my ($col, $info) = each %$result) {
180 # check for identities
181 my $sth = $self->dbh->prepare_cached(
185 WHERE tabschema = ? AND tabname = ? AND colname = ?
186 AND identity = 'Y' AND generated != ''
189 $sth->execute($table->schema, $table->name, $self->_uc($col));
190 if ($sth->fetchrow_array) {
191 $info->{is_auto_increment} = 1;
194 my $data_type = $info->{data_type};
196 if ($data_type !~ /^(?:(?:var)?(?:char|graphic)|decimal)\z/i) {
197 delete $info->{size};
200 if ($data_type eq 'double') {
201 $info->{data_type} = 'double precision';
203 elsif ($data_type eq 'decimal') {
204 no warnings 'uninitialized';
206 $info->{data_type} = 'numeric';
208 my @size = @{ $info->{size} || [] };
210 if ($size[0] == 5 && $size[1] == 0) {
211 delete $info->{size};
214 elsif ($data_type =~ /^(?:((?:var)?char) \(\) for bit data|(long varchar) for bit data)\z/i) {
215 my $base_type = lc($1 || $2);
217 (my $original_type = $data_type) =~ s/[()]+ //;
219 $info->{original}{data_type} = $original_type;
221 if ($base_type eq 'long varchar') {
222 $info->{data_type} = 'blob';
225 if ($base_type eq 'char') {
226 $info->{data_type} = 'binary';
228 elsif ($base_type eq 'varchar') {
229 $info->{data_type} = 'varbinary';
232 my ($size) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($col));
235 WHERE tabschema = ? AND tabname = ? AND colname = ?
238 $info->{size} = $size if $size;
242 if ((eval { lc ${ $info->{default_value} } }||'') =~ /^current (date|time(?:stamp)?)\z/i) {
245 ${ $info->{default_value} } = 'current_timestamp';
247 my $orig_deflt = "current $type";
248 $info->{original}{default_value} = \$orig_deflt;
257 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
258 L<DBIx::Class::Schema::Loader::DBI>
262 See L<DBIx::Class::Schema::Loader/AUTHORS>.
266 This library is free software; you can redistribute it and/or modify it under
267 the same terms as Perl itself.
272 # vim:et sts=4 sw=4 tw=0: