Commit | Line | Data |
18fca96a |
1 | package DBIx::Class::Schema::Loader::DB2; |
a78e3fed |
2 | |
3 | use strict; |
3980d69c |
4 | use warnings; |
2a4b8262 |
5 | use Class::C3; |
18fca96a |
6 | use base 'DBIx::Class::Schema::Loader::Generic'; |
a78e3fed |
7 | |
8 | =head1 NAME |
9 | |
18fca96a |
10 | DBIx::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( |
38348090 |
18 | dsn => "dbi:DB2:dbname", |
19 | user => "myuser", |
20 | password => "", |
21 | db_schema => "MYSCHEMA", |
22 | drop_schema => 1, |
a78e3fed |
23 | ); |
a78e3fed |
24 | |
25 | =head1 DESCRIPTION |
26 | |
18fca96a |
27 | See L<DBIx::Class::Schema::Loader>. |
a78e3fed |
28 | |
29 | =cut |
30 | |
3980d69c |
31 | sub _db_classes { |
af96f52e |
32 | return qw/DBIx::Class::PK::Auto::DB2/; |
a78e3fed |
33 | } |
34 | |
3980d69c |
35 | sub _tables { |
36 | my $self = shift; |
a78e3fed |
37 | my %args = @_; |
3980d69c |
38 | my $db_schema = uc $self->db_schema; |
39 | my $dbh = $self->schema->storage->dbh; |
b005807a |
40 | my $quoter = $dbh->get_info(29) || q{"}; |
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? |
b005807a |
49 | s/$quoter//g for @tables; |
a78e3fed |
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 | |
3980d69c |
55 | sub _table_info { |
56 | my ( $self, $table ) = @_; |
a78e3fed |
57 | # $|=1; |
3980d69c |
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) |
3980d69c |
63 | my $dbh = $self->schema->storage->dbh; |
a78e3fed |
64 | $dbh->{RaiseError} = 1; |
65 | |
66 | my $sth = $dbh->prepare(<<'SQL') or die; |
67 | SELECT c.COLNAME |
68 | FROM SYSCAT.COLUMNS as c |
69 | WHERE c.TABSCHEMA = ? and c.TABNAME = ? |
70 | SQL |
71 | |
af6c2665 |
72 | $sth->execute($db_schema, $tabname) or die; |
af96f52e |
73 | my @cols = map { lc } map { @$_ } @{$sth->fetchall_arrayref}; |
74 | |
66742793 |
75 | undef $sth; |
a78e3fed |
76 | |
77 | $sth = $dbh->prepare(<<'SQL') or die; |
78 | SELECT kcu.COLNAME |
79 | FROM SYSCAT.TABCONST as tc |
80 | JOIN SYSCAT.KEYCOLUSE as kcu ON tc.constname = kcu.constname |
81 | WHERE tc.TABSCHEMA = ? and tc.TABNAME = ? and tc.TYPE = 'P' |
82 | SQL |
83 | |
af6c2665 |
84 | $sth->execute($db_schema, $tabname) or die; |
a78e3fed |
85 | |
af96f52e |
86 | my @pri = map { lc } map { @$_ } @{$sth->fetchall_arrayref}; |
87 | |
a78e3fed |
88 | return ( \@cols, \@pri ); |
89 | } |
90 | |
af96f52e |
91 | # Find and setup relationships |
3980d69c |
92 | sub _load_relationships { |
93 | my $self = shift; |
af96f52e |
94 | |
3980d69c |
95 | my $dbh = $self->schema->storage->dbh; |
af96f52e |
96 | |
97 | my $sth = $dbh->prepare(<<'SQL') or die; |
98 | SELECT SR.COLCOUNT, SR.REFTBNAME, SR.PKCOLNAMES, SR.FKCOLNAMES |
99 | FROM SYSIBM.SYSRELS SR WHERE SR.TBNAME = ? |
100 | SQL |
101 | |
3980d69c |
102 | foreach my $table ( $self->tables ) { |
66742793 |
103 | next if ! $sth->execute(uc $table); |
104 | while(my $res = $sth->fetchrow_arrayref()) { |
105 | my ($colcount, $other, $other_column, $column) = |
106 | map { lc } @$res; |
107 | |
108 | my @self_cols = split(' ',$column); |
109 | my @other_cols = split(' ',$other_column); |
110 | if(@self_cols != $colcount || @other_cols != $colcount) { |
111 | die "Column count discrepancy while getting rel info"; |
af96f52e |
112 | } |
66742793 |
113 | |
114 | my %cond; |
115 | for(my $i = 0; $i < @self_cols; $i++) { |
116 | $cond{$other_cols[$i]} = $self_cols[$i]; |
117 | } |
118 | |
3980d69c |
119 | eval { $self->_make_cond_rel ($table, $other, \%cond); }; |
66742793 |
120 | warn qq/\# belongs_to_many failed "$@"\n\n/ |
3980d69c |
121 | if $@ && $self->debug; |
af96f52e |
122 | } |
123 | } |
124 | |
125 | $sth->finish; |
126 | $dbh->disconnect; |
127 | } |
128 | |
a78e3fed |
129 | =head1 SEE ALSO |
130 | |
18fca96a |
131 | L<DBIx::Class::Schema::Loader> |
a78e3fed |
132 | |
133 | =cut |
134 | |
135 | 1; |