blib/lib/SQL/Abstract/Plugin/Upsert.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 10 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 94.4% 51 / 54 SCAR 20.8
CC
8
Cov
98%
__ANON__
5.0
__ANON__
4.0
register_extensions
1.0
CRAP 8.0
« blib/lib/SQL/Abstract/Plugin/ExtraClausesFixed.pm ↑ Summary blib/lib/DBIx/Class/Schema/SQLA2Support.pm »
1 package SQL::Abstract::Plugin::Upsert;
2  
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 with 'SQL::Abstract::Role::Plugin';
5  
6 sub register_extensions {
7 10 ▶ my ($self, $sqla) = @_;
Subroutine register_extensions: called CC=1 SCAR=0.0
Pod undocumented
8 $sqla->clauses_of(
9 'insert' => sub {
10 10 ▶ my ($self, @clauses) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 10 splice @clauses, -1, 0, 'on_conflict';
12 10 @clauses;
13 }
14 10 );
15 $sqla->clause_expander(
16 'insert.on_conflict' => sub {
17 6 ▶ my ($sqla, $name, $value) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
18 # a 0 is DO NOTHING
19 6 ▶ return (on_conflict => { -do => 'NOTHING' }) unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 4 2 6
20  
21 # if we have keys that aren't prefixed by -, it's { TARGET => { SET_THIS => TO_THIS } }
22 2 ▶ if (!grep /^-/, keys %$value and keys %$value == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep(/^-/, keys %$value) and keys %$value == 1) 1 1 2
Condition66%2/3
not grep(/^-/, keys %$value) and keys %$value == 1
!ll&&!rl&&r
101
MC/DC50%1/2
not grep(/^-/, keys %$value) and keys %$value == 1
not grep(/^-/, keys %$value) keys %$value == 1
Truth table66%2/3
not(A) and B
A= not grep(/^-/, keys %$value)B= keys %$value == 1
ABresult
0X0
100
111
23 1 my @built;
24 1 for my $target (keys %$value) {
25 1 $value = { -target => $target, -set => $value->{$target} };
26 }
27 }
28 2 my (undef, $set) = $sqla->expand_clause('update.set', $value->{-set});
29 2 my $target = $sqla->expand_expr({ -list => $value->{-target} }, -ident);
30 2 return (on_conflict => { -target => $target, -set => $set });
31 }
32 10 );
33 $sqla->clause_renderer(
34 'insert.on_conflict' => sub {
35 6 ▶ my ($sqla, $type, $value) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
36 6 my @parts;
37 6 @parts = { -keyword => 'on conflict' };
38 6 ▶ if (my $target = $value->{-target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $target = $value->{"-target"}) 2 4 6
39 2 push @parts, '(', $sqla->render_aqt($target), ')';
40 }
41 6 ▶ if (my $what_to_do = $value->{-do}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $what_to_do = $value->{"-do"}) 4 2 6
42 4 push @parts, { -keyword => "DO $what_to_do" };
43 }
44 6 ▶ if (my $set = $value->{-set}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set = $value->{"-set"}) 2 4 6
45 2 push @parts, { -keyword => 'DO UPDATE SET' };
46 2 push @parts, $set;
47 }
48 6 $sqla->join_query_parts(' ', @parts);
49 }
50 10 );
51 }
52  
53 5 our $VERSION = '0.01_4';
54  
55 5 1;
56  
57 =encoding utf8
58  
59 =head1 NAME
60  
61 SQL::Abstract::Plugin::Upsert - Upsert (ON CONFLICT) support for SQLA2!
62  
63 =head1 SYNOPSIS
64  
65 # pass this to an SQLA 'insert'
66 { on_conflict => 0 }
67 # ON CONFLICT DO NOTHING
68  
69 # Do an update
70 { on_conflict => { id => { name => 'Bob Bobson' } } }
71 # ON CONFLICT (id) DO UPDATE SET name = 'Bob Bobson'
72  
73 # Slightly fancier
74 { on_conflict => { id => { name => \'name || ' ' || excluded.name } } }
75 # ON CONFLICT (id) DO UPDATE SET name = name || ' ' || excluded.name
76  
77 # More explicit
78 { on_conflict => { -target => 'id', -set => { name => 'Bob Bobson' } } }
79 # ON CONFLICT (id) DO UPDATE SET name = 'Bob Bobson'
80  
81 =head1 DESCRIPTION
82  
83 This is a work in progress to support upserts in SQLA2.
84  
85 B<EXPERIMENTAL>
86  
87 =head2 Using with DBIx::Class
88  
89 In order to use this with DBIx::Class, you need to add plugins to your Result and ResultSet classes.
90  
91 # In your Result:: Classes (you could also just inherit from it)
92 __PACKAGE__->load_components('Row::SQLA2Support');
93  
94 # In your ResultSet Classes (you could also just inherit from it)
95 __PACKAGE__->load_components('ResultSet::SQLA2Support')
96  
97 Now you can do the following cool things!
98  
99 =head3 create
100  
101 When making a new Row (like using $rs->create and friends), you can pass in a -on_conflict key which will get passed through to the INSERT for that row.
102  
103 $rs->create({ id => 3, name => 'John', -on_conflict => 0 });
104 # ON CONFLICT DO NOTHING
105
106 You can also pass a -upsert key to let us create the correct ON CONFLICT clause to just
107 stomp any existing row. This is safer than the usual find_or_create. This handles
108 composite PKs just fine, by the way.
109  
110 $rs->create({ id => 3, name => 'Bob Bobson', -upsert => 1 })
111 # ON CONFLICT (id) DO UPDATE SET name = 'Bob Bobson'
112  
113 =head3 populate
114  
115 When doing a multi-insert, you can pass in a second arg after the rows to be passed
116 through to SQLA2; this allows you to do a blanket ON CONFLICT DO NOTHING for the whole bunch of INSERTs.
117  
118 $rs->populate([
119 # one million rows later
120 ], { on_conflict => 0 })
121  
122 =cut
« blib/lib/SQL/Abstract/Plugin/ExtraClausesFixed.pm ↑ Summary blib/lib/DBIx/Class/Schema/SQLA2Support.pm »