DBIx/Class/ResultSet/SQLA2Support.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 70.0% 7 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 91.1% 62 / 68 CC 11 SCAR 10.9
CC
11
Cov
91%
upsert
5.0
populate_upsert
4.3
populate
3.0
CRAP 3.0
« SQL/Abstract/Plugin/Upsert.pm ↑ Summary DBIx/Class/SQLA2.pm »
1 package DBIx::Class::ResultSet::SQLA2Support;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use parent 'DBIx::Class::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use List::Util 'pairmap';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶my sub _create_upsert_clause ($self, $columns_ary, $overrides) {
Subroutine _create_upsert_clause: called CC=1 SCAR=0.0
10 3 my %pks = map +($_ => 1), $self->result_source->primary_columns;
11 return +{
12 -target => [ keys %pks ],
13 -set => {
14 # create a hash of non-pk cols w/ the value excluded.$_, which does the upsert
15 3 (map +($_ => { -ident => "excluded.$_" }), grep !$pks{$_}, $columns_ary->@*),
16 # and allow overrides from the caller
17 $overrides->%*
18 }
19 };
20 }
21  
22 2 ▶sub upsert ($self, $to_insert, $overrides = {}) {
Subroutine upsert: called CC=5 SCAR=16.2
Pod documented
23 # in case there are other passthroughs, you never know
24 2 ▶ my $sqla2_passthru = delete $to_insert->{-sqla2} || {};
Condition50%1/2
delete $to_insert->{'-sqla2'} || {}
l!l
02
MC/DC0%0/1
delete $to_insert->{'-sqla2'} || {}
delete $to_insert->{'-sqla2'}
Truth table50%1/2
A
A= delete $to_insert->{'-sqla2'}
Aresult
11
00
25  
26 # evil handling for RETURNING, b/c DBIC doesn't give us a place to do it properly.
27 # Basically force each input value to be a ref, and update the column config to use
28 # RETURNING, thus ensuring we get RETURNING handling
29 2 for my $col (keys $overrides->%*) {
30 1 ▶ next if ref $to_insert->{$col};
Branch: 1 branch
BranchTrueFalseTotal
if ref $to_insert->{$col} 0 1 1
31 1 $to_insert->{$col} = \[ '?' => $to_insert->{$col} ];
32 }
33 2 my $source = $self->result_source;
34 local $source->{_columns}
35 4 ▶ = { pairmap { $a => { $b->%*, exists $overrides->{$a} ? (retrieve_on_insert => 1) : () } }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $overrides->{$a} ? : 1 3 4
36 2 $source->{_columns}->%* };
37  
38 2 $sqla2_passthru->{on_conflict} = _create_upsert_clause($self, [ keys $to_insert->%* ], $overrides);
39 2 $self->create({ $to_insert->%*, -sqla2 => $sqla2_passthru });
40 }
41  
42 22 ▶sub populate ($self, $to_insert, $attrs = undef) {
Subroutine populate: called CC=3 SCAR=11.0
Pod documented
43 # NOTE - hrm, relations is a hard problem here. A "DO NOTHING" should be global, which
44 # is why we don't stomp when empty
45 22 ▶ local $self->result_source->storage->sql_maker->{_sqla2_insert_attrs} = $attrs if $attrs;
Branch: 1 branch
BranchTrueFalseTotal
if $attrs 2 20 22
46 22 $self->next::method($to_insert);
47 }
48  
49 1 ▶sub populate_upsert ($self, $to_insert, $overrides = {}) {
Subroutine populate_upsert: called CC=4 SCAR=14.6
Pod documented
50 1 ▶ croak "populate_upsert must be called in void context" if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 1 1
51 1 my @inserted_cols;
52 1 ▶ if (ref $to_insert->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to_insert->[0] eq 'ARRAY') { } 0 1 1
53 0 @inserted_cols = $to_insert->[0]->@*;
54 } else {
55 1 @inserted_cols = keys $to_insert->[0]->%*;
56 }
57 1 $self->populate($to_insert, { on_conflict => _create_upsert_clause($self, \@inserted_cols, $overrides) });
58 }
59  
60 5 1;
61  
62 =encoding utf8
63  
64 =head1 NAME
65  
66 DBIx::Class::SQLA2 - SQL::Abstract v2 support in DBIx::Class
67  
68 =head1 SYNOPSIS
69  
70 # resultset code
71 package MyApp::Schema::ResultSet;
72 __PACKAGE__->load_components('ResultSet::SQLA2Support');
73  
74  
75 # client code
76 my $rs = $schema->resultset('Album')->populate([{ name => 'thing' }, { name => 'stuff' } ], -sqla2 => { on_conflict => 0 }})
77  
78 =head1 DESCRIPTION
79  
80 This is a work in progress for simplifying using SQLA2 with DBIC. This is for using w/ the
81 most recent version of DBIC.
82  
83 B<EXPERIMENTAL>
84  
85 Allows you to passthru sqla2 options as an extra arg to populate, as in the SYNOPSIS. In
86 addition, provides some extra methods.
87  
88 =head2 METHODS
89  
90 =over 2
91  
92 =item upsert
93  
94 # on conflict, add this name to the existing name
95 $rs->upsert({ name => 'thingy', id => 9001 }, { name => \"name || ' ' || excluded.name" });
96  
97 The first argument is the same as you would pass to a call to C<insert>, except we generate
98 an ON CONFLICT clause which will upsert all non-primary-key values. You can pass a hashref
99 as the second argument to override the default on conflict value. You can pass in anything
100 (literal SQL is quite helpful here) and it will be retrieved by DBIC on the insert using
101 DBIC's return_on_insert functionality.
102  
103 =item populate_upsert
104  
105 # on conflict, add this name to the existing name
106 $rs->populate_upsert([{ name => 'thingy', id => 9001 }, { name => 'over 9000', id => 9002 }], { name => \"name || ' ' || excluded.name" });
107  
108 Same as C<upsert> above, just for C<populate>. Dies a horrible death if called in non-void context.
109  
110 =back
111  
112 =cut
« SQL/Abstract/Plugin/Upsert.pm ↑ Summary DBIx/Class/SQLA2.pm »