| 1 | package DBIx::Class::Helper::SimpleStats; | ||||||||||||
| 2 | |||||||||||||
| 3 | # ABSTRACT: Simple grouping and aggregate functions for DBIx::Class | ||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use v5.10.1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use base qw( DBIx::Class ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use List::Util 1.45 qw/ uniqstr /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Ref::Util qw/ is_plain_hashref is_ref /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | # RECOMMEND PREREQ: Ref::Util::XS | ||||||||||||
| 17 | |||||||||||||
| 18 | 1 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 1 | our $VERSION = 'v0.1.3'; | |||||||||||
| 21 | |||||||||||||
| 22 | |||||||||||||
| 23 | |||||||||||||
| 24 | 6 | ▶ | sub simple_stats { | ||||||||||
|
Subroutine
simple_stats: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 25 | 6 | my ( $self, @args ) = @_; | |||||||||||
| 26 | |||||||||||||
| 27 | 6 | ▶ | croak "No columns" unless @args; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | |||||||||||||
| 29 | 6 | my @cols; | |||||||||||
| 30 | my @funcs; | ||||||||||||
| 31 | |||||||||||||
| 32 | 6 | my $me = $self->current_source_alias; | |||||||||||
| 33 | |||||||||||||
| 34 | 20 | ▶ | my $alias = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||
| 35 | 20 | my $ident = shift; | |||||||||||
| 36 | 20 | ▶ | $ident = "$me.$ident" unless $ident =~ /^(\w+)\./; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 20 | return $ident; | |||||||||||
| 38 | 6 | }; | |||||||||||
| 39 | |||||||||||||
| 40 | 6 | foreach my $arg (@args) { | |||||||||||
| 41 | |||||||||||||
| 42 | 11 | ▶ | if ( is_ref($arg) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | |||||||||||||
| 44 | 7 | ▶ | if ( is_plain_hashref($arg) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | |||||||||||||
| 46 | 7 | my $as = delete $arg->{'-as'}; | |||||||||||
| 47 | 7 | my ( $func, $col ) = each %{$arg}; | |||||||||||
| 48 | |||||||||||||
| 49 | 7 | ▶ | $as //= "${col}_${func}"; | ||||||||||
|
Condition100%2/2
$as //= "${col}_$func"
MC/DC100%1/1
$as //= "${col}_$func"
$as
Truth table100%2/2
A
A= $as
| |||||||||||||
| 50 | |||||||||||||
| 51 | 7 | push @cols, $alias->($col); | |||||||||||
| 52 | |||||||||||||
| 53 | 7 | push @funcs, { $func => $alias->($col), -as => $as }; | |||||||||||
| 54 | |||||||||||||
| 55 | } | ||||||||||||
| 56 | else { | ||||||||||||
| 57 | |||||||||||||
| 58 | 0 | croak "Unsupported reference type: " . ref($arg); | |||||||||||
| 59 | |||||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | } | ||||||||||||
| 63 | else { | ||||||||||||
| 64 | |||||||||||||
| 65 | 4 | push @cols, $alias->($arg); | |||||||||||
| 66 | |||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 6 | ▶ | unless (@funcs) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | |||||||||||||
| 73 | 2 | my $func = "count"; | |||||||||||
| 74 | 2 | my $col = $cols[0]; | |||||||||||
| 75 | 2 | $col =~ s/^\w+\.// ; | |||||||||||
| 76 | |||||||||||||
| 77 | 2 | push @funcs, { $func => $alias->($col), -as => "${col}_${func}" }; | |||||||||||
| 78 | |||||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 6 | my @names = map { delete $_->{'-as'} } @funcs; | |||||||||||
| 82 | |||||||||||||
| 83 | 6 | my @group = uniqstr @cols; | |||||||||||
| 84 | |||||||||||||
| 85 | 6 | return $self->search( | |||||||||||
| 86 | undef, | ||||||||||||
| 87 | { | ||||||||||||
| 88 | group_by => \@group, | ||||||||||||
| 89 | select => [ @group, @funcs ], | ||||||||||||
| 90 | as => [ @group, @names ], | ||||||||||||
| 91 | order_by => \@group, | ||||||||||||
| 92 | } | ||||||||||||
| 93 | ); | ||||||||||||
| 94 | |||||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | |||||||||||||
| 98 | |||||||||||||
| 99 | |||||||||||||
| 100 | 1 | 1; | |||||||||||
| 101 | |||||||||||||
| 102 | __END__ | ||||||||||||