| 1 | package Sash::Table; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # Nice adapter and decorator pattern implementations here. | ||
| 6 | |||
| 7 | use Text::ASCIITable; | ||
| 8 | use base qw( Data::Table ); | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my $data = shift; | ||
| 14 | my $header = shift; | ||
| 15 | my $attributes = shift || $header; | ||
| 16 | |||
| 17 | # The zero as the last attribute indicates we want row based table and not | ||
| 18 | # a column based one. | ||
| 19 | my $self = $class->SUPER::new( $data, $header, 0 ); | ||
| 20 | |||
| 21 | |||
| 22 | bless $self, 'Sash::Table'; | ||
| 23 | |||
| 24 | $self->attributes( $attributes ); | ||
| 25 | |||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub attributes { | ||
| 30 | my $self = shift; | ||
| 31 | $self->{attributes} = ( shift || $self->{attributes} ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub clone { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | # This is one way to do it without introspecting on the parent | ||
| 38 | # and knowing anything about it's implementation details. | ||
| 39 | my $data = $self->rowRefs; | ||
| 40 | |||
| 41 | my $clone = $self->SUPER::new( $data, $self->{header}, 0 ); | ||
| 42 | |||
| 43 | $clone->attributes( $self->attributes ); | ||
| 44 | |||
| 45 | return $clone; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub sort { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | # Override the default behavior of doing an in place manipulation | ||
| 52 | # on the data to be consistent with the other table operations in | ||
| 53 | # the api. | ||
| 54 | my $clone = $self->clone; | ||
| 55 | |||
| 56 | # If you think $self is correct here than you really do not understand | ||
| 57 | # deep recursion in perl | ||
| 58 | my $sorted = $clone->SUPER::sort( @_ ); | ||
| 59 | |||
| 60 | croak 'Unable to sort data' unless $sorted; | ||
| 61 | |||
| 62 | return $clone; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub match_string { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | # Pay attention and be crafty here in that we want Sash::Table not Data::Table. | ||
| 69 | my $result = $self->SUPER::match_string( @_ ); | ||
| 70 | |||
| 71 | return bless $result, ( ref $self || $self ); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub display { | ||
| 75 | my $self = shift; | ||
| 76 | my $elapsed = shift; | ||
| 77 | |||
| 78 | my $table = Text::ASCIITable->new; | ||
| 79 | |||
| 80 | $table->setCols( $self->header ); | ||
| 81 | |||
| 82 | # This represents the total number of rows in the result set. | ||
| 83 | my $row_count = scalar( @{$self->rowRefs} ); | ||
| 84 | |||
| 85 | # This is the total number of attributes that make up a single | ||
| 86 | # row in the result set. | ||
| 87 | my $attribute_count = scalar( @{$self->attributes} ); | ||
| 88 | |||
| 89 | # We keep track of "vertical" rows because if you don't take | ||
| 90 | # into account attributes then the row count is off. | ||
| 91 | my $row_counter = 1; | ||
| 92 | my $vertical_row_counter = 1; | ||
| 93 | |||
| 94 | if ( Sash::Properties->output eq Sash::Properties->vertical ) { | ||
| 95 | # accounted | ||
| 96 | $row_count = $row_count / scalar( @{$self->attributes} ); | ||
| 97 | |||
| 98 | # Puts a pretty border around the row data. | ||
| 99 | $table->addRow( 'row', $vertical_row_counter ); | ||
| 100 | $table->addRowLine; | ||
| 101 | } | ||
| 102 | |||
| 103 | foreach ( @{$self->rowRefs} ) { | ||
| 104 | # Must have | ||
| 105 | $table->addRow( @$_ ); | ||
| 106 | |||
| 107 | # Make the vertical output correct and with its row counter | ||
| 108 | # line visible in a pretty border | ||
| 109 | if ( | ||
| 110 | ( $row_counter++ % $attribute_count == 0 ) && | ||
| 111 | ( $row_counter/$attribute_count <= $row_count ) && | ||
| 112 | ( Sash::Properties->output eq Sash::Properties->vertical ) | ||
| 113 | ) { | ||
| 114 | $vertical_row_counter++; | ||
| 115 | |||
| 116 | $table->addRowLine; | ||
| 117 | $table->addRow( 'row', $vertical_row_counter ); | ||
| 118 | $table->addRowLine; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | # Table layout to emulate mysql | ||
| 123 | print $table->draw( | ||
| 124 | ['+','+','-','+'], | ||
| 125 | ['|','|','|'], | ||
| 126 | ['+','+','-','+'], | ||
| 127 | ['|','|','|'], | ||
| 128 | ['+','+','-','+'], | ||
| 129 | ) . "\n"; | ||
| 130 | |||
| 131 | my $message = $row_count . ' rows in set '; | ||
| 132 | $message .= $elapsed if $elapsed; | ||
| 133 | |||
| 134 | print "$message\n"; | ||
| 135 | |||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; |