| 1 | package DBIx::Class::Helper::ResultSet::CrossTab; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: helper to simulate crosstab | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 4 | ▶ | use parent 'DBIx::Class::ResultSet'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | { | ||||||||||||||||||||
| 11 | package DBIx::Class::ResultSet; | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 4 | ▶ | use Data::Dump qw/dump/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | ▶ | my $quote = sub { return "'" . $_[0] . "'" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | sub crosstab { | ||||||||||||||||||||
| 18 | 5 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
crosstab: called CC=6 SCAR=18.8
Pod
undocumented
| |||||||||||||||||||||
| 19 | 5 | my $query = shift; | |||||||||||||||||||
| 20 | 5 | my $opts = shift; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 5 | ▶ | $quote = sub { return $self->result_source->storage->dbh->quote( $_[0]) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 5 | my @values; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 5 | ▶ | if ($opts->{in} && scalar @{$opts->{in}}) { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$opts->{"in"} and scalar @{$opts->{"in"};}
MC/DC0%0/1
$opts->{"in"} and scalar @{$opts->{"in"};}
$opts->{"in"}
Truth table50%1/2
A
A= $opts->{"in"}
| |||||||||||||||||||||
| 26 | @values = map { | ||||||||||||||||||||
| 27 | 0 | my %h; | |||||||||||||||||||
| 28 | 0 | ▶ | @h{@{$opts->{on}}} = ref $_ ? @{$_} : ($_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 0 | \%h; | |||||||||||||||||||
| 30 | 0 | } @{$opts->{in}}; | |||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||
| 32 | 5 | my $v = $self->search($query, { columns => $opts->{on}, group_by => $opts->{on}, order_by => $opts->{on} }); | |||||||||||||||||||
| 33 | 5 | $v->result_class('DBIx::Class::ResultClass::HashRefInflator'); | |||||||||||||||||||
| 34 | @values = map { | ||||||||||||||||||||
| 35 | 5 | my %h; | |||||||||||||||||||
| 36 | 18 | @h{@{$opts->{on}}} = @{$_}{@{$opts->{on}}}; | |||||||||||||||||||
| 37 | 18 | \%h; | |||||||||||||||||||
| 38 | } $v->all; | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 5 | my @funcs = @{ $opts->{pivot}}; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | #dump @funcs; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 5 | my @cross_select = map { my $f = $_; map { $self->_ref_to_cross($f, $_) } @values } @funcs; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # dump @funcs; | ||||||||||||||||||||
| 48 | # dump @cross_select; | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 5 | my @cross_as = map { $self->_ref_to_as($_) } @cross_select; | |||||||||||||||||||
| 51 | # dump @cross_as; | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 5 | my $re = quotemeta($opts->{on}->[0]); | |||||||||||||||||||
| 54 | 5 | my (@as, @select); | |||||||||||||||||||
| 55 | 5 | ▶ | @select = map { ref $_ ? $self->_ref_to_literal($_) : $_ } grep { !/$re/ } @{$self->_resolved_attrs->{select}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 5 | ▶ | @as = map { ref $_ ? $self->_ref_to_as($_) : $_ } @select; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | my $cross = $self | ||||||||||||||||||||
| 59 | ->search($query, { | ||||||||||||||||||||
| 60 | 'select' => [ @select, @cross_select ], | ||||||||||||||||||||
| 61 | 'as' => [ @as, @cross_as ], | ||||||||||||||||||||
| 62 | group_by => $opts->{group_by} | ||||||||||||||||||||
| 63 | 5 | }); | |||||||||||||||||||
| 64 | 5 | my ($sql, @bind) = @{${$cross->as_query}}; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 5 | $sql =~ s/^\s*\((.*)\)\s*$/($1)/; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 5 | my $alias = \[ $sql , @bind ]; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 5 | return $self->result_source->resultset->search(undef, { | |||||||||||||||||||
| 71 | alias => $self->current_source_alias, | ||||||||||||||||||||
| 72 | from => [{ | ||||||||||||||||||||
| 73 | $self->current_source_alias => $alias, | ||||||||||||||||||||
| 74 | -alias => $self->current_source_alias, | ||||||||||||||||||||
| 75 | -source_handle => $self->result_source->handle, | ||||||||||||||||||||
| 76 | }], | ||||||||||||||||||||
| 77 | result_class => $self->result_class, | ||||||||||||||||||||
| 78 | 'as' => [ @as, @cross_as ], | ||||||||||||||||||||
| 79 | 'select' => [ @as, @cross_as ], | ||||||||||||||||||||
| 80 | }) | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 4 | ▶ | use String::SQLColumnName qw/fix_name/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | sub _ref_to_cross { | ||||||||||||||||||||
| 86 | # takes a function and a value as input and returns the select statement for the crosstab | ||||||||||||||||||||
| 87 | # _ref_to_cross('sum', 'broker') returns 'sum (CASE WHEN channel='broker' then units ELSE NULL END) as sum_units_broker' | ||||||||||||||||||||
| 88 | # need to document more complex cases | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 22 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_ref_to_cross: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 91 | 22 | my $function = shift; | |||||||||||||||||||
| 92 | 22 | my $value = shift; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 22 | my $when = join ' AND ', map { $_ . '=' . $quote->($value->{$_}) } keys %$value; | |||||||||||||||||||
| 95 | 22 | my $pivot = join '_', map { $value->{$_} } sort keys %$value; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 22 | for (ref $function) { | |||||||||||||||||||
| 98 | 22 | my $res; | |||||||||||||||||||
| 99 | # { function => $field } | ||||||||||||||||||||
| 100 | 22 | ▶ | /HASH/ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 14 | my ($func, $arg) = (%{$function}); | |||||||||||||||||||
| 102 | 14 | my $as = fix_name(join '_', $func, $arg, $pivot); | |||||||||||||||||||
| 103 | 14 | my $res = sprintf "%s (CASE WHEN %s then %s ELSE NULL END) as %s", $func, $when, $arg, $as; | |||||||||||||||||||
| 104 | 14 | return \$res; | |||||||||||||||||||
| 105 | }; | ||||||||||||||||||||
| 106 | 8 | ▶ | /SCALAR/ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 8 | my ($func, $distinct, $arg, $as) = parse_statement($$function); | |||||||||||||||||||
| 108 | 8 | $as = fix_name(join '_', $as, $pivot); | |||||||||||||||||||
| 109 | 8 | my $res = sprintf "%s ( %s (CASE WHEN %s then %s ELSE NULL END) ) as %s", $func, $distinct, $when, $arg, $as; | |||||||||||||||||||
| 110 | 8 | return \$res; | |||||||||||||||||||
| 111 | }; | ||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | sub parse_statement { | ||||||||||||||||||||
| 116 | 8 | ▶ | local $_ = shift; | ||||||||||||||||||
|
Subroutine
parse_statement: called CC=8 SCAR=22.8
Pod
undocumented
| |||||||||||||||||||||
| 117 | #------------------------------------------------- | ||||||||||||||||||||
| 118 | # this doesn't handle nested functions yet | ||||||||||||||||||||
| 119 | #------------------------------------------------- | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 8 | my ($distinct, $as); | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # get 'distinct' or 'all' in function | ||||||||||||||||||||
| 124 | 8 | ▶ | if (s/\((distinct)\s/\(/i) { $distinct = $1 } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 8 | ▶ | if (s/\((all)\s/\(/i) { $distinct = $1 } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 8 | ▶ | $distinct ||= 'all'; | ||||||||||||||||||
|
Condition50%1/2
$distinct ||= "all"
MC/DC0%0/1
$distinct ||= "all"
$distinct
Truth table50%1/2
A
A= $distinct
| |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # get 'as' part | ||||||||||||||||||||
| 129 | 8 | ▶ | if (s/\sas\s+(.+)//i) { $as = $1 } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | # get function (word followed by round bracket) | ||||||||||||||||||||
| 132 | # and arguments (the part between the first and last brackets) | ||||||||||||||||||||
| 133 | 8 | my ($func, $arg) = ($_ =~ /\s*(\w+)\s*\(\s*(.+?)\s*\)\s*$/i); | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # eliminate the me.something from as to make a good | ||||||||||||||||||||
| 136 | # string for the as part | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 8 | my $arg_as = $arg; | |||||||||||||||||||
| 139 | 8 | for ($arg_as) { s/\w+?\.//g; s/\W+/_/g; }; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 8 | ▶ | $as ||= join '_', $func, $distinct, $arg_as; | ||||||||||||||||||
|
Condition33%1/3
$as ||= join("_", $func, $distinct, $arg_as)
MC/DC0%0/2
$as ||= join("_", $func, $distinct, $arg_as)
$as
join("_", $func, $distinct, $arg_as)
Truth table33%1/3
A ||= B
A= $asB= join("_", $func, $distinct, $arg_as)
| |||||||||||||||||||||
| 142 | 8 | for ($as) { s/__+/_/g; s/_+$//; } | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 8 | return $func, $distinct, $arg, $as; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | sub _ref_to_literal { | ||||||||||||||||||||
| 148 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_ref_to_literal: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 149 | 0 | my $function = shift; | |||||||||||||||||||
| 150 | 0 | for (ref $function) { | |||||||||||||||||||
| 151 | 0 | ▶ | /HASH/ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 0 | my ($func, $field) = (%{$function}); | |||||||||||||||||||
| 153 | 0 | my $as = fix_name(join '_', $func, $field); | |||||||||||||||||||
| 154 | 0 | my $res = sprintf "%s (%s) as %s", $func, $field, $as; | |||||||||||||||||||
| 155 | 0 | return \$res; | |||||||||||||||||||
| 156 | }; | ||||||||||||||||||||
| 157 | 0 | ▶ | /SCALAR/ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 0 | my ($func, $distinct, $arg, $as) = parse_statement($$function); | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 0 | my $res = sprintf "%s ( %s %s ) as %s", $func, $distinct, $arg, $as; | |||||||||||||||||||
| 161 | 0 | return \$res; | |||||||||||||||||||
| 162 | }; | ||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||
| 164 | }; | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | sub _ref_to_as { | ||||||||||||||||||||
| 167 | 22 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_ref_to_as: called CC=4 SCAR=15.5
| |||||||||||||||||||||
| 168 | 22 | my $function = shift; | |||||||||||||||||||
| 169 | 22 | for (ref $function) { | |||||||||||||||||||
| 170 | 22 | ▶ | /HASH/ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 0 | my ($func, $field) = (%{$function}); | |||||||||||||||||||
| 172 | 0 | return join '_', $func, $field; | |||||||||||||||||||
| 173 | }; | ||||||||||||||||||||
| 174 | 22 | ▶ | /SCALAR/ && do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | 22 | my ($as) = ($$function =~ /\sas\s(.+)/); | |||||||||||||||||||
| 176 | 22 | return $as; | |||||||||||||||||||
| 177 | }; | ||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 4 | 1; | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 NAME | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | DBIx::Class::Helper::ResultSet::CrossTab | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head1 DESCRIPTION | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | ABSTRACT: helper to simulate crosstab | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head1 REQUIRES | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | L<String::SQLColumnName> | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | L<Data::Dump> | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | L<parent> | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head1 METHODS | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head2 crosstab | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | my $r = $s->search({}, { columns => [qw/fruit channel/], } ) | ||||||||||||||||||||
| 208 | ->crosstab({}, { | ||||||||||||||||||||
| 209 | select => [qw/fruit/], | ||||||||||||||||||||
| 210 | on => [qw/channel/], | ||||||||||||||||||||
| 211 | pivot => [ { sum => 'units' } ], | ||||||||||||||||||||
| 212 | group_by => [qw/fruit/] | ||||||||||||||||||||
| 213 | }); | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | $r->result_class('DBIx::Class::ResultClass::HashRefInflator'); # to inflate rows | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head2 ATTRIBUTES | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =head3 pivot | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 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. | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | these: | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | pivot => [ { sum => 'units' } ] # aggregate function and field | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | pivot => [ \"sum(units)" ] # literal SQL | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | will both provide a sum of units as the result of the crosstab, while this: | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | pivot => [ \"sum(units)", { avg => 'units }, \"count(distinct country)" ] # mixed, multiple fields | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | will generate a total of units, the average and the number of distinct countries | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =head3 on | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | This is the field (or function thereof) that will generate the column headers | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | on => [qw/channel/] | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | will generate columns by channel | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | on => [qw/channel country/] | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | will have one column per channel and country combination. It's not possible yet to pivot on a function of a field. | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =head3 others | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | All other attributes work as documented in DBIx::Class::ResultSet | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | =head2 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | Copyright 2016-2017 Simone Cesano | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | =head2 CREDITS | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | I owe the general idea to Giuseppe Maxia, and I re-used a couple of code snippets from Arthur Axel "fREW" Schmidt. | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||
| 262 | |||||||||||||||||||||