| 1 | package Class::DBI::Pg; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | require Class::DBI; | |||||||||||||
| 5 | 1 | ▶ | use base 'Class::DBI'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | $VERSION = '0.08'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 0 | ▶ | sub set_up_table { | ||||||||||||
|
Subroutine
set_up_table: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||
| 11 | 0 | my ( $class, $table ) = @_; | |||||||||||||
| 12 | 0 | my $dbh = $class->db_Main; | |||||||||||||
| 13 | 0 | my $catalog = ""; | |||||||||||||
| 14 | 0 | ▶ | if ( $class->pg_version >= 7.3 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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; | |||||||||||||
| 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 | ▶ | if ($nextval_str) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 0 | ▶ | if ($class->pg_version() >= 8.1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | # hackish, but oh well... | ||||||||||||||
| 57 | 0 | ▶ | ($sequence) = | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | ▶ | next if $col->[0] =~ /^\.+pg\.dropped\.\d+\.+$/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | push @cols, $col->[0]; | |||||||||||||
| 73 | 0 | ▶ | next unless $prinum{ $col->[1] }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 0 | push @primary, $col->[0]; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | 0 | ▶ | if (!@primary) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | ▶ | $class->sequence($sequence) if $sequence; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | ▶ | sub pg_version { | ||||||||||||
|
Subroutine
pg_version: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 87 | 0 | 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 | my ($ver) = | ||||||||||||||
| 96 | $args{full_version} ? | ||||||||||||||
| 97 | 0 | ▶ | $ver_str =~ m/^PostgreSQL ([\d\.]{5})/ : | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | $ver_str =~ m/^PostgreSQL ([\d\.]{3})/; | ||||||||||||||
| 99 | 0 | return $ver; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | __END__ | ||||||||||||||