Pg reverse-engineering fix: now sets default
[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.05';
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 && $_->{typname} ne 'text' ) {
61       $len = $_->{atttypmod} - 4;
62       if ( $_->{typname} eq 'numeric' ) {
63         $len = ($len >> 16). ','. ($len & 0xffff);
64       }
65     }
66
67     my $type = $_->{'typname'};
68     $type = 'char' if $type eq 'bpchar';
69
70     [
71       $_->{'attname'},
72       $type,
73       ! $_->{'attnotnull'},
74       $len,
75       $default,
76       ''  #local
77     ];
78
79   } @{ $sth->fetchall_arrayref({}) };
80 }
81
82 sub primary_key {
83   my($proto, $dbh, $table) = @_;
84   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
85     SELECT a.attname, a.attnum
86     FROM pg_class c, pg_attribute a, pg_type t
87     WHERE c.relname = '${table}_pkey'
88       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
89 END
90   $sth->execute or die $sth->errstr;
91   my $row = $sth->fetchrow_hashref or return '';
92   $row->{'attname'};
93 }
94
95 sub unique {
96   my($proto, $dbh, $table) = @_;
97   my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
98       grep { $proto->_is_unique($dbh, $_ ) }
99         $proto->_all_indices($dbh, $table)
100   };
101 }
102
103 sub index {
104   my($proto, $dbh, $table) = @_;
105   my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
106       grep { ! $proto->_is_unique($dbh, $_ ) }
107         $proto->_all_indices($dbh, $table)
108   };
109 }
110
111 sub _all_indices {
112   my($proto, $dbh, $table) = @_;
113   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
114     SELECT c2.relname
115     FROM pg_class c, pg_class c2, pg_index i
116     WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
117 END
118   $sth->execute or die $sth->errstr;
119   map { $_->{'relname'} }
120     grep { $_->{'relname'} !~ /_pkey$/ }
121       @{ $sth->fetchall_arrayref({}) };
122 }
123
124 sub _index_fields {
125   my($proto, $dbh, $index) = @_;
126   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
127     SELECT a.attname, a.attnum
128     FROM pg_class c, pg_attribute a, pg_type t
129     WHERE c.relname = '$index'
130       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
131 END
132   $sth->execute or die $sth->errstr;
133   map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
134 }
135
136 sub _is_unique {
137   my($proto, $dbh, $index) = @_;
138   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
139     SELECT i.indisunique
140     FROM pg_index i, pg_class c, pg_am a
141     WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
142 END
143   $sth->execute or die $sth->errstr;
144   my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
145   $row->{'indisunique'};
146 }
147
148 =head1 AUTHOR
149
150 Ivan Kohler <ivan-dbix-dbschema@420.am>
151
152 =head1 COPYRIGHT
153
154 Copyright (c) 2000 Ivan Kohler
155 Copyright (c) 2000 Mail Abuse Prevention System LLC
156 All rights reserved.
157 This program is free software; you can redistribute it and/or modify it under
158 the same terms as Perl itself.
159
160 =head1 BUGS
161
162 Yes.
163
164 columns doesn't return column default information.
165
166 =head1 SEE ALSO
167
168 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>
169
170 =cut 
171
172 1;
173