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