DBIx/Class/Storage/DBI/Pg.pm

filter: Statementstmt 31.1% 29 / 93 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 33.3% 8 / 24 Podpod 100% 5 / 5 totaltotal 22.8% 42 / 184 CC 25 SCAR 33.2
CC
25
Cov
20%
_dbh_get_autoinc_seq
90.0
bind_attribute_by_data_type
42.0
last_insert_id
20.0
CRAP 27.8
« DBIx/Class/ResultSet.pm ↑ Summary DBIx/Class/CDBICompat/SQLTransformer.pm »
1 package DBIx::Class::Storage::DBI::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 ▶use base qw/DBIx::Class::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Scope::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Context::Preserve 'preserve_context';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 __PACKAGE__->sql_limit_dialect ('LimitOffset');
15 2 __PACKAGE__->sql_quote_char ('"');
16 2 __PACKAGE__->datetime_parser_type ('DateTime::Format::Pg');
17 2 __PACKAGE__->_use_multicolumn_in (1);
18  
19 sub _determine_supports_insert_returning {
20 0 ▶ return shift->_server_info->{normalized_dbms_version} >= 8.002
Subroutine _determine_supports_insert_returning: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->_server_info->{'normalized_dbms_version'} >= 8.002 ? : 0 0 0
21 ? 1
22 : 0
23 ;
24 }
25  
26 sub with_deferred_fk_checks {
27 0 ▶ my ($self, $sub) = @_;
Subroutine with_deferred_fk_checks: not called CC=1 SCAR=6.9
Pod documented
28  
29 0 my $txn_scope_guard = $self->txn_scope_guard;
30  
31 0 $self->_do_query('SET CONSTRAINTS ALL DEFERRED');
32  
33 my $sg = Scope::Guard->new(sub {
34 0 ▶ $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE');
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 });
36  
37 0 ▶ return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 }
39  
40 # only used when INSERT ... RETURNING is disabled
41 sub last_insert_id {
42 0 ▶ my ($self,$source,@cols) = @_;
Subroutine last_insert_id: not called CC=4 SCAR=30.0
Pod documented
43  
44 0 my @values;
45  
46 0 my $col_info = $source->columns_info(\@cols);
47  
48 0 for my $col (@cols) {
49 0 ▶ my $seq = ( $col_info->{$col}{sequence} ||= $self->dbh_do('_dbh_get_autoinc_seq', $source, $col) )
Branch: 1 branch
BranchTrueFalseTotal
my $seq = ($col_info->{$col}{"sequence"} ||= $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)) or $self->throw_exception(sprintf("Could not determine sequence for column '%s.%s', please consider adding a schema-qualified sequence to its column info", $source->name, $col)) 0 0 0
Condition0%0/3
$col_info->{$col}{"sequence"} ||= $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
l!l&&r!l&&!r
000
MC/DC0%0/2
$col_info->{$col}{"sequence"} ||= $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
$col_info->{$col}{"sequence"} $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
Truth table0%0/3
A ||= B
A= $col_info->{$col}{"sequence"}B= $self->dbh_do("_dbh_get_autoinc_seq", $source, $col)
ABresult
1X1
011
000
50 or $self->throw_exception( sprintf(
51 "Could not determine sequence for column '%s.%s', please consider adding a schema-qualified sequence to its column info",
52 $source->name,
53 $col,
54 ));
55  
56 0 push @values, $self->_dbh->last_insert_id(undef, undef, undef, undef, {sequence => $seq});
57 }
58  
59 0 return @values;
60 }
61  
62 sub _sequence_fetch {
63 0 ▶ my ($self, $function, $sequence) = @_;
Subroutine _sequence_fetch: not called CC=3 SCAR=24.8
64  
65 0 ▶ $self->throw_exception('No sequence to fetch') unless $sequence;
Branch: 1 branch
BranchTrueFalseTotal
unless $sequence 0 0 0
66  
67 0 ▶ my ($val) = $self->_get_dbh->selectrow_array(
Branch: 1 branch
BranchTrueFalseTotal
ref $sequence eq 'SCALAR' ? : 0 0 0
68 sprintf ("select %s('%s')", $function, (ref $sequence eq 'SCALAR') ? $$sequence : $sequence)
69 );
70  
71 0 return $val;
72 }
73  
74 sub _dbh_get_autoinc_seq {
75 0 ▶ my ($self, $dbh, $source, $col) = @_;
Subroutine _dbh_get_autoinc_seq: not called CC=9 SCAR=45.0
76  
77 0 my $schema;
78 0 my $table = $source->name;
79  
80 # deref table name if it needs it
81 0 ▶ $table = $$table
Branch: 1 branch
BranchTrueFalseTotal
if ref $table eq "SCALAR" 0 0 0
82 if ref $table eq 'SCALAR';
83  
84 # parse out schema name if present
85 0 ▶ if( $table =~ /^(.+)\.(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table =~ /^(.+)\.(.+)$/) 0 0 0
86 0 ( $schema, $table ) = ( $1, $2 );
87 }
88  
89 # get the column default using a Postgres-specific pg_catalog query
90 0 my $seq_expr = $self->_dbh_get_column_default( $dbh, $schema, $table, $col );
91  
92 # if no default value is set on the column, or if we can't parse the
93 # default value as a sequence, throw.
94 0 ▶ unless ( defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i) 0 0 0
Condition0%0/3
defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
defined $seq_expr $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
Truth table0%0/3
A and B
A= defined $seq_exprB= $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i
ABresult
0X0
100
111
95 0 ▶ $seq_expr = '' unless defined $seq_expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq_expr 0 0 0
96 0 ▶ $schema = "$schema." if defined $schema && length $schema;
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema and length $schema 0 0 0
Condition0%0/3
defined $schema and length $schema
!ll&&!rl&&r
000
MC/DC0%0/2
defined $schema and length $schema
defined $schema length $schema
Truth table0%0/3
A and B
A= defined $schemaB= length $schema
ABresult
0X0
100
111
97 0 ▶ $self->throw_exception( sprintf (
Branch: 1 branch
BranchTrueFalseTotal
$schema ? : 0 0 0
98 "No sequence found for '%s%s.%s', check the RDBMS table definition or explicitly set the ".
99 "'sequence' for this column in %s",
100 $schema ? "$schema." : '',
101 $table,
102 $col,
103 $source->source_name,
104 ));
105 }
106  
107 0 return $1; # exception thrown unless match is made above
108 }
109  
110 # custom method for fetching column default, since column_info has a
111 # bug with older versions of DBD::Pg
112 sub _dbh_get_column_default {
113 0 ▶ my ( $self, $dbh, $schema, $table, $col ) = @_;
Subroutine _dbh_get_column_default: not called CC=3 SCAR=24.8
114  
115 # Build and execute a query into the pg_catalog to find the Pg
116 # expression for the default value for this column in this table.
117 # If the table name is schema-qualified, query using that specific
118 # schema name.
119  
120 # Otherwise, find the table in the standard Postgres way, using the
121 # search path. This is done with the pg_catalog.pg_table_is_visible
122 # function, which returns true if a given table is 'visible',
123 # meaning the first table of that name to be found in the search
124 # path.
125  
126 # I *think* we can be assured that this query will always find the
127 # correct column according to standard Postgres semantics.
128 #
129 # -- rbuels
130  
131 0 my $sqlmaker = $self->sql_maker;
132 0 local $sqlmaker->{bindtype} = 'normal';
133  
134 0 ▶ my ($where, @bind) = $sqlmaker->where ({
Branch: 1 branch
BranchTrueFalseTotal
defined $schema && length $schema ? : 0 0 0
Condition0%0/3
defined $schema and length $schema
!ll&&!rl&&r
000
MC/DC0%0/2
defined $schema and length $schema
defined $schema length $schema
Truth table0%0/3
A and B
A= defined $schemaB= length $schema
ABresult
0X0
100
111
135 'a.attnum' => {'>', 0},
136 'c.relname' => $table,
137 'a.attname' => $col,
138 -not_bool => 'a.attisdropped',
139 (defined $schema && length $schema)
140 ? ( 'n.nspname' => $schema )
141 : ( -bool => \'pg_catalog.pg_table_is_visible(c.oid)' )
142 });
143  
144 0 my ($seq_expr) = $dbh->selectrow_array(<<EOS,undef,@bind);
145  
146 SELECT
147 (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid)
148 FROM pg_catalog.pg_attrdef d
149 WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
150 FROM pg_catalog.pg_class c
151 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
152 JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
153 $where
154  
155 EOS
156  
157 0 return $seq_expr;
158 }
159  
160  
161 sub sqlt_type {
162 0 ▶ return 'PostgreSQL';
Subroutine sqlt_type: not called CC=1 SCAR=6.9
Pod documented
163 }
164  
165 sub bind_attribute_by_data_type {
166 0 ▶ my ($self,$data_type) = @_;
Subroutine bind_attribute_by_data_type: not called CC=6 SCAR=37.4
Pod documented
167  
168 0 ▶ if ($self->_is_binary_lob_type($data_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_binary_lob_type($data_type)) { } 0 0 0
169 # this is a hot-ish codepath, use an escape flag to minimize
170 # amount of function/method calls
171 # additionally version.pm is cock, and memleaks on multiple
172 # ->VERSION calls
173 # the flag is stored in the DBD namespace, so that Class::Unload
174 # will work (unlikely, but still)
175 0 ▶ unless ($DBD::Pg::__DBIC_DBD_VERSION_CHECK_DONE__) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBD::Pg::__DBIC_DBD_VERSION_CHECK_DONE__) 0 0 0
176 0 ▶ if ($self->_server_info->{normalized_dbms_version} >= 9.0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_server_info->{'normalized_dbms_version'} >= 9.0) { } 0 0 0
elsif (not &try(sub { 'DBD::Pg'->VERSION('2.9.2'); 1; } )) { } 0 0 0
177 0 ▶ try { DBD::Pg->VERSION('2.17.2'); 1 } or carp (
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { "DBD::Pg"->VERSION("2.17.2"); 1; } ) or carp("DBIx::Class::Storage::DBI::Pg: BYTEA columns are known to not work on Pg >= 9.0 with DBD::Pg < 2.17.2") 0 0 0
178 __PACKAGE__.': BYTEA columns are known to not work on Pg >= 9.0 with DBD::Pg < 2.17.2'
179 );
180 }
181 0 ▶ elsif (not try { DBD::Pg->VERSION('2.9.2'); 1 } ) { carp (
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 __PACKAGE__.': DBD::Pg 2.9.2 or greater is strongly recommended for BYTEA column support'
183 )}
184  
185 0 $DBD::Pg::__DBIC_DBD_VERSION_CHECK_DONE__ = 1;
186 }
187  
188 0 return { pg_type => DBD::Pg::PG_BYTEA() };
189 }
190 else {
191 0 return undef;
192 }
193 }
194  
195 sub _exec_svp_begin {
196 0 ▶ my ($self, $name) = @_;
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
197  
198 0 $self->_dbh->pg_savepoint($name);
199 }
200  
201 sub _exec_svp_release {
202 0 ▶ my ($self, $name) = @_;
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
203  
204 0 $self->_dbh->pg_release($name);
205 }
206  
207 sub _exec_svp_rollback {
208 0 ▶ my ($self, $name) = @_;
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
209  
210 0 $self->_dbh->pg_rollback_to($name);
211 }
212  
213 sub deployment_statements {
214 0 ▶ my $self = shift;;
Subroutine deployment_statements: not called CC=4 SCAR=30.0
Pod documented
215 0 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
216  
217 0 ▶ $sqltargs ||= {};
Condition0%0/2
$sqltargs ||= {}
l!l
00
MC/DC0%0/1
$sqltargs ||= {}
$sqltargs
Truth table0%0/2
A
A= $sqltargs
Aresult
11
00
218  
219 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $sqltargs->{"producer_args"}{"postgres_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}) 0 0 0
Condition0%0/3
not exists $sqltargs->{"producer_args"}{"postgres_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $sqltargs->{"producer_args"}{"postgres_version"} and my $dver = $self->_server_info->{"normalized_dbms_version"}
not exists $sqltargs->{"producer_args"}{"postgres_version"} my $dver = $self->_server_info->{"normalized_dbms_version"}
Truth table0%0/3
not(A) and B
A= not exists $sqltargs->{"producer_args"}{"postgres_version"}B= my $dver = $self->_server_info->{"normalized_dbms_version"}
ABresult
0X0
100
111
220 ! exists $sqltargs->{producer_args}{postgres_version}
221 and
222 my $dver = $self->_server_info->{normalized_dbms_version}
223 ) {
224 0 $sqltargs->{producer_args}{postgres_version} = $dver;
225 }
226  
227 0 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
228 }
229  
230 2 1;
231  
232 __END__
« DBIx/Class/ResultSet.pm ↑ Summary DBIx/Class/CDBICompat/SQLTransformer.pm »