CrossTab.pm

filter: Statementstmt 96.3% 53 / 55 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 5 / 7 Podpod 100% 1 / 1 totaltotal 93.6% 59 / 63 CC 1 SCAR 0.4
CC
1
Cov
96%
__ANON__
1.1
__ANON__
1.1
crosstab
1.0
CRAP 1.0
« CrossTab/Util.pm ↑ Summary
1 package DBIx::Class::Helper::ResultSet::CrossTab;
2  
3 # ABSTRACT: helper to simulate crosstab
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use parent 'DBIx::Class::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # use Data::Dump qw/dump/;
11  
12 0 ▶my $quote = sub { return "'" . $_[0] . "'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
13  
14 5 ▶use DBIx::Class::Helper::ResultSet::CrossTab::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶sub crosstab {
Subroutine crosstab: called CC=1 SCAR=0.0
Pod documented
17 7 my $self = shift;
18 7 my $query = shift;
19 7 my $opts = shift;
20  
21 0 ▶ $DBIx::Class::Helper::ResultSet::CrossTab::Util::quote = sub { return $self->result_source->storage->dbh->quote( $_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
22  
23 7 my @values;
24  
25 # ---------------------------------------------------------------------
26 # get the values for pivoting
27 # ---------------------------------------------------------------------
28 7 my $v = $self->search($query, { columns => $opts->{on}, group_by => $opts->{on}, order_by => $opts->{on} });
29 7 $v->result_class('DBIx::Class::ResultClass::HashRefInflator');
30 7 @values = map { my %h; @h{@{$opts->{on}}} = @{$_}{@{$opts->{on}}}; \%h; } $v->all;
31  
32 # ---------------------------------------------------------------------
33 # create the pivot fields
34 # ---------------------------------------------------------------------
35 7 my @funcs = @{ $opts->{pivot}};
36 7 my @cross_select = map { my $f = $_; map { my $r = summary_function_to_pivot_field_def($f, %{$_}); \$r } @values } @funcs;
37 7 my @cross_as = map { my $f = $_; map { my $r = summary_function_to_pivot_field_name($f, %{$_}); $r } @values } @funcs;
38  
39 7 my $re = quotemeta($opts->{on}->[0]);
40  
41 7 my (@as, @select);
42  
43 7 @select = grep { !/$re/ } @{$self->_resolved_attrs->{select}};
44 7 @as = @select;
45  
46 my $cross = $self
47 ->search($query, {
48 'select' => [ @select, @cross_select ],
49 'as' => [ @as, @cross_as ],
50 group_by => $opts->{group_by}
51 7 });
52  
53 7 my ($sql, @bind) = @{${$cross->as_query}};
54 7 return $cross;
55 }
56  
57 5 1;
58  
59 #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
60  
61 =head1 NAME
62  
63 DBIx::Class::Helper::ResultSet::CrossTab
64  
65 =head1 DESCRIPTION
66  
67 A helper to simulate crosstab functionality in DBIx::Class
68  
69 =head1 REQUIRES
70  
71 L<String::SQLColumnName>
72  
73 L<parent>
74  
75 =head1 METHODS
76  
77 =head2 crosstab
78  
79 my $r = $s->search({}, { columns => [qw/fruit channel/], } )
80 ->crosstab({}, {
81 select => [qw/fruit/],
82 on => [qw/channel/],
83 pivot => [ { sum => 'units' } ],
84 group_by => [qw/fruit/]
85 });
86  
87 $r->result_class('DBIx::Class::ResultClass::HashRefInflator'); # to inflate rows
88  
89 =head2 ATTRIBUTES
90  
91 =head3 pivot
92  
93 This includes all functions and fields that must be summarized as an arrayref, in the same way as data can be aggregated in DBIx::Class.
94  
95 these:
96  
97 pivot => [ { sum => 'units' } ] # aggregate function and field
98
99 pivot => [ \"sum(units)" ] # literal SQL
100  
101 will both provide a sum of units as the result of the crosstab, while this:
102  
103 pivot => [ \"sum(units)", { avg => 'units }, \"count(distinct country)" ] # mixed, multiple fields
104  
105 will generate a total of units, the average and the number of distinct countries
106  
107 =head3 on
108  
109 This is the field (or function thereof) that will generate the column headers
110  
111 on => [qw/channel/]
112  
113 will generate columns by channel
114  
115 on => [qw/channel country/]
116  
117 will have one column per channel and country combination. It's not possible yet to pivot on a function of a field.
118  
119 =head3 others
120  
121 All other attributes work as documented in DBIx::Class::ResultSet
122  
123 -head2 CHANGES
124  
125 Version 0.006 adds functionality for nested summary functions and therefore breaks column naming.
126  
127 =head2 COPYRIGHT AND LICENSE
128  
129 Copyright 2016-2017 Simone Cesano
130  
131 This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
132  
133 =head2 CREDITS
134  
135 I owe the general idea to Giuseppe Maxia, and I re-used a couple of code snippets from Arthur Axel "fREW" Schmidt.
136  
137 =cut
138  
« CrossTab/Util.pm ↑ Summary