| 1 | package Class::DBI::ViewLoader::Pg; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '0.05'; | |||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Class::DBI::ViewLoader::Pg - Class::DBI::Viewloader implementation for Postgresql. | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | use Class::DBI::ViewLoader; | ||||||||||
| 15 | |||||||||||
| 16 | $loader = Class::DBI::ViewLoader dsn => 'dbi:Pg:dbname=mydb'; | ||||||||||
| 17 | |||||||||||
| 18 | # load views from mydb | ||||||||||
| 19 | @classes = $loader->load_views | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | This is the postgresql driver for L<Class::DBI::ViewLoader>, | ||||||||||
| 24 | |||||||||||
| 25 | =head1 METHODS | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | use Class::DBI::Pg; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 2 | ▶ | use base qw( Class::DBI::ViewLoader ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | =head2 base_class | ||||||||||
| 34 | |||||||||||
| 35 | Returns 'Class::DBI::Pg'. This class will be used as the main base class for all | ||||||||||
| 36 | classes generated using this driver. | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | sub base_class { 'Class::DBI::Pg' }; | ||||||||
|
Subroutine
base_class: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 41 | |||||||||||
| 42 | =head2 get_views | ||||||||||
| 43 | |||||||||||
| 44 | @views = $obj->get_views | ||||||||||
| 45 | |||||||||||
| 46 | Returns a list of the names of the views in the current database. | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | sub get_views { | ||||||||
|
Subroutine
get_views: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 51 | 0 | my $self = shift; | |||||||||
| 52 | 0 | my $dbh = $self->_get_dbi_handle; | |||||||||
| 53 | |||||||||||
| 54 | 0 | return $dbh->tables( | |||||||||
| 55 | undef, # catalog | ||||||||||
| 56 | "public", # schema | ||||||||||
| 57 | "", # name | ||||||||||
| 58 | "view", # type | ||||||||||
| 59 | { noprefix => 1, pg_noprefix => 1 } | ||||||||||
| 60 | ); | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | =head2 get_view_cols | ||||||||||
| 64 | |||||||||||
| 65 | @cols = $obj->get_view_cols($view) | ||||||||||
| 66 | |||||||||||
| 67 | Returns the columns contained in the given view. | ||||||||||
| 68 | |||||||||||
| 69 | =cut | ||||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | sub get_view_cols { | ||||||||
|
Subroutine
get_view_cols: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 72 | 0 | my($self, $view) = @_; | |||||||||
| 73 | 0 | my $sth = $self->_get_cols_sth; | |||||||||
| 74 | |||||||||||
| 75 | 0 | $sth->execute($view); | |||||||||
| 76 | |||||||||||
| 77 | 0 | my @columns = map {$_->[0]} @{$sth->fetchall_arrayref}; | |||||||||
| 78 | |||||||||||
| 79 | 0 | $sth->finish; | |||||||||
| 80 | |||||||||||
| 81 | 0 | return grep { !/^\.+pg\.dropped\.\d+\.+$/ } @columns; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | # SQL to get columns cribbed from Class::DBI::Pg->set_up_table | ||||||||||
| 85 | 2 | my $col_sql = <<END_SQL; | |||||||||
| 86 | SELECT a.attname | ||||||||||
| 87 | FROM pg_catalog.pg_class c | ||||||||||
| 88 | JOIN pg_catalog.pg_attribute a on a.attrelid = c.oid | ||||||||||
| 89 | WHERE c.relname = ? | ||||||||||
| 90 | AND a.attnum > 0 | ||||||||||
| 91 | ORDER BY a.attnum | ||||||||||
| 92 | END_SQL | ||||||||||
| 93 | |||||||||||
| 94 | # cache the statement handle | ||||||||||
| 95 | 0 | ▶ | sub _get_cols_sth { | ||||||||
|
Subroutine
_get_cols_sth: not called CC=2 SCAR=17.9
| |||||||||||
| 96 | 0 | my $self = shift; | |||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | if (defined $self->{__col_sth}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | return $self->{__col_sth}; | |||||||||
| 100 | } | ||||||||||
| 101 | else { | ||||||||||
| 102 | 0 | my $dbh = $self->_get_dbi_handle; | |||||||||
| 103 | |||||||||||
| 104 | 0 | return $self->{__col_sth} = $dbh->prepare($col_sql); | |||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | # make sure the cache is cleared when the dbi handle is cleaned up | ||||||||||
| 109 | 0 | ▶ | sub _clear_dbi_handle { | ||||||||
|
Subroutine
_clear_dbi_handle: not called CC=1 SCAR=6.9
| |||||||||||
| 110 | 0 | my $self = shift; | |||||||||
| 111 | |||||||||||
| 112 | # Should be no need to explicitly finish this.. | ||||||||||
| 113 | 0 | delete $self->{__col_sth}; | |||||||||
| 114 | |||||||||||
| 115 | 0 | $self->SUPER::_clear_dbi_handle(@_); | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 2 | 1; | |||||||||
| 119 | |||||||||||
| 120 | __END__ | ||||||||||