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