initial import
[DBIx-DBSchema.git] / DBSchema / DBD / Pg.pm
1 package DBIx::DBSchema::DBD::Pg;
2
3 use strict;
4 use vars qw($VERSION);
5
6 $VERSION = '0.01';
7
8 =head1 NAME
9
10 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
11
12 =head1 SYNOPSIS
13
14 use DBI;
15 use DBIx::DBSchema;
16
17 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
18 $schema = new_native DBIx::DBSchema $dbh;
19
20 =head1 DESCRIPTION
21
22 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
23
24 =cut
25
26 sub columns {
27   my($proto, $dbh, $table) = @_;
28   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
29     SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull
30     FROM pg_class c, pg_attribute a, pg_type t
31     WHERE c.relname = '$table'
32       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
33 END
34   $sth->execute or die $sth->errstr;
35   map {
36     [
37       $_->{'attname'},
38       $_->{'typname'},
39       ! $_->{'attnotnull'},
40       $_->{'attlen'} == -1
41         ? $_->{'atttypmod'} - 4
42         : ''
43     ]
44   } @{ $sth->fetchall_arrayref({}) };
45 }
46
47 sub primary_key {
48   my($proto, $dbh, $table) = @_;
49   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
50     SELECT a.attname, a.attnum
51     FROM pg_class c, pg_attribute a, pg_type t
52     WHERE c.relname = '${table}_pkey'
53       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
54 END
55   $sth->execute or die $sth->errstr;
56   my $row = $sth->fetchrow_hashref or return '';
57   $row->{'attname'};
58 }
59
60 sub unique {
61   my($proto, $dbh, $table) = @_;
62   my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
63       grep { $proto->_is_unique($dbh, $_ ) }
64         $proto->_all_indices($dbh, $table)
65   };
66 }
67
68 sub index {
69   my($proto, $dbh, $table) = @_;
70   my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
71       grep { ! $proto->_is_unique($dbh, $_ ) }
72         $proto->_all_indices($dbh, $table)
73   };
74 }
75
76 sub _all_indices {
77   my($proto, $dbh, $table) = @_;
78   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
79     SELECT c2.relname
80     FROM pg_class c, pg_class c2, pg_index i
81     WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
82 END
83   $sth->execute or die $sth->errstr;
84   map { $_->{'relname'} }
85     grep { $_->{'relname'} !~ /_pkey$/ }
86       @{ $sth->fetchall_arrayref({}) };
87 }
88
89 sub _index_fields {
90   my($proto, $dbh, $index) = @_;
91   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
92     SELECT a.attname, a.attnum
93     FROM pg_class c, pg_attribute a, pg_type t
94     WHERE c.relname = '$index'
95       AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
96 END
97   $sth->execute or die $sth->errstr;
98   map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
99 }
100
101 sub _is_unique {
102   my($proto, $dbh, $index) = @_;
103   my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
104     SELECT i.indisunique
105     FROM pg_index i, pg_class c, pg_am a
106     WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
107 END
108   $sth->execute or die $sth->errstr;
109   my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
110   $row->{'indisunique'};
111 }
112
113 =head1 AUTHOR
114
115 Ivan Kohler <ivan-dbix-dbschema@420.am>
116
117 =head1 COPYRIGHT
118
119 Copyright (c) 2000 Ivan Kohler
120 Copyright (c) 2000 Mail Abuse Prevention System LLC
121 All rights reserved.
122 This program is free software; you can redistribute it and/or modify it under
123 the same terms as Perl itself.
124
125 =head1 BUGS
126
127 Yes.
128
129 =head1 SEE ALSO
130
131 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>
132
133 =cut 
134
135 1;
136