LRpt/RKeysRdr.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 199 CC 19 SCAR 39.5
CC
19
Cov
0%
scan_key_from_cmd
110.0
initialize_keys
42.0
new
6.0
CRAP 52.1
1 ###################################################################
2 #
3 # $Id: RKeysRdr.pm,v 1.4 2006/09/17 19:20:40 pkaluski Exp $
4 # $Name: Stable_0_16 $
5 #
6 # This module reads definitions of row keys
7 #
8 # $Log: RKeysRdr.pm,v $
9 # Revision 1.4 2006/09/17 19:20:40 pkaluski
10 # Added handling of several ways of specifying keys
11 #
12 # Revision 1.3 2006/04/09 15:42:19 pkaluski
13 # Small code clean-up. Each module has comprehensive POD
14 #
15 # Revision 1.2 2006/01/14 12:52:33 pkaluski
16 # New tool design in progress
17 #
18 # Revision 1.1 2006/01/07 22:15:03 pkaluski
19 # Initial revision. Works.
20 #
21 #
22 #
23 ###################################################################
24 package LRpt::RKeysRdr;
25  
26 use LRpt::JarReader;
27 use strict;
28  
29 =head1 NAME
30  
31 LRpt::RKeysRdr - a module for reading definitions of row keys
32  
33 =head1 DESCRIPTION
34  
35 This class is a part of L<C<LRpt>|LRpt> library.
36 Object of this class provides an easy interface row keys definition
37 files.
38  
39 =cut
40  
41 our @rkeys_rules = ( { 'name' => 'select_name' },
42 { 'name' => 'key_columns' } );
43  
44 our $rkeys_data = "";
45
46  
47 =head1 METHODS
48  
49 In this sections you will find a more or less complete listing of all
50 methods provided by the package. Note that the package itself is not
51 public so none of those methods are guaranteed to be maintained in future
52 (including the package itself).
53  
54 =cut
55  
56 ############################################################################
57  
58 =head2 C<new>
59  
60 my $rkr = LRpt::RKeysRdr->new( fname => $fname,
61 string => $string );
62  
63 Constructor. Meaning of parameters:
64  
65 =over 4
66  
67 =item fname
68  
69 Name of a file containing definitions of row keys
70  
71 =item string
72  
73 Definition of a row keys given as one string. This feature is experimantal
74 and you better don't use it.
75  
76 =back
77  
78 =cut
79  
80 #############################################################################
81 sub new
82 {
83 my $proto = shift;
84 my %params = @_;
85
86 my $class = ref( $proto ) || $proto;
87 my $self = {};
88 bless( $self, $class );
89 if( !$rkeys_data ){
90 $self->initialize_keys( $params{ 'fname' },
91 $params{ 'key' },
92 $params{ 'key_cols' },
93 $params{ 'global_keys' } );
94 }
95 return $self;
96 }
97  
98  
99 ############################################################################
100  
101 =head2 C<initialize_keys>
102  
103 $rkr->initialize_keys( $fname, $string );
104  
105 Called by the contructor to perform initialization. Meaning of parameters:
106  
107 =over 4
108  
109 =item $fname
110  
111 Name of a file containing definitions of row keys
112  
113 =item $string
114  
115 Definition of a row keys given as one string. This feature is experimantal
116 and you better don't use it.
117  
118 =back
119  
120 =cut
121  
122 #############################################################################
123 sub initialize_keys
124 {
125 my $self = shift;
126 my $rk_fname = shift;
127 my $key_strings = shift;
128 my $key_col_strings = shift;
129 my $global_keys = shift;
130
131 $rkeys_data = {};
132 $self->{ 'default' }->{ 'columns' } = [ 0 ];
133 $self->{ 'default' }->{ 'name' } = 'key1';
134 if( @$key_strings ){
135 $self->scan_key_from_cmd( $key_strings );
136 }
137 if( @$key_col_strings ){
138 $self->scan_key_cols_from_cmd( $key_col_strings );
139 }
140  
141 if( $global_keys ){
142 my $config = LRpt::Config->new();
143 my $fname = $config->get_value( 'global_keys_file' );
144 if( -e $fname ){
145 $self->load_keys( $fname );
146 }
147 }
148 if( $rk_fname ){
149 $self->load_keys( $rk_fname );
150 }
151  
152 }
153  
154 ######################################################################
155  
156 =head2 C<scan_keys_from_cmd>
157  
158 $rkr->scan_keys_from_cmd( $string );
159  
160 Scans row key definition given in the command line. If nothing is given,
161 a default row key is assumed. Key parts are separated by commas.
162 The key will be used in all collections, for which a key in a file was
163 not defined.
164  
165 =cut
166  
167 ######################################################################
168 sub scan_key_from_cmd
169 {
170 my $self = shift;
171 my $key_strings = shift;
172
173 my $fmt_str = "";
174 my @col_idxs = ();
175 my @col_type = ();
176 foreach my $string ( @$key_strings ){
177 my( $start, $end ) = split( /,/, $string );
178 my $start_idx;
179 my $start_field_length;
180 my $end_idx;
181 my $end_field_length;
182  
183 if( $start =~ /^\d+$/ ) # Text field
184 {
185 $start_idx = $start;
186 }elsif( $start =~ /^\d+n\d+$/ ){
187 ( $start_idx, $start_field_length ) = ( $start =~ /^(\d+)n(\d+)$/ )[ 0, 1 ];
188 }else{
189 die "$string is not a right format of a key specification";
190 }
191
192 if( $end ){
193 if( $end =~ /^\d+$/ ) # Text field
194 {
195 $end_idx = $end;
196 }elsif( $end =~ /^\d+n\d+$/ ){
197 ( $end_idx, $end_field_length ) = ( $end =~ /^(\d+)n(\d+)$/ )[ 0, 1 ];
198 }else{
199 die "$string is not a right format of a key specification";
200 }
201 }else{
202 $end_idx = $start_idx;
203 }
204
205 my $cur_idx = @col_idxs;
206 if( $start_idx <= $end_idx ){
207 for( my $i = $start_idx - 1; $i < $end_idx; $i++ ){
208 push( @col_idxs, $i );
209 push( @col_type, "s" );
210 }
211 }else{
212 for( my $i = $start_idx - 1; $i >= ( $end_idx - 1 ); $i-- ){
213 push( @col_idxs, $i );
214 push( @col_type, "s" );
215 }
216 }
217 $col_type[ $cur_idx ] = $start_field_length if $start_field_length;
218 $col_type[ $#col_type ] = $end_field_length if $end_field_length;
219 }
220
221 for( my $i = 0; $i < @col_idxs; $i++ ){
222 if( $col_type[ $i ] eq "s" ){
223 $fmt_str = $fmt_str . "\%s#";
224 }else{
225 $fmt_str = $fmt_str . "%0" . $col_type[ $i ] . "d#";
226 }
227 }
228 $fmt_str =~ s/#$//;
229 $self->{ 'default' }->{ 'columns' } = \@col_idxs;
230 $self->{ 'default' }->{ 'function' } = $fmt_str;
231 }
232  
233 sub scan_key_cols_from_cmd
234 {
235 my $self = shift;
236 my $key_strings = shift;
237
238 my @cols = ();
239 my $fmt_str = "";
240 foreach my $string ( @$key_strings ){
241 my @keys_parts = split( /,/, $string );
242 foreach my $key ( @keys_parts ){
243 my @parts = split( /:/, $key );
244 push( @cols, $parts[ 0 ] );
245 if( $parts[ 1 ] ){
246 $fmt_str = $fmt_str . "%0$parts[ 1 ]" . "d#";
247 }else{
248 $fmt_str = $fmt_str . "%s#";
249 }
250 }
251 }
252 $fmt_str =~ s/#$//;
253 $self->{ 'default' }->{ 'columns' } = \@cols;
254 $self->{ 'default' }->{ 'function' } = $fmt_str;
255 }
256  
257 ######################################################################
258  
259 =head2 C<load_keys>
260  
261 $rkr->load_keys( $rk_fname );
262  
263 Loads row keys definitions from jar-record file.
264  
265 =cut
266  
267 ######################################################################
268 sub load_keys
269 {
270 my $self = shift;
271 my $fname = shift;
272 open( KEYS, "<$fname" ) or die "Cannot open '$fname' : $!";
273 my $jr = LRpt::JarReader->new(
274 'rules' => \@rkeys_rules, 'filehandle' => *KEYS );
275 $jr->read_all();
276 my @selects = $jr->get_all_values_of( 'select_name' );
277  
278 foreach my $select ( @selects ){
279 my $sect = $jr->get_section_with( 'select_name' => $select );
280 my @selects_in_entry = split( /\s*,\s*/, $select );
281 foreach my $s ( @selects_in_entry ){
282 $self->{ 'definitions' }->{ $s } =
283 $self->parse_rkey_str( $sect->{ 'key_columns' } );
284 }
285 }
286 close( KEYS ) or die "Cannot close $fname";
287
288 }
289  
290 ######################################################################
291  
292 =head2 C<parse_rkey_str>
293  
294 $rkr->parse_rkey_str( $keys_str );
295  
296 Parse a definition of one row key given in C<$keys_str>.
297  
298 =cut
299  
300 ######################################################################
301 sub parse_rkey_str
302 {
303 my $self = shift;
304 my $keys_str = shift;
305  
306 my %rkey = ();
307 $rkey{ 'name' } = 'key1';
308 my $fmt_str = "";
309 my @cols = ();
310  
311 my @keys_parts = split( /\s*,\s*/, $keys_str );
312 foreach my $key ( @keys_parts ){
313 my @parts = split( /:/, $key );
314 push( @cols, $parts[ 0 ] );
315 if( $parts[ 1 ] ){
316 $fmt_str = $fmt_str . "%0$parts[ 1 ]" . "d#";
317 }else{
318 $fmt_str = $fmt_str . "%s#";
319 }
320 }
321 $fmt_str =~ s/#$//;
322 $rkey{ 'columns' } = \@cols;
323 $rkey{ 'function' } = $fmt_str;
324 return \%rkey;
325 }
326  
327 ######################################################################
328  
329 =head2 find_key
330  
331 $key = $rkr->find_key( $name );
332  
333 Find a row key to be applied to a given file.
334  
335 =cut
336  
337 ######################################################################
338 sub find_key
339 {
340 my $self = shift;
341 my $name = shift;
342 if( not exists $self->{ 'definitions' }->{ $name } ){
343 return $self->{ 'default' };
344 }else{
345 return $self->{ 'definitions' }->{ $name };
346 }
347 }
348  
349 1;
350  
351 =head1 SEE ALSO
352  
353 The project is maintained on Source Forge L<http://lreport.sourceforge.net>.
354 You can find there links to some helpful documentation like tutorial.
355  
356 =head1 AUTHORS
357  
358 Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt>
359  
360 =head1 COPYRIGHT
361  
362 Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved.
363  
364 You may distribute under the terms of either the GNU General Public License
365 or the Artistic License, as specified in the Perl README file.
366  
367 =cut
368