| 1 | package DBIx::dbMan::Extension::SQLOutputCube; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use Text::FormatTable; | ||
| 6 | |||
| 7 | our $VERSION = '0.04'; | ||
| 8 | |||
| 9 | 1; | ||
| 10 | |||
| 11 | sub IDENTIFICATION { return "000001-000082-000004"; } | ||
| 12 | |||
| 13 | sub preference { return 0; } | ||
| 14 | |||
| 15 | sub known_actions { return [ qw/SQL_OUTPUT/ ]; } | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | my $obj = shift; | ||
| 19 | $obj->{-mempool}->register('output_format','cube'); | ||
| 20 | } | ||
| 21 | |||
| 22 | sub done { | ||
| 23 | my $obj = shift; | ||
| 24 | $obj->{-mempool}->deregister('output_format','cube'); | ||
| 25 | if ($obj->{-mempool}->get('output_format') eq 'cube') { | ||
| 26 | my @all_formats = $obj->{-mempool}->get_register('output_format'); | ||
| 27 | $obj->{-mempool}->set('output_format', @all_formats ? $all_formats[0] : ''); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | sub handle_action { | ||
| 32 | my ($obj,%action) = @_; | ||
| 33 | |||
| 34 | $action{processed} = 1; | ||
| 35 | if ($action{action} eq 'SQL_OUTPUT') { # table is standard fallback | ||
| 36 | if ($obj->{-mempool}->get('output_format') eq 'cube') { | ||
| 37 | my %cube = (); | ||
| 38 | my %fields = (); | ||
| 39 | for (@{$action{result}}) { | ||
| 40 | my $x = ($_->[0] eq 'NULL') ? chr(255).'total' : $_->[0]; | ||
| 41 | my $y = ($_->[1] eq 'NULL') ? chr(255).'total' : $_->[1]; | ||
| 42 | my $value = $_->[2] || 0; | ||
| 43 | ++$fields{$x}; | ||
| 44 | $cube{$y}->{$x} = $value; | ||
| 45 | } | ||
| 46 | |||
| 47 | $action{result} = []; | ||
| 48 | for my $y (sort keys %cube) { | ||
| 49 | my @temporary = ($y eq chr(255).'total' ? 'Total' : $y); | ||
| 50 | for my $x (sort keys %fields) { | ||
| 51 | push @temporary,($cube{$y}->{$x} || 0); | ||
| 52 | } | ||
| 53 | push @{$action{result}},\@temporary; | ||
| 54 | } | ||
| 55 | $action{fieldnames} = [ 'y\x', map { $_ eq chr(255).'total' ? 'Total' : $_ } sort keys %fields ] ; | ||
| 56 | |||
| 57 | # $action{fieldtypes} - formatting ? | ||
| 58 | my $table = new Text::FormatTable ('|'.( 'l|' x scalar @{$action{fieldnames}} )); | ||
| 59 | $table->rule; | ||
| 60 | $table->head(@{$action{fieldnames}}); | ||
| 61 | $table->rule; | ||
| 62 | for (@{$action{result}}) { | ||
| 63 | $table->row(@$_); | ||
| 64 | } | ||
| 65 | $table->rule; | ||
| 66 | $action{action} = 'OUTPUT'; | ||
| 67 | $action{output} = $table->render($obj->{-interface}->render_size); | ||
| 68 | delete $action{processed}; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | return %action; | ||
| 73 | } |