DBIx/Class/Storage/DBI/Pg.pm

filter: Statementstmt 31.5% 30 / 95 Branchbran 7.8% 3 / 38 Conditioncond 5.2% 1 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 34.6% 9 / 26 Podpod 100% 5 / 5 totaltotal 24.6% 48 / 195 CC 28 SCAR 32.4
CC
28
Cov
22%
_dbh_get_autoinc_seq
90.0
bind_attribute_by_data_type
42.0
last_insert_id
20.0
CRAP 25.5
« DBIx/Class/CDBICompat/SQLTransformer.pm ↑ Summary DBIx/Class/CDBICompat/ColumnGroups.pm »
1 package DBIx::Class::Storage::DBI::Pg;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use base qw/DBIx::Class::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Scope::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Context::Preserve 'preserve_context';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 __PACKAGE__->sql_limit_dialect ('LimitOffset');
15 4 __PACKAGE__->sql_quote_char ('"');
16 4 __PACKAGE__->datetime_parser_type ('DateTime::Format::Pg');
17 4 __PACKAGE__->_use_multicolumn_in (1);
18  
19 0 ▶sub _determine_supports_insert_returning {
Subroutine _determine_supports_insert_returning: not called CC=2 SCAR=17.9
20 0 ▶ return shift->_server_info->{normalized_dbms_version} >= 8.002
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->_server_info->{'normalized_dbms_version'} >= 8.002 ? : 0 0 0
21 ? 1
22 : 0
23 ;
24 }
25  
26 0 ▶sub with_deferred_fk_checks {
Subroutine with_deferred_fk_checks: not called CC=1 SCAR=6.9
Pod documented
27 0 my ($self, $sub) = @_;
28  
29 0 my $txn_scope_guard = $self->txn_scope_guard;
30  
31 0 $self->_do_query('SET CONSTRAINTS ALL DEFERRED');
32  
33 0 ▶ my $sg = Scope::Guard->new(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 $self->_do_query('SET CONSTRAINTS ALL IMMEDIATE');
35 0 });
36  
37 0 ▶ return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 }
39  
40 # only used when INSERT ... RETURNING is disabled
41 0 ▶sub last_insert_id {
Subroutine last_insert_id: not called CC=4 SCAR=30.0
Pod documented
42 0 my ($self,$source,@cols) = @_;
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 0 ▶sub _sequence_fetch {
Subroutine _sequence_fetch: not called CC=3 SCAR=24.8
63 0 my ($self, $function, $sequence) = @_;
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 0 ▶sub _dbh_get_autoinc_seq {
Subroutine _dbh_get_autoinc_seq: not called CC=9 SCAR=45.0
75 0 my ($self, $dbh, $source, $col) = @_;
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 0 ▶sub _dbh_get_column_default {
Subroutine _dbh_get_column_default: not called CC=3 SCAR=24.8
113 0 my ( $self, $dbh, $schema, $table, $col ) = @_;
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 0 ▶sub sqlt_type {
Subroutine sqlt_type: not called CC=1 SCAR=6.9
Pod documented
162 0 return 'PostgreSQL';
163 }
164  
165 # Pg is not able to MAX(boolean_column), sigh...
166 #
167 # Generally it would make more sense to have this in the SQLMaker hierarchy,
168 # so that eventually { -max => ... } DTRT, but plans going forward are
169 # murky at best
170 # --ribasushi
171 #
172 2 ▶sub _minmax_operator_for_datatype {
Subroutine _minmax_operator_for_datatype: called CC=4 SCAR=15.8
173 #my ($self, $datatype, $want_max) = @_;
174  
175 2 ▶ return ($_[2] ? 'BOOL_OR' : 'BOOL_AND')
Branch: 2 branches
BranchTrueFalseTotal
if ($_[1] || "") =~ /\Abool(?:ean)?\z/i 2 0 2
$_[2] ? : 1 1 2
Condition50%1/2
$_[1] || ''
l!l
20
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
176 if ($_[1] || '') =~ /\Abool(?:ean)?\z/i;
177  
178 0 shift->next::method(@_);
179 }
180  
181 0 ▶sub bind_attribute_by_data_type {
Subroutine bind_attribute_by_data_type: not called CC=6 SCAR=37.4
Pod documented
182 0 my ($self,$data_type) = @_;
183  
184 0 ▶ if ($self->_is_binary_lob_type($data_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_binary_lob_type($data_type)) { } 0 0 0
185 # this is a hot-ish codepath, use an escape flag to minimize
186 # amount of function/method calls
187 # additionally version.pm is cock, and memleaks on multiple
188 # ->VERSION calls
189 # the flag is stored in the DBD namespace, so that Class::Unload
190 # will work (unlikely, but still)
191 0 ▶ unless ($DBD::Pg::__DBIC_DBD_VERSION_CHECK_DONE__) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBD::Pg::__DBIC_DBD_VERSION_CHECK_DONE__) 0 0 0
192 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
193 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
194 __PACKAGE__.': BYTEA columns are known to not work on Pg >= 9.0 with DBD::Pg < 2.17.2'
195 );
196 }
197 0 ▶ elsif (not try { DBD::Pg->VERSION('2.9.2'); 1 } ) { carp (
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 __PACKAGE__.': DBD::Pg 2.9.2 or greater is strongly recommended for BYTEA column support'
199 )}
200  
201 0 $DBD::Pg::__DBIC_DBD_VERSION_CHECK_DONE__ = 1;
202 }
203  
204 0 return { pg_type => DBD::Pg::PG_BYTEA() };
205 }
206 else {
207 0 return undef;
208 }
209 }
210  
211 0 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
212 0 my ($self, $name) = @_;
213  
214 0 $self->_dbh->pg_savepoint($name);
215 }
216  
217 0 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
218 0 my ($self, $name) = @_;
219  
220 0 $self->_dbh->pg_release($name);
221 }
222  
223 0 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
224 0 my ($self, $name) = @_;
225  
226 0 $self->_dbh->pg_rollback_to($name);
227 }
228  
229 0 ▶sub deployment_statements {
Subroutine deployment_statements: not called CC=4 SCAR=30.0
Pod documented
230 0 my $self = shift;;
231 0 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
232  
233 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
234  
235 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
236 ! exists $sqltargs->{producer_args}{postgres_version}
237 and
238 my $dver = $self->_server_info->{normalized_dbms_version}
239 ) {
240 0 $sqltargs->{producer_args}{postgres_version} = $dver;
241 }
242  
243 0 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
244 }
245  
246 4 1;
247  
248 __END__
« DBIx/Class/CDBICompat/SQLTransformer.pm ↑ Summary DBIx/Class/CDBICompat/ColumnGroups.pm »