File Coverage

blib/lib/Class/DBI/Pg.pm
Criterion Covered Total %
statement 9 54 16.6
branch 0 20 0.0
condition n/a
subroutine 3 5 60.0
pod 2 2 100.0
total 14 81 17.2


line stmt bran cond sub pod time code
1             package Class::DBI::Pg;
2              
3 1     1   49579 use strict;
  1         3  
  1         59  
4             require Class::DBI;
5 1     1   6 use base 'Class::DBI';
  1         2  
  1         4440  
6 1     1   256578 use vars qw($VERSION);
  1         9  
  1         858  
7              
8             $VERSION = '0.08';
9              
10             sub set_up_table {
11 0     0 1   my ( $class, $table ) = @_;
12 0           my $dbh = $class->db_Main;
13 0           my $catalog = "";
14 0 0         if ( $class->pg_version >= 7.3 ) {
15 0           $catalog = 'pg_catalog.';
16             }
17              
18             # find primary key
19 0           my $sth = $dbh->prepare(<<"SQL");
20             SELECT indkey FROM ${catalog}pg_index
21             WHERE indisprimary=true AND indrelid=(
22             SELECT oid FROM ${catalog}pg_class
23             WHERE relname = ?)
24             SQL
25 0           $sth->execute($table);
26 0           my %prinum = map { $_ => 1 } split ' ', $sth->fetchrow_array;
  0            
27 0           $sth->finish;
28              
29             # find all columns
30 0           $sth = $dbh->prepare(<<"SQL");
31             SELECT a.attname, a.attnum
32             FROM ${catalog}pg_class c, ${catalog}pg_attribute a
33             WHERE c.relname = ?
34             AND a.attnum > 0 AND a.attrelid = c.oid
35             ORDER BY a.attnum
36             SQL
37 0           $sth->execute($table);
38 0           my $columns = $sth->fetchall_arrayref;
39 0           $sth->finish;
40              
41             # find SERIAL type.
42             # nextval('"table_id_seq"'::text)
43 0           $sth = $dbh->prepare(<<"SQL");
44             SELECT adsrc FROM ${catalog}pg_attrdef
45             WHERE
46             adrelid=(SELECT oid FROM ${catalog}pg_class WHERE relname=?)
47             SQL
48 0           $sth->execute($table);
49 0           my ($nextval_str) = $sth->fetchrow_array;
50 0           $sth->finish;
51              
52             # the text representation for nextval() changed between 7.x and 8.x
53 0           my $sequence;
54 0 0         if ($nextval_str) {
55 0 0         if ($class->pg_version() >= 8.1) {
56             # hackish, but oh well...
57 0 0         ($sequence) =
    0          
58             $nextval_str =~ m!^nextval\('"?([^"']+)"?'::regclass\)!i ?
59             $1 :
60             $nextval_str =~ m!^nextval\(\("?([^"']+)"?'::text\)?::regclass\)!i ?
61             $1 :
62             undef;
63             } else {
64 0           ($sequence) = $nextval_str =~ m!^nextval\('"?([^"']+)"?'::text\)!;
65             }
66             }
67              
68 0           my ( @cols, @primary );
69 0           foreach my $col (@$columns) {
70             # skip dropped column.
71 0 0         next if $col->[0] =~ /^\.+pg\.dropped\.\d+\.+$/;
72 0           push @cols, $col->[0];
73 0 0         next unless $prinum{ $col->[1] };
74 0           push @primary, $col->[0];
75             }
76 0 0         if (!@primary) {
77 0           require Carp;
78 0           Carp::croak("$table has no primary key");
79             }
80 0           $class->table($table);
81 0           $class->columns( Primary => @primary );
82 0           $class->columns( All => @cols );
83 0 0         $class->sequence($sequence) if $sequence;
84             }
85              
86             sub pg_version {
87 0     0 1   my $class = shift;
88 0           my %args = @_;
89              
90 0           my $dbh = $class->db_Main;
91 0           my $sth = $dbh->prepare("SELECT version()");
92 0           $sth->execute;
93 0           my ($ver_str) = $sth->fetchrow_array;
94 0           $sth->finish;
95 0 0         my ($ver) =
96             $args{full_version} ?
97             $ver_str =~ m/^PostgreSQL ([\d\.]{5})/ :
98             $ver_str =~ m/^PostgreSQL ([\d\.]{3})/;
99 0           return $ver;
100             }
101              
102             __END__