Fix for SQLite PKs
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DB2.pm
CommitLineData
18fca96a 1package DBIx::Class::Schema::Loader::DB2;
a78e3fed 2
3use strict;
18fca96a 4use base 'DBIx::Class::Schema::Loader::Generic';
a78e3fed 5use DBI;
6use Carp;
7
8=head1 NAME
9
18fca96a 10DBIx::Class::Schema::Loader::DB2 - DBIx::Class::Schema::Loader DB2 Implementation.
a78e3fed 11
12=head1 SYNOPSIS
13
18fca96a 14 use DBIx::Schema::Class::Loader;
a78e3fed 15
18fca96a 16 # $loader is a DBIx::Class::Schema::Loader::DB2
17 my $loader = DBIx::Class::Schema::Loader->new(
a78e3fed 18 dsn => "dbi:DB2:dbname",
19 user => "myuser",
20 password => "",
21 namespace => "Data",
22 schema => "MYSCHEMA",
23 dropschema => 0,
24 );
a78e3fed 25
26=head1 DESCRIPTION
27
18fca96a 28See L<DBIx::Class::Schema::Loader>.
a78e3fed 29
30=cut
31
32sub _db_classes {
33 return ();
34}
35
36sub _tables {
a4a19f3c 37 my $class = shift;
a78e3fed 38 my %args = @_;
af6c2665 39 my $db_schema = uc ($args{db_schema} || '');
a4a19f3c 40 my $dbh = $class->storage->dbh;
a78e3fed 41
42 # this is split out to avoid version parsing errors...
43 my $is_dbd_db2_gte_114 = ( $DBD::DB2::VERSION >= 1.14 );
44 my @tables = $is_dbd_db2_gte_114 ?
45 $dbh->tables( { TABLE_SCHEM => '%', TABLE_TYPE => 'TABLE,VIEW' } )
46 : $dbh->tables;
a78e3fed 47 # People who use table or schema names that aren't identifiers deserve
48 # what they get. Still, FIXME?
49 s/\"//g for @tables;
50 @tables = grep {!/^SYSIBM\./ and !/^SYSCAT\./ and !/^SYSSTAT\./} @tables;
af6c2665 51 @tables = grep {/^$db_schema\./} @tables if($db_schema);
a78e3fed 52 return @tables;
53}
54
55sub _table_info {
a4a19f3c 56 my ( $class, $table ) = @_;
a78e3fed 57# $|=1;
58# print "_table_info($table)\n";
af6c2665 59 my ($db_schema, $tabname) = split /\./, $table, 2;
60 # print "DB_Schema: $db_schema, Table: $tabname\n";
a78e3fed 61
62 # FIXME: Horribly inefficient and just plain evil. (JMM)
a4a19f3c 63 my $dbh = $class->storage->dbh;
a78e3fed 64 $dbh->{RaiseError} = 1;
65
66 my $sth = $dbh->prepare(<<'SQL') or die;
67SELECT c.COLNAME
68FROM SYSCAT.COLUMNS as c
69WHERE c.TABSCHEMA = ? and c.TABNAME = ?
70SQL
71
af6c2665 72 $sth->execute($db_schema, $tabname) or die;
a78e3fed 73 my @cols = map { @$_ } @{$sth->fetchall_arrayref};
74
75 $sth = $dbh->prepare(<<'SQL') or die;
76SELECT kcu.COLNAME
77FROM SYSCAT.TABCONST as tc
78JOIN SYSCAT.KEYCOLUSE as kcu ON tc.constname = kcu.constname
79WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'P'
80SQL
81
af6c2665 82 $sth->execute($db_schema, $tabname) or die;
a78e3fed 83
84 my @pri = map { @$_ } @{$sth->fetchall_arrayref};
85
86 return ( \@cols, \@pri );
87}
88
89=head1 SEE ALSO
90
18fca96a 91L<DBIx::Class::Schema::Loader>
a78e3fed 92
93=cut
94
951;