X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FLoader%2FDBI%2FDB2.pm;h=886a45b7623d4a6b1aabb1c4e1992bd3a2ab563b;hb=bc1cb85e84e6a30c75763edd478378a68009c722;hp=5ec113fc89c3d7e90f51fd23403333e153379ad5;hpb=996be9ee6e82ec9928f801ecdc69c9e07d64339c;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm b/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm index 5ec113f..886a45b 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/DB2.pm @@ -2,9 +2,15 @@ package DBIx::Class::Schema::Loader::DBI::DB2; use strict; use warnings; -use base 'DBIx::Class::Schema::Loader::DBI'; +use base qw/ + DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault + DBIx::Class::Schema::Loader::DBI +/; +use Carp::Clan qw/^DBIx::Class/; use Class::C3; +our $VERSION = '0.07000'; + =head1 NAME DBIx::Class::Schema::Loader::DBI::DB2 - DBIx::Class::Schema::Loader::DBI DB2 Implementation. @@ -14,10 +20,7 @@ DBIx::Class::Schema::Loader::DBI::DB2 - DBIx::Class::Schema::Loader::DBI DB2 Imp package My::Schema; use base qw/DBIx::Class::Schema::Loader/; - __PACKAGE__->loader_options( - relationships => 1, - db_schema => "MYSCHEMA", - ); + __PACKAGE__->loader_options( db_schema => "MYSCHEMA" ); 1; @@ -27,6 +30,19 @@ See L. =cut +sub _setup { + my $self = shift; + + $self->next::method(@_); + + my $dbh = $self->schema->storage->dbh; + $self->{db_schema} ||= $dbh->selectrow_array('VALUES(CURRENT_USER)', {}); + + if (not defined $self->preserve_case) { + $self->preserve_case(0); + } +} + sub _table_uniq_info { my ($self, $table) = @_; @@ -34,14 +50,15 @@ sub _table_uniq_info { my $dbh = $self->schema->storage->dbh; - my $sth = $dbh->prepare(<<'SQL') or die; -SELECT kcu.COLNAME, kcu.CONSTNAME, kcu.COLSEQ -FROM SYSCAT.TABCONST as tc -JOIN SYSCAT.KEYCOLUSE as kcu ON tc.CONSTNAME = kcu.CONSTNAME -WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U' -SQL + my $sth = $self->{_cache}->{db2_uniq} ||= $dbh->prepare( + q{SELECT kcu.COLNAME, kcu.CONSTNAME, kcu.COLSEQ + FROM SYSCAT.TABCONST as tc + JOIN SYSCAT.KEYCOLUSE as kcu + ON tc.CONSTNAME = kcu.CONSTNAME AND tc.TABSCHEMA = kcu.TABSCHEMA + WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'U'} + ) or die $DBI::errstr; - $sth->execute($self->db_schema, $table) or die; + $sth->execute($self->db_schema, uc $table) or die $DBI::errstr; my %keydata; while(my $row = $sth->fetchrow_arrayref) { @@ -53,16 +70,90 @@ SQL @{$keydata{$keyname}}; push(@uniqs, [ $keyname => \@ordered_cols ]); } + $sth->finish; return \@uniqs; } +# DBD::DB2 doesn't follow the DBI API for ->tables +sub _tables_list { + my ($self, $opts) = @_; + + my $dbh = $self->schema->storage->dbh; + my @tables = map { lc } $dbh->tables( + $self->db_schema ? { TABLE_SCHEM => $self->db_schema } : undef + ); + s/\Q$self->{_quoter}\E//g for @tables; + s/^.*\Q$self->{_namesep}\E// for @tables; + + return $self->_filter_tables(\@tables, $opts); +} + +sub _table_pk_info { + my ($self, $table) = @_; + return $self->next::method(uc $table); +} + +sub _table_fk_info { + my ($self, $table) = @_; + + my $rels = $self->next::method(uc $table); + + foreach my $rel (@$rels) { + $rel->{remote_table} = lc $rel->{remote_table}; + } + + return $rels; +} + +sub _columns_info_for { + my $self = shift; + my ($table) = @_; + + my $result = $self->next::method(uc $table); + + my $dbh = $self->schema->storage->dbh; + + while (my ($col, $info) = each %$result) { + # check for identities + my $sth = $dbh->prepare_cached( + q{ + SELECT COUNT(*) + FROM syscat.columns + WHERE tabschema = ? AND tabname = ? AND colname = ? + AND identity = 'Y' AND generated != '' + }, + {}, 1); + $sth->execute($self->db_schema, uc $table, uc $col); + if ($sth->fetchrow_array) { + $info->{is_auto_increment} = 1; + } + + if (eval { lc ${ $info->{default_value} } }||'' eq 'current timestamp') { + ${ $info->{default_value} } = 'CURRENT_TIMESTAMP'; + delete $info->{size}; + } + } + + return $result; +} + =head1 SEE ALSO L, L, L +=head1 AUTHOR + +See L and L. + +=head1 LICENSE + +This library is free software; you can redistribute it and/or modify it under +the same terms as Perl itself. + =cut 1; +# vim:et sts=4 sw=4 tw=0: