DBIx/Class/SQLA2.pm

filter: Statementstmt 96.3% 53 / 55 Branchbran 62.5% 10 / 16 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 13 / 13 Podpod 75.0% 3 / 4 totaltotal 83.1% 84 / 101 CC 13 SCAR 10.5
CC
13
Cov
85%
_render_hashrefrefs
5.8
new
3.0
_recurse_fields
3.0
CRAP 2.9
« DBIx/Class/ResultSet/SQLA2Support.pm ↑ Summary SQL/Abstract/Plugin/CaseExpr.pm »
1 package DBIx::Class::SQLA2;
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 feature 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶no warnings 'experimental::postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use base qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 DBIx::Class::SQLMaker::ClassicExtensions
10 SQL::Abstract
11 SQL::Abstract::Classic
12 5 );
13  
14 5 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 __PACKAGE__->mk_group_accessors(simple => '_connection_info');
17  
18 5 ▶sub _render_hashrefrefs {
Subroutine _render_hashrefrefs: called CC=5 SCAR=17.5
19 5 my ($self, $list) = @_;
20 5 ▶ my @fields = ref $list eq 'ARRAY' ? @$list : $list;
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq 'ARRAY' ? : 0 5 5
21 return [
22 map {
23 5 ref $_ eq 'REF' && ref $$_ eq 'HASH'
24 5 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'REF' && ref $$_ eq 'HASH' ? : 5 0 5
Condition33%1/3
ref $_ eq "REF" and ref $$_ eq "HASH"
!ll&&!rl&&r
005
MC/DC0%0/2
ref $_ eq "REF" and ref $$_ eq "HASH"
ref $_ eq "REF" ref $$_ eq "HASH"
Truth table33%1/3
A and B
A= ref $_ eq "REF"B= ref $$_ eq "HASH"
ABresult
0X0
100
111
25 5 my %f = $$_->%*;
26 5 my $as = delete $f{-as};
27 \[
28 5 ▶ $as
Branch: 1 branch
BranchTrueFalseTotal
$as ? : 5 0 5
29 ? $self->render_expr({ -op => [ 'as', \%f, { -ident => $as } ] })
30 : $self->render_expr(\%f)
31 ];
32 }
33 : $_
34 } @fields
35 ];
36 }
37  
38 119 ▶sub _recurse_fields {
Subroutine _recurse_fields: called CC=3 SCAR=11.0
39 119 my ($self, $fields) = @_;
40 119 ▶ if (ref $fields eq 'REF' && ref $$fields eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fields eq "REF" and ref $$fields eq "HASH") 5 114 119
Condition66%2/3
ref $fields eq "REF" and ref $$fields eq "HASH"
!ll&&!rl&&r
11405
MC/DC50%1/2
ref $fields eq "REF" and ref $$fields eq "HASH"
ref $fields eq "REF" ref $$fields eq "HASH"
Truth table66%2/3
A and B
A= ref $fields eq "REF"B= ref $$fields eq "HASH"
ABresult
0X0
100
111
41 5 return $self->next::method($self->_render_hashrefrefs($fields)->[0]);
42 }
43 114 return $self->next::method($fields);
44  
45 }
46  
47 38 ▶sub select {
Subroutine select: called CC=2 SCAR=7.6
Pod documented
48 38 my ($self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
49  
50 38 ▶ if (my $gb = $rs_attrs->{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gb = $rs_attrs->{"group_by"}) 0 38 38
51 0 $rs_attrs = { %$rs_attrs, group_by => $self->_render_hashrefrefs($gb) };
52 }
53 38 $self->next::method($table, $fields, $where, $rs_attrs, $limit, $offset);
54 }
55  
56  
57 25 ▶sub insert {
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
58 # TODO - this works, ish. The issue is that if you have rels involved, you may actually
59 # hit `insert` before the intended insert. Not sure what to do but put that on the
60 # user...
61 25 my ($self, $source, $cols, $attrs) = @_;
62 25 ▶ $attrs ||= {};
Condition50%1/2
$attrs ||= {}
l!l
025
MC/DC0%0/1
$attrs ||= {}
$attrs
Truth table50%1/2
A
A= $attrs
Aresult
11
00
63 25 ▶ if (my $extra_attrs = $self->{_sqla2_insert_attrs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra_attrs = $self->{"_sqla2_insert_attrs"}) 7 18 25
64 7 $attrs = { %$attrs, %$extra_attrs };
65 }
66 25 $self->next::method($source, $cols, $attrs);
67 }
68  
69 3 ▶sub expand_clause {
Subroutine expand_clause: called CC=2 SCAR=7.2
Pod undocumented
70 3 my ($self, $clause, $value) = @_;
71 3 my ($probably_key, $expanded) = $self->${ \$self->clause_expander($clause) }(undef, $value);
72 3 ▶ if ($expanded) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expanded) { } 3 0 3
73 3 return ($probably_key => $expanded);
74 } else {
75 0 return (undef => $probably_key);
76 }
77 }
78  
79 10 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
80 10 my $new = shift->next::method(@_);
81 10 ▶ unless (grep {m/^with$/} $new->clauses_of('select')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/^with$/;} $new->clauses_of("select")) 10 0 10
82 10 $new->plugin("+$_") for qw/ExtraClausesFixed WindowFunctions Upsert BangOverrides CaseExpr/;
83 }
84 10 return $new;
85 }
86  
87 5 our $VERSION = '0.02_1';
88  
89 5 1;
90  
91 =encoding utf8
92  
93 =head1 NAME
94  
95 DBIx::Class::SQLA2 - SQL::Abstract v2 support in DBIx::Class
96  
97 =head1 SYNOPSIS
98  
99 $schema->connect_call_rebase_sqlmaker('DBIx::Class::SQLA2');
100  
101 =head1 DESCRIPTION
102  
103 This is a work in progress for simplifying using SQLA2 with DBIC. This is for using w/ the
104 most recent version of DBIC.
105  
106 For a simple way of using this, take a look at L<DBIx::Class::Schema::SQLA2Support>.
107  
108 In order to have inserts passthru SQLA2 keys, you should use the
109 L<DBIx::Class::Row::SQLA2Support> component, which allows you to add a C<-sqla2> key w/ a
110 hashref containing your passthroughs.
111  
112 In order to support SQLA2 keys for bulk inserts (C<populate>), use the
113 L<DBIx::Class::ResultSet::SQLA2Support> component. It adds a second arg to C<populate> which
114 allows you to add things to the final query (though you may get undefined behavior if
115 you're creating deep relationships - real world experience would be nice here). In
116 addition, it adds an C<upsert> method to simplify the case of update on a primary
117 key conflict (see there for more details).
118  
119 B<EXPERIMENTAL>
120  
121 This role itself will add handling of hashref-refs to select lists + group by clauses,
122 which will render the inner hashref as if it had been passed through to SQLA2 rather than
123 doing the recursive function rendering that DBIC does.
124  
125 =head2 Included Plugins
126  
127 This will add the following SQLA2 plugins:
128  
129 =over 2
130  
131 =item L<SQL::Abstract::Plugin::ExtraClauses>
132  
133 Adds support for CTEs, and other fun new SQL syntax
134  
135 =item L<SQL::Abstract::Plugin::WindowFunctions>
136  
137 Adds support for window functions and advanced aggregates.
138  
139 =item L<SQL::Abstract::Plugin::Upsert>
140  
141 Adds support for Upserts (ON CONFLICT clause)
142  
143 =item L<SQL::Abstract::Plugin::BangOverrides>
144  
145 Adds some hacky stuff so you can bypass/supplement DBIC's handling of certain clauses
146  
147 =back
148  
149 =head1 AUTHOR
150  
151 Copyright (c) 2022-24 Veesh Goldman <veesh@cpan.org>
152  
153 =head1 LICENSE
154  
155 This module is free software; you may copy this under the same
156 terms as perl itself (either the GNU General Public License or
157 the Artistic License)
158  
159 =cut
« DBIx/Class/ResultSet/SQLA2Support.pm ↑ Summary SQL/Abstract/Plugin/CaseExpr.pm »