blib/lib/DBIO/PostgreSQL/SQLMaker.pm

filter: Statementstmt 99.1% 121 / 122 Branchbran 95.5% 65 / 68 Conditioncond 74.1% 46 / 62 MC/DCmcdc 56.2% 18 / 32 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 88.7% 268 / 302 CC 58 SCAR 32.1
CC
58
Cov
92%
_insert_on_conflict
41.9
_lock_select
6.0
insert
5.0
CRAP 24.9
« blib/lib/DBIO/PostgreSQL/Diff.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/Column.pm »
1 package DBIO::PostgreSQL::SQLMaker;
2 # ABSTRACT: PostgreSQL-specific SQL generation for DBIO
3  
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 19 our $VERSION = '0.900007';
8  
9 19 ▶use base 'DBIO::SQLMaker';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use DBIO::Util qw(is_literal_value);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 19 my $JSON = JSON::MaybeXS->new( utf8 => 1, canonical => 1 );
15  
16 19 my %JSONB_OP = (
17 '@>' => '_where_op_jsonb_contains',
18 '<@' => '_where_op_jsonb_contains',
19 '@?' => '_where_op_jsonb_path',
20 '@@' => '_where_op_jsonb_path',
21 '?' => '_where_op_jsonb_exists',
22 '?|' => '_where_op_jsonb_exists',
23 '?&' => '_where_op_jsonb_exists',
24 );
25  
26 34 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
27 34 my $class = shift;
28 0 ▶ my %opts = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 34 34
29  
30 # Register the JSONB operators with the new-style expand_op mechanism (the
31 # base DBIO::SQLMaker disables the old special_ops system). Each handler
32 # keeps its original ($self, $col, $op, $val) -> ($sql, @bind) signature; we
33 # wrap its output in a -literal node.
34 34 for my $op (keys %JSONB_OP) {
35 238 my $meth = $JSONB_OP{$op};
36 33 ▶ $opts{expand_op}{$op} ||= sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 33 my ($self, $op, $v, $k) = @_;
38 33 return +{ -literal => [ $self->$meth($k, $op, $v) ] };
39 238 ▶ };
Condition33%1/3
$opts{"expand_op"}{$op} ||= sub { my($self, $op, $v, $k) = @_; return {"-literal", [$self->$meth($k, $op, $v)]}; }
l!l&&r!l&&!r
02380
MC/DC0%0/2
$opts{"expand_op"}{$op} ||= sub { my($self, $op, $v, $k) = @_; return {"-literal", [$self->$meth($k, $op, $v)]}; }
$opts{"expand_op"}{$op} sub { my($self, $op, $v, $k) = @_; return {"-literal", [$self->$meth($k, $op, $v)]}; }
Truth table33%1/3
A ||= B
A= $opts{"expand_op"}{$op}B= sub { my($self, $op, $v, $k) = @_; return {"-literal", [$self->$meth($k, $op, $v)]}; }
ABresult
1X1
011
000
40 }
41  
42 34 $class->next::method( \%opts );
43 }
44  
45  
46 11 ▶sub _where_op_jsonb_contains {
Subroutine _where_op_jsonb_contains: called CC=4 SCAR=13.9
47 11 my ( $self, $col, $op, $val ) = @_;
48  
49 11 my $quoted = $self->_quote($col);
50  
51 11 ▶ if ( ref $val eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "SCALAR") 1 10 11
52 1 return ( sprintf( '%s %s %s', $quoted, $op, $$val ), () );
53 }
54  
55 10 ▶ my $json =
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'HASH' || ref $val eq 'ARRAY' ? : 9 1 10
Condition100%3/3
ref $val eq "HASH" or ref $val eq "ARRAY"
l!l&&r!l&&!r
721
MC/DC100%2/2
ref $val eq "HASH" or ref $val eq "ARRAY"
ref $val eq "HASH" ref $val eq "ARRAY"
Truth table100%3/3
A or B
A= ref $val eq "HASH"B= ref $val eq "ARRAY"
ABresult
1X1
011
000
56 ( ref $val eq 'HASH' || ref $val eq 'ARRAY' )
57 ? $JSON->encode($val)
58 : $val; # already a JSON string
59  
60 10 return ( sprintf( '%s %s ?::jsonb', $quoted, $op ), $json );
61 }
62  
63  
64 11 ▶sub _where_op_jsonb_path {
Subroutine _where_op_jsonb_path: called CC=2 SCAR=6.9
65 11 my ( $self, $col, $op, $val ) = @_;
66  
67 11 my $quoted = $self->_quote($col);
68 # the ? of the operator is no placeholder: written \?
69 11 ▶ my $written = $op eq '@?' ? '@\?' : $op;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '@?' ? : 8 3 11
70 11 return ( sprintf( '%s %s ?::jsonpath', $quoted, $written ), $val );
71 }
72  
73  
74 11 ▶sub _where_op_jsonb_exists {
Subroutine _where_op_jsonb_exists: called CC=3 SCAR=11.0
75 11 my ( $self, $col, $op, $val ) = @_;
76  
77 11 my $quoted = $self->_quote($col);
78  
79 # the ? of the operator is no placeholder: written \?
80 11 ▶ if ( $op eq '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "?") 3 8 11
81 3 return ( sprintf( '%s \? ?', $quoted ), $val );
82 }
83  
84 8 ▶ my @keys = ref $val eq 'ARRAY' ? @$val : ($val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 6 2 8
85 8 my $arr = 'ARRAY[' . join( ', ', ('?') x scalar @keys ) . ']';
86  
87 8 return ( sprintf( '%s \%s %s', $quoted, $op, $arr ), @keys );
88 }
89  
90  
91 65 ▶sub insert {
Subroutine insert: called CC=5 SCAR=16.1
Pod documented
92 65 my ($self, $table, $data, $options) = @_;
93  
94 # ADR 0036: the conflict structure MUST be consumed here. If it survives
95 # into the generic DBIO::SQLMaker::ClassicExtensions::insert that path
96 # throws -- deliberately, so "storage declares supports_upsert but its SQL
97 # generator does not render the clause" fails loud instead of silently
98 # emitting a plain INSERT.
99 my $on_conflict = ref $options eq 'HASH'
100 ? delete $options->{on_conflict}
101 65 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 53 12 65
102  
103 65 ▶ return $self->next::method($table, $data, $options) unless $on_conflict;
Branch: 1 branch
BranchTrueFalseTotal
unless $on_conflict 13 52 65
104  
105 # PostgreSQL grammar is INSERT ... ON CONFLICT ... RETURNING, but the
106 # generic insert appends the RETURNING tail last. So hold the key back over
107 # the call to the base, then append the two tails in engine order.
108 #
109 # It is put BACK afterwards: DBIO::Storage::DBI::insert reads
110 # $sqla_opts->{returning} again after execution to know which columns the
111 # statement handed back. Dropping it would silently cost the upsert its
112 # identity (ADR 0036 section 4).
113 52 my $returning = delete $options->{returning};
114  
115 52 my ($sql, @bind) = $self->next::method($table, $data, $options);
116  
117 52 my ($conflict_sql, @conflict_bind) = $self->_insert_on_conflict($on_conflict, $table);
118 38 $sql .= $conflict_sql;
119 38 push @bind, @conflict_bind;
120  
121 38 ▶ if (defined $returning) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $returning) 7 31 38
122 7 $options->{returning} = $returning;
123 7 my ($returning_sql, @returning_bind) = $self->_insert_returning($options);
124 7 $sql .= $returning_sql;
125 7 push @bind, @returning_bind;
126 }
127  
128 38 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 37 1 38
129 }
130  
131  
132 52 ▶sub _insert_on_conflict {
Subroutine _insert_on_conflict: called CC=40 SCAR=37.3
133 52 my ($self, $on_conflict, $table) = @_;
134  
135 52 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_conflict eq "HASH" 0 52 52
136 'on_conflict must be a hashref of { target, action, set } (ADR 0036)'
137 ) unless ref $on_conflict eq 'HASH';
138  
139 52 ▶ my $action = defined $on_conflict->{action} ? $on_conflict->{action} : 'update';
Branch: 1 branch
BranchTrueFalseTotal
defined $on_conflict->{'action'} ? : 52 0 52
140 52 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $action eq "update" or $action eq "nothing" 1 51 52
Condition100%3/3
$action eq "update" or $action eq "nothing"
l!l&&r!l&&!r
38131
MC/DC100%2/2
$action eq "update" or $action eq "nothing"
$action eq "update" $action eq "nothing"
Truth table100%3/3
A or B
A= $action eq "update"B= $action eq "nothing"
ABresult
1X1
011
000
141 "on_conflict action must be 'update' or 'nothing', not '$action'"
142 ) unless $action eq 'update' or $action eq 'nothing';
143  
144 # The conditional conflict branch rides on the update branch. Core refuses
145 # the combination up front, so a structure that still carries both is
146 # broken -- and dropping the condition here would produce a valid
147 # statement, which is precisely the silent failure mode ADR 0036 section 3
148 # gave this part of the structure its own capability for.
149 #
150 # exists, not defined: core sets the key only when it has a condition, so a
151 # key that arrives here in ANY state -- undef included -- comes from a
152 # structure that is already broken, and answering it with a valid DO NOTHING
153 # is the one outcome with no symptom of its own.
154 $self->throw_exception(
155 "on_conflict where has no meaning with action 'nothing': there is no "
156 . 'update branch to condition'
157 51 ▶ ) if $action eq 'nothing' and exists $on_conflict->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "nothing" and exists $on_conflict->{"where"} 2 49 51
Condition100%3/3
$action eq "nothing" and exists $on_conflict->{"where"}
!ll&&!rl&&r
38112
MC/DC100%2/2
$action eq "nothing" and exists $on_conflict->{"where"}
$action eq "nothing" exists $on_conflict->{"where"}
Truth table100%3/3
A and B
A= $action eq "nothing"B= exists $on_conflict->{"where"}
ABresult
0X0
100
111
158  
159 49 my $target = $on_conflict->{target};
160 49 my $target_sql;
161  
162 49 ▶ if (ref $target eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $target eq 'ARRAY') { } 34 15 49
elsif (ref $target eq 'HASH') { } 10 5 15
elsif (defined $target and not length ref $target and $target eq 'any') { } 4 1 5
163 34 ▶ $self->throw_exception('on_conflict target is an empty column list')
Branch: 1 branch
BranchTrueFalseTotal
unless @$target 1 33 34
164 unless @$target;
165 33 $target_sql = '(' . join(', ', map { $self->_quote($_) } @$target) . ')';
166 }
167 elsif (ref $target eq 'HASH') {
168 10 my $name = $target->{constraint};
169 10 my $columns = $target->{columns};
170  
171 $self->throw_exception(
172 'on_conflict target hashref must be { constraint => $name }, '
173 . '{ columns => \@columns } or both (ADR 0036)'
174 10 ▶ ) unless ! grep { $_ ne 'constraint' and $_ ne 'columns' } keys %$target
Branch: 1 branch
BranchTrueFalseTotal
unless not grep({$_ ne "constraint" and $_ ne "columns";} keys %$target) and defined $name || defined $columns and not defined $name && length ref $name and !defined($columns) || ref $columns eq "ARRAY" 2 8 10
Condition80%17/21
not grep({$_ ne "constraint" and $_ ne "columns";} keys %$target) and defined $name || defined $columns and not defined $name && length ref $name and !defined($columns) || ref $columns eq "ARRAY"
!ll&&!rl&&r
208
not grep({$_ ne "constraint" and $_ ne "columns";} keys %$target) and defined $name || defined $columns and not defined $name && length ref $name
!ll&&!rl&&r
208
not grep({$_ ne "constraint" and $_ ne "columns";} keys %$target) and defined $name || defined $columns
!ll&&!rl&&r
118
$_ ne 'constraint' && $_ ne 'columns'
!ll&&!rl&&r
871
defined $name or defined $columns
l!l&&r!l&&!r
711
defined $name && length ref $name
!ll&&!rl&&r
170
not defined $columns or ref $columns eq "ARRAY"
l!l&&r!l&&!r
170
MC/DC42%3/7
not grep({$_ ne "constraint" and $_ ne "columns";} keys %$target) and defined $name || defined $columns and not defined $name && length ref $name and !defined($columns) || ref $columns eq "ARRAY"
not grep({$_ ne "constraint" and $_ ne "columns";} keys %$target) defined $name defined $columns defined $name length ref $name not defined $columns ref $columns eq "ARRAY"
MC/DC100%2/2
$_ ne 'constraint' && $_ ne 'columns'
$_ ne 'constraint' $_ ne 'columns'
Truth table31%5/16
not(A) and B or C and not(D && E) and not(F) or G
A= not grep({$_ ne "constraint" and $_ ne "columns";} keys %$target)B= defined $nameC= defined $columnsD= defined $nameE= length ref $nameF= not defined $columnsG= ref $columns eq "ARRAY"
ABCDEFGresult
0XXXXXX0
100XXXX0
11X11XX0
10111XX0
11X0X000
11X10000
1010X000
10110000
11X0X1X1
11X0X011
11X101X1
11X10011
1010X1X1
1010X011
101101X1
10110011
Truth table100%3/3
A && B
A= $_ ne 'constraint'B= $_ ne 'columns'
ABresult
0X0
100
111
175 and ( defined $name or defined $columns )
176 and ( ! defined $name or ! length ref $name )
177 and ( ! defined $columns or ref $columns eq 'ARRAY' );
178  
179 # key => $constraint arrives as BOTH halves (ADR 0036 section 2): the name
180 # the caller used and the columns core resolved it to. Render the columns.
181 #
182 # A DBIO constraint name is client-side metadata from
183 # add_unique_constraint and need not name anything PostgreSQL knows --
184 # 'primary', which unique_constraint_names returns for the primary key,
185 # exists in no database, so ON CONSTRAINT "primary" is a guaranteed
186 # runtime error. The resolved columns name the very index the constraint
187 # names, and they are correct whether or not the schema was deployed from
188 # this metadata. ON CONSTRAINT stays reachable for the caller who really
189 # means a server-side constraint (below), but it is not how a resolved
190 # target is read.
191 8 ▶ if (defined $columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $columns) { } 7 1 8
192 7 ▶ $self->throw_exception('on_conflict target is an empty column list')
Branch: 1 branch
BranchTrueFalseTotal
unless @$columns 1 6 7
193 unless @$columns;
194 6 $target_sql = '(' . join(', ', map { $self->_quote($_) } @$columns) . ')';
195 }
196 else {
197 # Name and no columns: nothing here can resolve it -- a SQLMaker gets
198 # ($table, $data, $options) and no ResultSource -- so the name is taken
199 # at face value as a server-side constraint. That is the opt-in the
200 # paragraph above leaves open, and it is what an older core
201 # (pre core k104, which passed the name alone) still sends.
202 1 $target_sql = $self->_sqlcase('on constraint ') . $self->_quote($name);
203 }
204 }
205 ▶ elsif (defined $target and ! length ref $target and $target eq 'any') {
Condition50%3/6
defined $target and not length ref $target and $target eq "any"
!ll&&!rl&&r
014
defined $target and not length ref $target
!ll&&!rl&&r
005
MC/DC33%1/3
defined $target and not length ref $target and $target eq "any"
defined $target not length ref $target $target eq "any"
Truth table50%2/4
A and not(B) and C
A= defined $targetB= not length ref $targetC= $target eq "any"
ABCresult
0XX0
10X0
1100
1111
206 # 'any' is MySQL's target-less ON DUPLICATE KEY UPDATE. PostgreSQL's
207 # grammar has no such thing for DO UPDATE -- the arbiter index has to be
208 # named -- so this is an error rather than a guess at which constraint the
209 # caller meant. DO NOTHING is fine without a target, and that combination
210 # is the portable "insert unless it is already there" (ADR 0036 section 1).
211 4 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "update" 1 3 4
212 "PostgreSQL cannot DO UPDATE on an unspecified conflict target: "
213 . "ON CONFLICT DO UPDATE requires the arbiter index to be named. Pass "
214 . "conflict_target => \\\@columns or key => \$constraint_name. "
215 . "conflict_target => 'any' is only portable together with "
216 . "on_conflict => 'nothing'"
217 ) if $action eq 'update';
218 }
219 else {
220 1 $self->throw_exception(
221 "on_conflict target must be an arrayref of columns, "
222 . "{ constraint => \$name, columns => \\\@columns }, or the string 'any'"
223 );
224 }
225  
226 43 my $sql = $self->_sqlcase(' on conflict');
227 43 ▶ $sql .= " $target_sql" if defined $target_sql;
Branch: 1 branch
BranchTrueFalseTotal
if defined $target_sql 40 3 43
228  
229 43 ▶ return ($sql . $self->_sqlcase(' do nothing')) if $action eq 'nothing';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "nothing" 6 37 43
230  
231 37 my $set = $on_conflict->{set};
232 37 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set eq "HASH" and keys %$set 1 36 37
Condition66%2/3
ref $set eq "HASH" and keys %$set
!ll&&!rl&&r
0136
MC/DC50%1/2
ref $set eq "HASH" and keys %$set
ref $set eq "HASH" keys %$set
Truth table66%2/3
A and B
A= ref $set eq "HASH"B= keys %$set
ABresult
0X0
100
111
233 'on_conflict action update needs a non-empty set of columns to write'
234 ) unless ref $set eq 'HASH' and keys %$set;
235  
236 36 my (@set_sql, @bind);
237 36 for my $col (sort keys %$set) {
238 43 my $lhs = $self->_quote($col);
239 43 my $val = $set->{$col};
240  
241 43 ▶ if (ref $val eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 24 19 43
elsif (my $literal = &is_literal_value($val)) { } 6 13 19
242 # -attempted is the engine-neutral core token for "the value this
243 # statement tried to insert". On PostgreSQL that is the EXCLUDED
244 # pseudo-row. The value names the source column, which need not be the
245 # column being written.
246 24 my ($key, @extra) = keys %$val;
247 24 my $src = $val->{'-attempted'};
248  
249 24 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless not @extra and defined $key and $key eq "-attempted" and defined $src and not length ref $src 1 23 24
Condition58%7/12
not @extra and defined $key and $key eq "-attempted" and defined $src and not length ref $src
!ll&&!rl&&r
1023
not @extra and defined $key and $key eq "-attempted" and defined $src
!ll&&!rl&&r
1023
not @extra and defined $key and $key eq "-attempted"
!ll&&!rl&&r
0123
not @extra and defined $key
!ll&&!rl&&r
0024
MC/DC20%1/5
not @extra and defined $key and $key eq "-attempted" and defined $src and not length ref $src
not @extra defined $key $key eq "-attempted" defined $src not length ref $src
Truth table33%2/6
not(A) and B and C and D and not(E)
A= not @extraB= defined $keyC= $key eq "-attempted"D= defined $srcE= not length ref $src
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
250 "on_conflict set for '$col' must be { -attempted => \$column }, a "
251 . 'literal, or a plain value'
252 ) unless ! @extra and defined $key and $key eq '-attempted'
253 and defined $src and ! length ref $src;
254  
255 23 push @set_sql, sprintf '%s = %s',
256 $lhs, $self->_upsert_attempted_sql($src);
257 }
258 elsif (my $literal = is_literal_value($val)) {
259 6 my ($literal_sql, @literal_bind) = @$literal;
260 6 push @set_sql, "$lhs = $literal_sql";
261 6 push @bind, @literal_bind;
262 }
263 else {
264 13 push @set_sql, "$lhs = ?";
265 # The maker's own bindtype shapes the bind: [ $col => $val ] under
266 # 'columns' (the sync driver), the bare value under 'normal' (the
267 # future_io and ev transports build their maker without one). A pair
268 # pushed by hand among bare values reaches the driver as an array.
269 13 push @bind, $self->_bindtype($col, $val);
270 }
271 }
272  
273 35 $sql .= $self->_sqlcase(' do update set ') . join(', ', @set_sql);
274  
275 # ADR 0036 section 1 (core k105): the conditional conflict branch. The key
276 # exists only when the caller passed update_where, so every call written
277 # before the option existed still renders byte-identical SQL. PostgreSQL
278 # wants the condition after the SET list and before RETURNING, and its
279 # binds go in at that same position.
280 #
281 # exists, not defined: the key being there at all is the caller asking for a
282 # conditional branch, and every state of it that cannot become a condition
283 # has to end in the throw below rather than in an unconditional DO UPDATE.
284 35 ▶ if (exists $on_conflict->{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $on_conflict->{"where"}) 14 21 35
285 14 my $where = $on_conflict->{where};
286  
287 # undef is not a condition, so it never reaches the renderer -- whether
288 # SQL::Abstract tolerates it, warns or dies is a dependency's business, and
289 # the message below is not going to be left to it. It falls through to the
290 # same throw with an empty render.
291 14 ▶ my ($where_sql, @where_bind) = defined $where
Branch: 1 branch
BranchTrueFalseTotal
defined $where ? : 13 1 14
292 ? $self->_upsert_condition_sql($where, $table)
293 : ()
294 ;
295  
296 13 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where_sql and length $where_sql 2 11 13
Condition100%3/3
defined $where_sql and length $where_sql
!ll&&!rl&&r
1111
MC/DC100%2/2
defined $where_sql and length $where_sql
defined $where_sql length $where_sql
Truth table100%3/3
A and B
A= defined $where_sqlB= length $where_sql
ABresult
0X0
100
111
297 'on_conflict where rendered to an empty condition -- an unconditional '
298 . 'DO UPDATE is the overwrite the condition exists to prevent'
299 ) unless defined $where_sql and length $where_sql;
300  
301 11 $sql .= $self->_sqlcase(' where ') . $where_sql;
302 11 push @bind, @where_bind;
303 }
304  
305 32 return ($sql, @bind);
306 }
307  
308  
309 29 ▶sub _upsert_attempted_sql {
Subroutine _upsert_attempted_sql: called CC=1 SCAR=0.0
310 29 my ($self, $col) = @_;
311 29 return $self->_sqlcase('excluded') . '.' . $self->_quote($col);
312 }
313  
314  
315  
316 137 ▶sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
317 137 my $self = shift;
318  
319 # The base _lock_select only receives $rs_attrs->{for}, but the PostgreSQL
320 # row-lock modifiers ride on the sibling attributes skip_locked / nowait.
321 # Stash the whole rs_attrs (4th positional: table, fields, where, rs_attrs)
322 # for the duration of the base call so our _lock_select override can read
323 # them. local restores it afterwards and keeps nested selects independent.
324 137 local $self->{_pg_row_lock_attrs} = $_[3];
325  
326 137 return $self->next::method(@_);
327 }
328  
329 5 ▶sub _lock_select {
Subroutine _lock_select: called CC=6 SCAR=17.9
330 5 my ($self, $type) = @_;
331  
332 5 my $sql = $self->next::method($type);
333  
334 5 ▶ my $attrs = $self->{_pg_row_lock_attrs} || {};
Condition50%1/2
$self->{'_pg_row_lock_attrs'} || {}
l!l
50
MC/DC0%0/1
$self->{'_pg_row_lock_attrs'} || {}
$self->{'_pg_row_lock_attrs'}
Truth table50%1/2
A
A= $self->{'_pg_row_lock_attrs'}
Aresult
11
00
335 5 my $skip_locked = $attrs->{skip_locked};
336 5 my $nowait = $attrs->{nowait};
337  
338 5 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if $skip_locked and $nowait 1 4 5
Condition100%3/3
$skip_locked and $nowait
!ll&&!rl&&r
221
MC/DC100%2/2
$skip_locked and $nowait
$skip_locked $nowait
Truth table100%3/3
A and B
A= $skip_lockedB= $nowait
ABresult
0X0
100
111
339 "The row-lock options 'skip_locked' and 'nowait' are mutually exclusive"
340 ) if $skip_locked && $nowait;
341  
342 4 ▶ $sql .= ' SKIP LOCKED' if $skip_locked;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_locked 2 2 4
343 4 ▶ $sql .= ' NOWAIT' if $nowait;
Branch: 1 branch
BranchTrueFalseTotal
if $nowait 1 3 4
344  
345 4 return $sql;
346 }
347  
348  
349  
350 19 1;
351  
352 __END__
« blib/lib/DBIO/PostgreSQL/Diff.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/Column.pm »