blib/lib/DBIx/ReportBuilder/Search.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 196 CC 12 SCAR 22.1
CC
12
Cov
0%
Column
20.0
GroupByCols
20.0
_ApplyLimits
12.0
CRAP 9.1
1 # $File: //member/autrijus/DBIx-ReportBuilder/lib/DBIx/ReportBuilder/Search.pm $ $Author: autrijus $
2 # $Revision: #15 $ $Change: 8717 $ $DateTime: 2003/11/06 15:55:51 $
3  
4 package DBIx::ReportBuilder::Search;
5  
6 use strict;
7 use base 'DBIx::SearchBuilder';
8  
9 =head1 NAME
10  
11 DBIx::ReportBuilder::Search - Extended SearchBuilder
12  
13 =head1 SYNOPSIS
14  
15 my $sb = DBIx::ReportBuilder::Search->new(
16 Host => 'localhost',
17 Port => '3306',
18 User => 'root',
19 Driver => 'mysql',
20 Database => 'rt3',
21 );
22 $sb->SetTable('Queues');
23 $sb->GroupBy( FIELD => 'Disabled' );
24 my $col1 = $sb->Column( FIELD => 'Disabled' );
25 my $col2 = $sb->Column( FIELD => 'Id', FUNCTION => 'SUM' );
26 my $col3 = $sb->Column( FIELD => 'Id', FUNCTION => 'COUNT' );
27 $sb->UnLimit;
28 print join("\t", qw(Disabled SUM(id) COUNT(id))), "\n";
29 while (my $row = $sb->Next) {
30 print join(
31 "\t",
32 $row->{$col1}, $row->{$col2}, $row->{$col3}
33 ), "\n";
34 };
35  
36 =head1 DESCRIPTION
37  
38 This module implements a subclass of L<DBIx::SearchBuilder>.
39 The main difference is that it defines a default C<LoadFromHash>
40 and C<NewItem>, so is directly usable without needing to subclass
41 it first.
42  
43 It also implements C<GroupBy>, C<GroupByCols>, C<Column> and an
44 assortment of other utility functions.
45  
46 =cut
47  
48 if (!eval { require Encode; 1 }) {
49 *Encode::decode = sub { $_[0] };
50 }
51  
52 sub new {
53 my ($self, %args) = @_;
54 if (!$args{Handle}) {
55 require DBIx::SearchBuilder::Handle;
56 my $handle = DBIx::SearchBuilder::Handle->new;
57 $handle->Connect(%args, DisconnectHandleOnDestroy => 1);
58 %args = (Handle => $handle);
59 }
60 return $self->SUPER::new(%args);
61 }
62  
63 sub NewItem {
64 my $encoding = eval { $_[0]->_Handle->Encoding };
65 $encoding ||= 'big5'
66 if eval { $_[0]->_Handle->dbh->{Driver}{Name} } eq 'ODBC';
67 $encoding ||= 'utf8';
68 bless \$encoding, ref($_[0]);
69 }
70  
71 sub LoadFromHash {
72 my $encoding = ${$_[0]};
73 bless($_[0] = $_[1]);
74  
75 foreach my $key (sort keys %{$_[0]}) {
76 $_[0]{$key} = Encode::decode($encoding, $_[0]{$key});
77 $_[0]{$key} =~ s/&#([1-9]\d\d+);/chr($1)/eg; # anti-escapism
78 }
79 return $_[0];
80 }
81 sub Id { $_[0]->{id} }
82  
83 sub Column {
84 my ($self, %args) = @_;
85 my $table = $args{TABLE} || do {
86 if ( my $alias = $args{ALIAS} ) {
87 $alias =~ s/_\d+$//;
88 $alias;
89 }
90 else {
91 $self->{table};
92 }
93 };
94  
95 my $name = ($args{ALIAS} || 'main') . '.' . $args{FIELD};
96 if (my $func = $args{FUNCTION}) {
97 if ($func =~ /^DISTINCT\s*COUNT$/i) {
98 $name = "COUNT(DISTINCT $name)";
99 }
100 else {
101 $name = "\U$func\E($name)";
102 }
103 }
104  
105 my $column = "col" . @{$self->{columns}||=[]};
106 push @{$self->{columns}}, "$name AS $column";
107 return $column;
108 }
109  
110 sub Fields {
111 my ($self, $table) = @_;
112 my $dbh = $self->_Handle->dbh;
113  
114 return map lc($_->[0]), @{
115 eval { $dbh->column_info('', '', $table, '')->fetchall_arrayref([3]) }
116 || $dbh->selectall_arrayref("DESCRIBE $table;")
117 || $dbh->selectall_arrayref("DESCRIBE \u$table;")
118 || []
119 };
120 }
121  
122 sub HasField {
123 my ($self, %args) = @_;
124 my $table = $args{TABLE} or die;
125 my $field = $args{FIELD} or die;
126 return grep { $_ eq $field } $self->Fields($table);
127 }
128  
129 sub _ApplyLimits {
130 my ($self, $ref) = @_;
131 $self->SUPER::_ApplyLimits($ref);
132 $$ref =~ s/main\.\*/join(', ', @{$self->{columns}})/eg
133 if $self->{columns} and @{$self->{columns}};
134 if (my $groupby = $self->_GroupClause) {
135 $$ref =~ s/(LIMIT \d+)?$/$groupby $1/;
136 }
137 }
138  
139 sub SetTable {
140 my $self = shift;
141 $self->{table} = shift;
142 return $self->{table};
143 }
144  
145 sub Table { $_[0]->{table} }
146  
147 sub GroupBy {
148 my $self = shift;
149 my %args = ( @_ );
150 $self->GroupByCols( \%args );
151 }
152  
153 sub GroupByCols {
154 my $self = shift;
155 my @args = @_;
156 my $row;
157 my $clause;
158  
159 foreach $row ( @args ) {
160 my %rowhash = ( ALIAS => 'main',
161 FIELD => undef,
162 %$row
163 );
164  
165 if ( ($rowhash{'ALIAS'}) and
166 ($rowhash{'FIELD'}) ) {
167  
168 $clause .= ($clause ? ", " : " ");
169 $clause .= $rowhash{'ALIAS'} . ".";
170 $clause .= $rowhash{'FIELD'};
171 }
172 }
173  
174 if ($clause) {
175 $self->{'group_clause'} = "GROUP BY" . $clause;
176 }
177 else {
178 $self->{'group_clause'} = "";
179 }
180 $self->RedoSearch();
181 }
182  
183 sub _GroupClause {
184 my $self = shift;
185  
186 unless ( defined $self->{'group_clause'} ) {
187 return "";
188 }
189 return ($self->{'group_clause'});
190 }
191  
192 1;
193  
194 =head1 SEE ALSO
195  
196 L<DBIx::ReportBuilder>, L<DBIx::SearchBuilder>
197  
198 =head1 AUTHORS
199  
200 Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>
201  
202 =head1 COPYRIGHT
203  
204 Copyright 2003 by Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>.
205  
206 This program is free software; you can redistribute it and/or
207 modify it under the same terms as Perl itself.
208  
209 See L<http://www.perl.com/perl/misc/Artistic.html>
210  
211 =cut