depend on DBD::Pg 1.32 or 1.41+ (1.40 was bunk)
[DBIx-DBSchema.git] / DBSchema / DBD / Pg.pm
1 package DBIx::DBSchema::DBD::Pg;
2
3 use strict;
4 use vars qw($VERSION @ISA %typemap);
5 use DBD::Pg 1.32;
6 die "DBD::Pg version 1.32 or 1.41 (or later) required--".
7     "this is only version $DBD::Pg::VERSION\n"
8   if $DBD::Pg::VERSION != 1.32 && $DBD::Pg::VERSION < 1.41;
9 use DBIx::DBSchema::DBD;
10
11 $VERSION = '0.09';
12 @ISA = qw(DBIx::DBSchema::DBD);
13
14 %typemap = (
15   'BLOB' => 'BYTEA',
16   'LONG VARBINARY' => 'BYTEA',
17 );
18
19 =head1 NAME
20
21 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
22
23 =head1 SYNOPSIS
24
25 use DBI;
26 use DBIx::DBSchema;
27
28 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
29 $schema = new_native DBIx::DBSchema $dbh;
30
31 =head1 DESCRIPTION
32
33 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
34
35 =cut
36
37 sub columns {
38   my($proto, $dbh, $table) = @_;
39   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
40     SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
41            a.atthasdef, a.attnum
42     FROM pg_class c, pg_attribute a, pg_type t
43     WHERE c.relname = '$table'
44       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
45     ORDER BY a.attnum
46 END
47   $sth->execute or die $sth->errstr;
48
49   map {
50
51     my $default = '';
52     if ( $_->{atthasdef} ) {
53       my $attnum = $_->{attnum};
54       my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
55         SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
56         WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
57 END
58       $d_sth->execute or die $d_sth->errstr;
59
60       $default = $d_sth->fetchrow_arrayref->[0];
61     };
62
63     my $len = '';
64     if ( $_->{attlen} == -1 && $_->{atttypmod} != -1 
65          && $_->{typname} ne 'text'                  ) {
66       $len = $_->{atttypmod} - 4;
67       if ( $_->{typname} eq 'numeric' ) {
68         $len = ($len >> 16). ','. ($len & 0xffff);
69       }
70     }
71
72     my $type = $_->{'typname'};
73     $type = 'char' if $type eq 'bpchar';
74
75     [
76       $_->{'attname'},
77       $type,
78       ! $_->{'attnotnull'},
79       $len,
80       $default,
81       ''  #local
82     ];
83
84   } @{ $sth->fetchall_arrayref({}) };
85 }
86
87 sub primary_key {
88   my($proto, $dbh, $table) = @_;
89   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
90     SELECT a.attname, a.attnum
91     FROM pg_class c, pg_attribute a, pg_type t
92     WHERE c.relname = '${table}_pkey'
93       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
94 END
95   $sth->execute or die $sth->errstr;
96   my $row = $sth->fetchrow_hashref or return '';
97   $row->{'attname'};
98 }
99
100 sub unique {
101   my($proto, $dbh, $table) = @_;
102   my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
103       grep { $proto->_is_unique($dbh, $_ ) }
104         $proto->_all_indices($dbh, $table)
105   };
106 }
107
108 sub index {
109   my($proto, $dbh, $table) = @_;
110   my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
111       grep { ! $proto->_is_unique($dbh, $_ ) }
112         $proto->_all_indices($dbh, $table)
113   };
114 }
115
116 sub _all_indices {
117   my($proto, $dbh, $table) = @_;
118   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
119     SELECT c2.relname
120     FROM pg_class c, pg_class c2, pg_index i
121     WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
122 END
123   $sth->execute or die $sth->errstr;
124   map { $_->{'relname'} }
125     grep { $_->{'relname'} !~ /_pkey$/ }
126       @{ $sth->fetchall_arrayref({}) };
127 }
128
129 sub _index_fields {
130   my($proto, $dbh, $index) = @_;
131   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
132     SELECT a.attname, a.attnum
133     FROM pg_class c, pg_attribute a, pg_type t
134     WHERE c.relname = '$index'
135       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
136 END
137   $sth->execute or die $sth->errstr;
138   map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
139 }
140
141 sub _is_unique {
142   my($proto, $dbh, $index) = @_;
143   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
144     SELECT i.indisunique
145     FROM pg_index i, pg_class c, pg_am a
146     WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
147 END
148   $sth->execute or die $sth->errstr;
149   my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
150   $row->{'indisunique'};
151 }
152
153 =head1 AUTHOR
154
155 Ivan Kohler <ivan-dbix-dbschema@420.am>
156
157 =head1 COPYRIGHT
158
159 Copyright (c) 2000 Ivan Kohler
160 Copyright (c) 2000 Mail Abuse Prevention System LLC
161 All rights reserved.
162 This program is free software; you can redistribute it and/or modify it under
163 the same terms as Perl itself.
164
165 =head1 BUGS
166
167 Yes.
168
169 columns doesn't return column default information.
170
171 =head1 SEE ALSO
172
173 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>
174
175 =cut 
176
177 1;
178