blib/lib/DBIx/Class/Helper/TableSample.pm

filter: Statementstmt 95.4% 42 / 44 Branchbran 83.3% 15 / 18 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 91.0% 71 / 78 CC 12 SCAR 18.6
CC
12
Cov
91%
_resolved_attrs
10.2
tablesample
3.0
BEGIN
1.0
CRAP 6.4
↑ Summary
1 package DBIx::Class::Helper::TableSample;
2  
3 # ABSTRACT: Add support for tablesample clauses
4  
5 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use parent 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Ref::Util qw/ is_plain_arrayref is_plain_hashref is_plain_scalarref /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # RECOMMEND PREREQ: Ref::Util::XS
13  
14 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = 'v0.7.0';
17  
18  
19 25 ▶sub _resolved_attrs {
Subroutine _resolved_attrs: called CC=10 SCAR=23.2
20 25 my $rs = $_[0];
21  
22 25 $rs->next::method;
23  
24 25 my $attrs = $rs->{_attrs};
25  
26 25 ▶ if ( my $conf = delete $attrs->{tablesample} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conf = delete $attrs->{"tablesample"}) 25 0 25
27  
28 25 my $from = $attrs->{from};
29  
30 25 ▶ $conf = { fraction => $conf } unless is_plain_hashref($conf);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($conf) 4 21 25
31  
32 25 ▶ $rs->throw_exception('tablesample must be a hashref')
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($conf) 0 25 25
33 unless is_plain_hashref($conf);
34  
35 25 my $sqla = $rs->result_source->storage->sql_maker;
36  
37 25 my $part_sql = " tablesample";
38  
39 25 ▶ if (my $type = ($conf->{method} // $conf->{type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $conf->{"method"} // $conf->{"type"}) 13 12 25
Condition100%3/3
$conf->{"method"} // $conf->{"type"}
l!l&&r!l&&!r
9412
MC/DC100%2/2
$conf->{"method"} // $conf->{"type"}
$conf->{"method"} $conf->{"type"}
Truth table100%3/3
A // B
A= $conf->{"method"}B= $conf->{"type"}
ABresult
1X1
011
000
40 13 $part_sql .= " $type";
41 }
42  
43 25 my $arg = $conf->{fraction};
44 25 ▶ $arg = $$arg if is_plain_scalarref($arg);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_plain_scalarref($arg) 6 19 25
45 25 $part_sql .= "($arg)";
46  
47 25 ▶ if ( defined $conf->{repeatable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"repeatable"}) 4 21 25
48 4 my $seed = $conf->{repeatable};
49 4 ▶ $seed = $$seed if is_plain_scalarref($seed);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_plain_scalarref($seed) 2 2 4
50 4 $part_sql .= sprintf( ' repeatable (%s)', $seed );
51 }
52  
53 25 ▶ if (is_plain_arrayref($from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($from)) { } 25 0 25
54 25 my $sql = $sqla->_from_chunk_to_sql($from->[0]) . $sqla->_sqlcase($part_sql);
55 25 $from->[0] = \$sql;
56 }
57 else {
58 0 my $sql = $sqla->_from_chunk_to_sql($from) . $sqla->_sqlcase($part_sql);
59 0 $attrs->{from} = \$sql;
60 }
61  
62 }
63  
64 25 return $attrs;
65 }
66  
67  
68 14 ▶sub tablesample {
Subroutine tablesample: called CC=3 SCAR=11.0
Pod documented
69 14 my ( $rs, $frac, $options ) = @_;
70 14 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
140
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
71 14 ▶ $options = { method => $options } unless is_plain_hashref($options);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($options) 3 11 14
72 14 return $rs->search_rs(
73 undef,
74 {
75 tablesample => {
76 fraction => $frac,
77 %$options
78 }
79 }
80 );
81 }
82  
83  
84 1 1;
85  
86 __END__
↑ Summary