| 1 | ##################################################################### | ||
| 2 | # | ||
| 3 | # $Id: Collection.pm,v 1.14 2006/09/17 19:25:31 pkaluski Exp $ | ||
| 4 | # $Name: Stable_0_16 $ | ||
| 5 | # | ||
| 6 | # This module is a container for results of one select. | ||
| 7 | # | ||
| 8 | # $Log: Collection.pm,v $ | ||
| 9 | # Revision 1.14 2006/09/17 19:25:31 pkaluski | ||
| 10 | # Many, many changes due to performance tuning. There is only key possible, Tie::File is not used any more, other changes | ||
| 11 | # | ||
| 12 | # Revision 1.13 2006/09/10 18:29:14 pkaluski | ||
| 13 | # Added chunking. The class does not connect to database any more. Removed unused code. | ||
| 14 | # | ||
| 15 | # Revision 1.12 2006/02/10 22:32:16 pkaluski | ||
| 16 | # Major redesign in progress. Updated POD. Works. | ||
| 17 | # | ||
| 18 | # Revision 1.11 2006/01/14 12:52:33 pkaluski | ||
| 19 | # New tool design in progress | ||
| 20 | # | ||
| 21 | # Revision 1.10 2006/01/07 21:32:24 pkaluski | ||
| 22 | # Adjusting to new design of LReport tool chain. | ||
| 23 | # | ||
| 24 | # Revision 1.9 2005/09/02 19:59:31 pkaluski | ||
| 25 | # Next refinement of PODs. Ready for distribution. Some work on clarity still to be done | ||
| 26 | # | ||
| 27 | # Revision 1.8 2005/09/01 20:00:18 pkaluski | ||
| 28 | # Refined PODs. Separation between public and private methods still to be done | ||
| 29 | # | ||
| 30 | # Revision 1.7 2005/01/24 21:22:51 pkaluski | ||
| 31 | # Added pod documentation | ||
| 32 | # | ||
| 33 | # Revision 1.6 2005/01/21 20:43:15 pkaluski | ||
| 34 | # Fixed bug 1105088 - Columns sorting does not work for unkeyed rows | ||
| 35 | # | ||
| 36 | # Revision 1.5 2004/12/21 21:21:29 pkaluski | ||
| 37 | # Columns ordering introduced for reporting rows different from expected | ||
| 38 | # | ||
| 39 | # Revision 1.4 2004/10/17 18:22:43 pkaluski | ||
| 40 | # Added test for unkeyed A-E comparison. The test pass. | ||
| 41 | # | ||
| 42 | # Revision 1.3 2004/10/17 08:30:32 pkaluski | ||
| 43 | # Added handling of unkeyed A-E comparison. Old tests still work. New tests to be added | ||
| 44 | # | ||
| 45 | # Revision 1.2 2004/10/15 21:51:10 pkaluski | ||
| 46 | # Added test case for logging when comparing with expecations. Fixed some bugs. | ||
| 47 | # | ||
| 48 | # Revision 1.1.1.1 2004/10/02 11:30:57 pkaluski | ||
| 49 | # Changed the naming convention. All packages start with LRpt | ||
| 50 | # | ||
| 51 | # | ||
| 52 | # | ||
| 53 | #################################################################### | ||
| 54 | package LRpt::Collection; | ||
| 55 | use LRpt::CollUnkeyed; | ||
| 56 | use LRpt::Config; | ||
| 57 | use Carp; | ||
| 58 | use strict; | ||
| 59 | use IO::File; | ||
| 60 | |||
| 61 | =head1 NAME | ||
| 62 | |||
| 63 | LRpt::Collection - A container for rows returned by select statement. | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | This class is a part of L<C<LRpt>|LRpt> (B<LReport>) library. | ||
| 68 | This module is a container for one I<csv> file. | ||
| 69 | Rows are stored in a form of hashes returned by DBI's C<fetch_arrayref( {} ) >. | ||
| 70 | Keys of a hash | ||
| 71 | are column names, values are column values. References to all rows are | ||
| 72 | kept in a list, in an order in which they are kept in I<csv> file. | ||
| 73 | Collection provides a notion of a row key. | ||
| 74 | You can use row keys for ordering rows and for quick searches of row with | ||
| 75 | a particular key value (like in databases). Collection may have several | ||
| 76 | keys defined. Each key has a name, columns, which compose a key and a | ||
| 77 | function, which basing on key fields' values, generates a string, which | ||
| 78 | can be used as a hash key. | ||
| 79 | |||
| 80 | =head1 METHODS | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub new_empty_copy | ||
| 85 | { | ||
| 86 | my $proto = shift; | ||
| 87 | my %params = @_; | ||
| 88 | |||
| 89 | if( ! $params{ 'src_coll' } ){ | ||
| 90 | die "No source collection specified"; | ||
| 91 | } | ||
| 92 | |||
| 93 | my $class = ref( $proto ) || $proto; | ||
| 94 | my $self = {}; | ||
| 95 | bless( $self, $class ); | ||
| 96 | |||
| 97 | $self->{ 'name' } = $params{ 'src_coll' }->{ 'name' }; | ||
| 98 | $self->{ 'columns' } = $params{ 'src_coll' }->{ 'columns' }; | ||
| 99 | $self->{ 'col_idxs' } = $params{ 'src_coll' }->{ 'col_idxs' }; | ||
| 100 | $self->{ 'key' } = $params{ 'src_coll' }->{ 'key' }; | ||
| 101 | $self->{ 'copied' } = 1; | ||
| 102 | my $config = LRpt::Config->new(); | ||
| 103 | my $chunk_size = $config->get_value( 'chunk_size' ); | ||
| 104 | if( not defined $params{ 'chunk_size' } ){ | ||
| 105 | $self->{ 'chunk_size' } = $chunk_size; | ||
| 106 | }else{ | ||
| 107 | $self->{ 'chunk_size' } = $params{ 'chunk_size' }; | ||
| 108 | } | ||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub new_from_csv | ||
| 113 | { | ||
| 114 | my $proto = shift; | ||
| 115 | my %params = @_; | ||
| 116 | |||
| 117 | die "Name of the statement not given" if not $params{ 'name' }; | ||
| 118 | die "Name of the data file not given" if not $params{ 'data_file' }; | ||
| 119 | |||
| 120 | my $class = ref( $proto ) || $proto; | ||
| 121 | my $self = {}; | ||
| 122 | bless( $self, $class ); | ||
| 123 | my $config = LRpt::Config->new(); | ||
| 124 | my $chunk_size = $config->get_value( 'chunk_size' ); | ||
| 125 | if( not defined $params{ 'chunk_size' } ){ | ||
| 126 | $self->{ 'chunk_size' } = $chunk_size; | ||
| 127 | }else{ | ||
| 128 | $self->{ 'chunk_size' } = $params{ 'chunk_size' }; | ||
| 129 | } | ||
| 130 | |||
| 131 | $self->{ 'name' } = $params{ 'name' }; | ||
| 132 | $self->{ 'data_file' } = $params{ 'data_file' }; | ||
| 133 | $self->upload_from_csv( $params{ 'data_file' } ); | ||
| 134 | |||
| 135 | if( $params{ 'key' } ){ | ||
| 136 | $self->init_keys_data( $params{ 'key' } ); | ||
| 137 | } | ||
| 138 | if( $self->{ 'key' } ){ | ||
| 139 | $self->build_keys(); | ||
| 140 | } | ||
| 141 | return $self; | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | ######################################################################### | ||
| 148 | |||
| 149 | =head2 C<init_keys_data> | ||
| 150 | |||
| 151 | $coll->init_keys_data( $keys_info ); | ||
| 152 | |||
| 153 | Initializes internal structure, storing information about keys. | ||
| 154 | The meaning of fields is as follows: | ||
| 155 | |||
| 156 | =over 4 | ||
| 157 | |||
| 158 | =item C<$self->{ keys }> | ||
| 159 | |||
| 160 | Hash. Key in a hash is a key name. Value is a reference | ||
| 161 | to a hash with the following key-value pairs: | ||
| 162 | |||
| 163 | C<columns> => reference to array of names of columns which are part of the key | ||
| 164 | |||
| 165 | C<function> => reference to a function, which creates a key value from the | ||
| 166 | columns' values. If not defined C<generate_key> is used. | ||
| 167 | |||
| 168 | After initialization, current key is set to the first specified key. | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | ########################################################################### | ||
| 175 | sub init_keys_data | ||
| 176 | { | ||
| 177 | my $self = shift; | ||
| 178 | my $inp_structs = shift; | ||
| 179 | |||
| 180 | $self->{ 'key' } = {}; | ||
| 181 | |||
| 182 | # | ||
| 183 | # We are generous, so we allow user to specify either column | ||
| 184 | # names, or their indexes (0 based) | ||
| 185 | # | ||
| 186 | |||
| 187 | my @col_names = (); | ||
| 188 | foreach my $col ( @{ $inp_structs->{ 'columns' } } ){ | ||
| 189 | if( $col =~ /^\d+$/ ){ | ||
| 190 | if( $col >= @{ $self->{ 'columns' } } ){ | ||
| 191 | warn "No column of index $col for " . | ||
| 192 | $self->{ 'data_file' }; | ||
| 193 | } | ||
| 194 | push( @col_names, $self->{ 'columns' }->[ $col ] ); | ||
| 195 | }else{ | ||
| 196 | if( not exists $self->{ 'col_idxs' }->{ $col } ){ | ||
| 197 | warn "No column $col " . $self->{ 'data_file' }; | ||
| 198 | } | ||
| 199 | push( @col_names, $col ); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | $self->{ 'key' } = | ||
| 203 | { | ||
| 204 | 'columns' => [ @col_names ], | ||
| 205 | 'function' => $inp_structs->{ 'function' } | ||
| 206 | }; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | #################### | ||
| 211 | # Loads next chunk of data | ||
| 212 | # | ||
| 213 | #################### | ||
| 214 | sub load_next_chunk | ||
| 215 | { | ||
| 216 | my $self = shift; | ||
| 217 | my $key = shift; | ||
| 218 | |||
| 219 | if( $self->{ 'chunk_size' } == 0 ){ | ||
| 220 | die "load_next_chunk should not be called when chunk_size is zero. " . | ||
| 221 | "If you got this message, there is a serious bug in the code."; | ||
| 222 | } | ||
| 223 | if( $self->{ 'file_handle' } ){ | ||
| 224 | my $fh = $self->{ 'file_handle' }; | ||
| 225 | my @rows = (); | ||
| 226 | my $i = 0; | ||
| 227 | my $cols = $self->{ 'columns' }; | ||
| 228 | my $last_row = $self->get_keyed_rows()->{ $key }; | ||
| 229 | while( <$fh> ){ | ||
| 230 | chomp; | ||
| 231 | my @values = split( /\t/, $_ ); | ||
| 232 | my %row = (); | ||
| 233 | @row{ @$cols } = @values; | ||
| 234 | push( @rows, \%row ); | ||
| 235 | if( $i == $self->{ 'chunk_size' } - 1 ) | ||
| 236 | { | ||
| 237 | last; | ||
| 238 | } | ||
| 239 | $i++; | ||
| 240 | } | ||
| 241 | if( $i == 0 ){ | ||
| 242 | $fh->close() or | ||
| 243 | die "Cannot close " . $self->{ 'data_file' } . ": $!"; | ||
| 244 | $self->{ 'file_handle' } = undef; | ||
| 245 | my $rows = $self->{ 'rows' }; | ||
| 246 | $self->{ 'rows' } = []; | ||
| 247 | $self->{ 'rows' }->[ 0 ] = $last_row; | ||
| 248 | $self->build_keys(); | ||
| 249 | return 1; # Nothing new loaded | ||
| 250 | } | ||
| 251 | unshift( @rows, $last_row ); | ||
| 252 | $self->{ 'rows' } = \@rows; | ||
| 253 | $self->build_keys(); | ||
| 254 | return 2; #New chunk loaded | ||
| 255 | } | ||
| 256 | else{ | ||
| 257 | return 1; | ||
| 258 | } | ||
| 259 | |||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | ######################################################################### | ||
| 264 | |||
| 265 | =head2 C<build_keys> | ||
| 266 | |||
| 267 | $coll->build_keys(); | ||
| 268 | |||
| 269 | Builds all data structures needed for efficient use of keys for rows | ||
| 270 | retrieval. | ||
| 271 | It generates a key value for each row. Then creates the following | ||
| 272 | structure: | ||
| 273 | |||
| 274 | $self->{ 'keyed_rows' } => Reference to a hash. Key of a hash is a name | ||
| 275 | of a key. Value is a reference to a hash in | ||
| 276 | which key is a value of row key, and hash | ||
| 277 | value is reference to a row. | ||
| 278 | |||
| 279 | Accesing a row with key = xxx would require to use the following reference: | ||
| 280 | |||
| 281 | $self->{ 'keyed_rows' }->{ 'rows' }->{ 'xxx' } | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | #################################################################### | ||
| 286 | sub build_keys | ||
| 287 | { | ||
| 288 | my $self = shift; | ||
| 289 | |||
| 290 | my $sel_key = $self->{ 'key' }; | ||
| 291 | $self->{ 'unkeyed_coll' } = LRpt::CollUnkeyed->new(); | ||
| 292 | $self->build_one_key( $sel_key ); | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | ##################################################################### | ||
| 297 | |||
| 298 | =head2 C<build_one_key> | ||
| 299 | |||
| 300 | $coll->build_one_key(); | ||
| 301 | |||
| 302 | Builds a structure described in C<build_keys> for one row key. | ||
| 303 | All rows, which do not have values for all columns from the key, are | ||
| 304 | moved to a list of 'unkeyed' rows. | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | #################################################################### | ||
| 309 | sub build_one_key | ||
| 310 | { | ||
| 311 | my $self = shift; | ||
| 312 | |||
| 313 | #print STDERR "Chunk size is " . $self->{ 'chunk_size' } . "\n"; | ||
| 314 | my $key = $self->{ 'key' }; | ||
| 315 | |||
| 316 | my $key_function = \&LRpt::Collection::generate_key; | ||
| 317 | if( $key->{ 'function' } ){ | ||
| 318 | if( !ref $key->{ 'function' } ){ | ||
| 319 | $key_function = \&LRpt::Collection::generate_fmt_key; | ||
| 320 | }else{ | ||
| 321 | $key_function = $key->{ 'function' }; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | my $unkeyed = 0; | ||
| 325 | $self->{ 'keyed_rows' }->{ 'rows' } = {}; | ||
| 326 | foreach my $row ( @{ $self->{ 'rows' } } ){ | ||
| 327 | if( $self->has_all_key_columns( $row ) ){ | ||
| 328 | my $key_value = &$key_function( $row, $key ); | ||
| 329 | $self->{ 'keyed_rows' }->{ 'rows' }->{ $key_value } = $row; | ||
| 330 | }else{ | ||
| 331 | $self->{ 'unkeyed_coll' }->add_row( $row ); | ||
| 332 | $unkeyed++; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | my @chunk_keys = | ||
| 336 | sort keys %{ $self->{ 'keyed_rows' }->{ 'rows' } }; | ||
| 337 | if( @chunk_keys ){ | ||
| 338 | $self->set_first_chunk_key( $chunk_keys[ 0 ] ); | ||
| 339 | $self->set_last_chunk_key( $chunk_keys[ $#chunk_keys ] ); | ||
| 340 | } | ||
| 341 | if( ( scalar( @{ $self->{ 'rows' } } ) - $unkeyed ) != | ||
| 342 | ( keys %{ $self->{ 'keyed_rows' }->{ 'rows' } } ) ) | ||
| 343 | { | ||
| 344 | if( exists $self->{ 'data_file' } ){ | ||
| 345 | warn "Key is not unique. Some rows from file " . | ||
| 346 | $self->{ 'data_file' } . " will be lost!!!"; | ||
| 347 | }else{ | ||
| 348 | die "Should not reach this place"; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | sub set_first_chunk_key | ||
| 354 | { | ||
| 355 | my $self = shift; | ||
| 356 | my $first_key = shift; | ||
| 357 | if( not exists $self->{ 'first_chunk_key' } ) | ||
| 358 | { | ||
| 359 | $self->{ 'first_chunk_key' } = $first_key; | ||
| 360 | }else{ | ||
| 361 | # | ||
| 362 | # If a key of the first row in a new chunk is lower then or equal | ||
| 363 | # to the current one... | ||
| 364 | # | ||
| 365 | if( $first_key lt | ||
| 366 | $self->{ 'last_chunk_key' } ) | ||
| 367 | { | ||
| 368 | die "Unsorted input for collection " . $self->{ 'data_file' } . | ||
| 369 | ". When input is chunked it must be sorted"; | ||
| 370 | }else{ | ||
| 371 | $self->{ 'first_chunk_key' } = $first_key; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | sub set_last_chunk_key | ||
| 377 | { | ||
| 378 | my $self = shift; | ||
| 379 | my $last_key = shift; | ||
| 380 | if( not exists $self->{ 'last_chunk_key' } ) | ||
| 381 | { | ||
| 382 | $self->{ 'last_chunk_key' } = $last_key; | ||
| 383 | }else{ | ||
| 384 | # | ||
| 385 | # If a key of the first row in a new chunk is lower then or equal | ||
| 386 | # to the current one... | ||
| 387 | # | ||
| 388 | if( $last_key lt | ||
| 389 | $self->{ 'last_chunk_key' } ) | ||
| 390 | { | ||
| 391 | die "Unsorted input for collection " . $self->{ 'data_file' } . | ||
| 392 | ". When input is chunked it must be sorted"; | ||
| 393 | }else{ | ||
| 394 | $self->{ 'last_chunk_key' } = $last_key; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | ##################################################################### | ||
| 401 | |||
| 402 | =head2 C<get_unkeyed_coll> | ||
| 403 | |||
| 404 | $unk_rows = $coll->get_unkeyed_coll(); | ||
| 405 | |||
| 406 | Returns a reference to a collection of 'unkeyed' rows | ||
| 407 | (L<C<LRpt::CollUnkeyed>|LRpt::CollUnkeyed>). | ||
| 408 | |||
| 409 | =cut | ||
| 410 | |||
| 411 | #################################################################### | ||
| 412 | sub get_unkeyed_coll | ||
| 413 | { | ||
| 414 | my $self = shift; | ||
| 415 | return $self->{ 'unkeyed_coll' }; | ||
| 416 | } | ||
| 417 | |||
| 418 | |||
| 419 | ######################################################################### | ||
| 420 | |||
| 421 | =head2 C<has_all_key_columns> | ||
| 422 | |||
| 423 | $coll->has_all_key_columns($row ); | ||
| 424 | |||
| 425 | Checks if a rows has values in columns, which are part of a key | ||
| 426 | |||
| 427 | =cut | ||
| 428 | |||
| 429 | #################################################################### | ||
| 430 | sub has_all_key_columns | ||
| 431 | { | ||
| 432 | my $self = shift; | ||
| 433 | my $row = shift; | ||
| 434 | |||
| 435 | my $key_cols = $self->{ 'key' }->{ 'columns' }; | ||
| 436 | foreach my $col ( @$key_cols ){ | ||
| 437 | if( not exists $row->{ $col } ){ | ||
| 438 | return 0; | ||
| 439 | } | ||
| 440 | } | ||
| 441 | return 1; | ||
| 442 | } | ||
| 443 | |||
| 444 | ######################################################################### | ||
| 445 | |||
| 446 | =head2 C<dump_rows> | ||
| 447 | |||
| 448 | $coll->dump_rows( $path ); | ||
| 449 | |||
| 450 | Dumps rows from the collections to the text file (tab separated) | ||
| 451 | |||
| 452 | =cut | ||
| 453 | |||
| 454 | #################################################################### | ||
| 455 | sub dump_rows | ||
| 456 | { | ||
| 457 | my $self = shift; | ||
| 458 | my $path = shift; | ||
| 459 | my $config = LRpt::Config->new(); | ||
| 460 | |||
| 461 | if( !$path ){ | ||
| 462 | $path = $config->get_value( 'path' ); | ||
| 463 | } | ||
| 464 | my $ext = $config->get_value( 'ext' ); | ||
| 465 | my $sep = $config->get_value( 'sep' ); | ||
| 466 | |||
| 467 | my $rows = $self->{ 'rows' }; | ||
| 468 | my $name = $self->{ 'name' }; | ||
| 469 | |||
| 470 | my $row = ""; | ||
| 471 | |||
| 472 | open( OUTFILE, ">$path/$name.$ext" ) or | ||
| 473 | die "Cannot open $path/$name.$ext : $!"; | ||
| 474 | # | ||
| 475 | # Print header (all column names) | ||
| 476 | # | ||
| 477 | print OUTFILE join( $sep, @{ $self->{ columns } } ) . "\n"; | ||
| 478 | |||
| 479 | foreach $row ( @{ $rows } ){ | ||
| 480 | # | ||
| 481 | # @ord_cols should contain all columns, in the same order in which | ||
| 482 | # they are in querried tables | ||
| 483 | # | ||
| 484 | |||
| 485 | my @ord_cols = @$row{ @{ $self->{ 'columns' } } }; | ||
| 486 | print OUTFILE "" . join( $sep, @ord_cols ) . "\n"; | ||
| 487 | } | ||
| 488 | close( OUTFILE ) or die "Cannot close $path/$name.txt : $!"; | ||
| 489 | } | ||
| 490 | |||
| 491 | |||
| 492 | ######################################################################### | ||
| 493 | |||
| 494 | =head2 C<upload_from_csv> | ||
| 495 | |||
| 496 | $coll->upload_from_csv( $path ); | ||
| 497 | |||
| 498 | Uploads rows from the text file (tab separated) | ||
| 499 | |||
| 500 | =cut | ||
| 501 | |||
| 502 | #################################################################### | ||
| 503 | sub upload_from_csv | ||
| 504 | { | ||
| 505 | my $self = shift; | ||
| 506 | my $path = shift; | ||
| 507 | |||
| 508 | my $fh = new IO::File; | ||
| 509 | $fh->open( "< $path" ) or die "Cannot open $path : $!"; | ||
| 510 | |||
| 511 | $self->{ 'file_handle' } = $fh; | ||
| 512 | my $header = <$fh>; | ||
| 513 | die "No header given in $path" if not $header; | ||
| 514 | chomp( $header ); | ||
| 515 | |||
| 516 | my @cols = split( /\t/, $header ); | ||
| 517 | $self->{ 'columns' } = \@cols; | ||
| 518 | my %col_idxs = (); | ||
| 519 | for( my $i = 0; $i < @cols; $i++ ){ | ||
| 520 | $col_idxs{ $cols[ $i ] } = $i; | ||
| 521 | } | ||
| 522 | $self->{ 'col_idxs' } = \%col_idxs; | ||
| 523 | |||
| 524 | my @rows = (); | ||
| 525 | my $i = 1; | ||
| 526 | while( <$fh> ) | ||
| 527 | { | ||
| 528 | chomp; | ||
| 529 | my @values = split( /\t/, $_ ); | ||
| 530 | my %row = (); | ||
| 531 | @row{ @cols } = @values; | ||
| 532 | push( @rows, \%row ); | ||
| 533 | if( $i == $self->{ 'chunk_size' } and $self->{ 'chunk_size' } ) | ||
| 534 | { | ||
| 535 | last; | ||
| 536 | } | ||
| 537 | $i++; | ||
| 538 | } | ||
| 539 | |||
| 540 | $self->{ 'rows' } = \@rows; | ||
| 541 | |||
| 542 | } | ||
| 543 | |||
| 544 | ######################################################################### | ||
| 545 | |||
| 546 | =head2 C<get_name> | ||
| 547 | |||
| 548 | $coll->get_name(); | ||
| 549 | |||
| 550 | Gets the name of the collection. | ||
| 551 | |||
| 552 | =cut | ||
| 553 | |||
| 554 | ######################################################################### | ||
| 555 | sub get_name | ||
| 556 | { | ||
| 557 | my $self = shift; | ||
| 558 | return $self->{ 'name' }; | ||
| 559 | } | ||
| 560 | |||
| 561 | |||
| 562 | ######################################################################### | ||
| 563 | |||
| 564 | =head2 C<get_columns> | ||
| 565 | |||
| 566 | $coll->get_columns(); | ||
| 567 | |||
| 568 | Returns all columns names returned by the select | ||
| 569 | |||
| 570 | =cut | ||
| 571 | |||
| 572 | ######################################################################### | ||
| 573 | sub get_columns | ||
| 574 | { | ||
| 575 | my $self = shift; | ||
| 576 | return $self->{ 'columns' }; | ||
| 577 | } | ||
| 578 | |||
| 579 | |||
| 580 | ######################################################################### | ||
| 581 | |||
| 582 | =head2 C<generate_key> | ||
| 583 | |||
| 584 | my $key_value = LRpt::Collection::generate_keys( $row, $key ); | ||
| 585 | |||
| 586 | Default function for generating row key values. | ||
| 587 | It simply joins all key columns using '#' as a separator | ||
| 588 | |||
| 589 | =cut | ||
| 590 | |||
| 591 | ######################################################################## | ||
| 592 | sub generate_key | ||
| 593 | { | ||
| 594 | my $row = shift; | ||
| 595 | my $key = shift; | ||
| 596 | |||
| 597 | my $cols = $key->{ 'columns' }; | ||
| 598 | foreach my $col ( @$cols ){ | ||
| 599 | if( not exists $row->{ $col } ){ | ||
| 600 | die "Invalid rkey definition. There is no such column '$col'"; | ||
| 601 | } | ||
| 602 | } | ||
| 603 | my @key_cols = @$row{ @$cols }; | ||
| 604 | return join( '#', @key_cols ); | ||
| 605 | } | ||
| 606 | |||
| 607 | |||
| 608 | ######################################################################### | ||
| 609 | |||
| 610 | =head2 C<get_key_value> | ||
| 611 | |||
| 612 | my $key_value = $coll->get_key_value( $row ); | ||
| 613 | |||
| 614 | Returns value of the current key for a given row. | ||
| 615 | |||
| 616 | =cut | ||
| 617 | |||
| 618 | ######################################################################### | ||
| 619 | sub get_key_value | ||
| 620 | { | ||
| 621 | my $self = shift; | ||
| 622 | my $row = shift; | ||
| 623 | |||
| 624 | return LRpt::Collection::generate_key( $row, $self->{ 'key' } ); | ||
| 625 | } | ||
| 626 | |||
| 627 | |||
| 628 | ######################################################################### | ||
| 629 | |||
| 630 | =head2 C<get_key_columns> | ||
| 631 | |||
| 632 | my @cols = $coll->get_key_columns(); | ||
| 633 | |||
| 634 | For a given key (or the current key, of no key name given) returns | ||
| 635 | all columns, which are part of the a key. | ||
| 636 | |||
| 637 | =cut | ||
| 638 | |||
| 639 | ######################################################################## | ||
| 640 | sub get_key_columns | ||
| 641 | { | ||
| 642 | my $self = shift; | ||
| 643 | return @{ $self->order_columns( $self->{ 'key' }->{ 'columns' } ) }; | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | ######################################################################### | ||
| 648 | |||
| 649 | =head2 C<order_columns> | ||
| 650 | |||
| 651 | my $cols = $coll->order_columns( $unord_cols ); | ||
| 652 | |||
| 653 | Orders and returns columns by their appearance in the table | ||
| 654 | |||
| 655 | =cut | ||
| 656 | |||
| 657 | ######################################################################### | ||
| 658 | sub order_columns | ||
| 659 | { | ||
| 660 | my $self = shift; | ||
| 661 | my $cols = shift; | ||
| 662 | |||
| 663 | my $idx_hash = $self->{ 'col_idxs' }; | ||
| 664 | my $columns = $self->{ 'columns' }; | ||
| 665 | |||
| 666 | my @col_idxs = @$idx_hash{ @$cols }; # Indexes of all columns given in | ||
| 667 | # $cols | ||
| 668 | @col_idxs = sort { $a <=> $b } @col_idxs; | ||
| 669 | my @ord_cols = @$columns[ @col_idxs ]; | ||
| 670 | # Columns from cols sorted by the | ||
| 671 | # order of appearance in a table | ||
| 672 | |||
| 673 | return \@ord_cols; | ||
| 674 | } | ||
| 675 | |||
| 676 | |||
| 677 | ######################################################################### | ||
| 678 | |||
| 679 | =head2 C<generate_fmt_key> | ||
| 680 | |||
| 681 | my $key_str = LRpt::Collection::generate_fmt_key( $row, $key ); | ||
| 682 | |||
| 683 | Generates a key string, which can be used as a key in a hash. | ||
| 684 | |||
| 685 | =cut | ||
| 686 | |||
| 687 | ######################################################################## | ||
| 688 | sub generate_fmt_key | ||
| 689 | { | ||
| 690 | my $row = shift; | ||
| 691 | my $key = shift; | ||
| 692 | |||
| 693 | my $cols = $key->{ 'columns' }; | ||
| 694 | foreach my $col ( @$cols ){ | ||
| 695 | if( not exists $row->{ $col } ){ | ||
| 696 | die "Invalid rkey definition. There is no such column '$col'"; | ||
| 697 | } | ||
| 698 | } | ||
| 699 | my @key_cols = @$row{ @$cols }; | ||
| 700 | return sprintf( $key->{ 'function' }, @key_cols ); | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | ######################################################################### | ||
| 705 | |||
| 706 | =head2 C<get_keyed_rows> | ||
| 707 | |||
| 708 | my $rows = $coll->get_keyed_rows(); | ||
| 709 | |||
| 710 | Returns a reference to a hash were keys are values of key and | ||
| 711 | values are reference to rows with those keys. | ||
| 712 | |||
| 713 | =cut | ||
| 714 | |||
| 715 | ######################################################################## | ||
| 716 | sub get_keyed_rows | ||
| 717 | { | ||
| 718 | my $self = shift; | ||
| 719 | return $self->{ 'keyed_rows' }->{ 'rows' }; | ||
| 720 | } | ||
| 721 | |||
| 722 | ######################################################################### | ||
| 723 | |||
| 724 | =head2 C<get_key_values> | ||
| 725 | |||
| 726 | my @values = $coll->get_key_values(); | ||
| 727 | |||
| 728 | Returns all values of a given key. | ||
| 729 | |||
| 730 | =cut | ||
| 731 | |||
| 732 | ######################################################################## | ||
| 733 | sub get_key_values | ||
| 734 | { | ||
| 735 | my $self = shift; | ||
| 736 | |||
| 737 | if( $self->no_rows() ){ | ||
| 738 | return (); | ||
| 739 | } | ||
| 740 | return keys %{ $self->get_keyed_rows() }; | ||
| 741 | } | ||
| 742 | |||
| 743 | |||
| 744 | ######################################################################### | ||
| 745 | |||
| 746 | =head2 C<get_row> | ||
| 747 | |||
| 748 | my $row = $coll->get_row( $key_value ) | ||
| 749 | |||
| 750 | Using a key value C<$key_value> for key retrieves a row. | ||
| 751 | |||
| 752 | =cut | ||
| 753 | |||
| 754 | ######################################################################## | ||
| 755 | sub get_row | ||
| 756 | { | ||
| 757 | my $self = shift; | ||
| 758 | my $key = shift; | ||
| 759 | |||
| 760 | if( $self->no_rows() ){ | ||
| 761 | return undef; | ||
| 762 | } | ||
| 763 | |||
| 764 | if( $self->{ 'chunk_size' } != 0 ){ | ||
| 765 | if( $key eq $self->{ 'last_chunk_key' } ){ | ||
| 766 | my $load = $self->load_next_chunk( $key ); | ||
| 767 | if( $load == 1 ){ | ||
| 768 | return $self->get_keyed_rows()->{ $key }; | ||
| 769 | }elsif( $load == 2 ){ | ||
| 770 | return ( $self->get_keyed_rows()->{ $key }, | ||
| 771 | $self->get_key_values() ); | ||
| 772 | } | ||
| 773 | } | ||
| 774 | } | ||
| 775 | return $self->get_keyed_rows()->{ $key }; | ||
| 776 | } | ||
| 777 | |||
| 778 | |||
| 779 | ######################################################################### | ||
| 780 | |||
| 781 | =head2 C<no_rows> | ||
| 782 | |||
| 783 | my $res = $coll->no_rows(); | ||
| 784 | |||
| 785 | Returns 1 if collection is empty | ||
| 786 | |||
| 787 | =cut | ||
| 788 | |||
| 789 | ######################################################################## | ||
| 790 | sub no_rows | ||
| 791 | { | ||
| 792 | my $self = shift; | ||
| 793 | if( @{ $self->{ 'rows' } } ){ | ||
| 794 | return 0; | ||
| 795 | }else{ | ||
| 796 | return 1; | ||
| 797 | } | ||
| 798 | } | ||
| 799 | |||
| 800 | ######################################################################### | ||
| 801 | |||
| 802 | =head2 C<adopt_rows> | ||
| 803 | |||
| 804 | $coll->adopt_rows( $rows ); | ||
| 805 | |||
| 806 | Appends a set of rows to the collection ( C<$rows> should be a reference to | ||
| 807 | and array of references to hashes, each representing one row ). | ||
| 808 | |||
| 809 | =cut | ||
| 810 | |||
| 811 | ############################################################################ | ||
| 812 | sub adopt_rows | ||
| 813 | { | ||
| 814 | my $self = shift; | ||
| 815 | my $rows = shift; | ||
| 816 | |||
| 817 | foreach my $row ( @$rows ){ | ||
| 818 | foreach my $col ( keys %$row ){ | ||
| 819 | if( not exists $self->{ 'col_idxs' }->{ $col } ){ | ||
| 820 | die "Adopted row has unknown column $col"; | ||
| 821 | } | ||
| 822 | } | ||
| 823 | } | ||
| 824 | |||
| 825 | if( !$self->{ 'rows' } ){ | ||
| 826 | $self->{ 'rows' } = [ @$rows ]; | ||
| 827 | }else{ | ||
| 828 | my $all_rows = [ @{ $self->{ 'rows' } }, @$rows ]; | ||
| 829 | $self->{ 'rows' } = $all_rows; | ||
| 830 | } | ||
| 831 | if( $self->{ 'key' } ){ | ||
| 832 | $self->build_keys(); | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | ######################################################################### | ||
| 837 | |||
| 838 | =head2 C<get_all_rows> | ||
| 839 | |||
| 840 | $coll->get_all_rows(); | ||
| 841 | |||
| 842 | Returns a reference to a list containing all rows in the order returned | ||
| 843 | by select. | ||
| 844 | |||
| 845 | =cut | ||
| 846 | |||
| 847 | ############################################################################ | ||
| 848 | sub get_all_rows | ||
| 849 | { | ||
| 850 | my $self = shift; | ||
| 851 | return $self->{ 'rows' }; | ||
| 852 | } | ||
| 853 | |||
| 854 | |||
| 855 | =head1 INTERNAL DATA STRUCTURE | ||
| 856 | |||
| 857 | Below you can find something, which is my pathetic attempt to create | ||
| 858 | a readable diagram. It is supposed to show a hierarchy of internal data | ||
| 859 | structure. I am open to suggestions how to make it look more readable | ||
| 860 | |||
| 861 | $self-> | ||
| 862 | | | ||
| 863 | |->{ name } | ||
| 864 | |->{ data_file } | ||
| 865 | |->{ key } | ||
| 866 | | |->{ columns } = [ $col1, $col2, ... ] | ||
| 867 | | |->{ function } = $fmt_string | ||
| 868 | |->{ columns } = $sth->{ 'NAME' } | ||
| 869 | |->{ col_idxs } = $sth->{ 'NAME_hash' } | ||
| 870 | |->{ rows } = [ $row1, $row2, ... , $rown ] | ||
| 871 | |->{ unkeyed_coll } = LRpt::CollUnkeyed object | ||
| 872 | |->{ keyed_rows } | ||
| 873 | | |->{ $key_value1 } = $self->{ rows }->[ ... ] | ||
| 874 | | |->{ $key_value2 } = $self->{ rows }->[ ... ] | ||
| 875 | | |->{ ........... } = $self->{ rows }->[ ... ] | ||
| 876 | | |->{ $key_valueN } = $self->{ rows }->[ ... ] | ||
| 877 | |->{ select_stmt } | ||
| 878 | | |->{ statement } --> obsolete | ||
| 879 | |||
| 880 | =over 4 | ||
| 881 | |||
| 882 | =item name | ||
| 883 | |||
| 884 | Name of a collection. Does not have a particular meaning for | ||
| 885 | Collection object itself, but is helpful for managing a group of | ||
| 886 | collections and is used by other LRpt modules | ||
| 887 | |||
| 888 | =item data_file | ||
| 889 | |||
| 890 | Name of a csv file used as a data source | ||
| 891 | |||
| 892 | =item key | ||
| 893 | |||
| 894 | A structure keeping an information about row key used by a | ||
| 895 | collection. | ||
| 896 | Key entry contains 'columns' which is a reference to list of | ||
| 897 | columns which are parts of a key (order matters), and a function, | ||
| 898 | which specifies some additional formatting, which should be | ||
| 899 | applied to key columns in order to get | ||
| 900 | a key value. Currently it's a formatting string used by sprintf to build | ||
| 901 | one string from all key columns. If function is not given, it is a simple | ||
| 902 | concatenation. | ||
| 903 | |||
| 904 | =item columns | ||
| 905 | |||
| 906 | equivalent of DBI's $sth->{ 'NAME' } - A list of columns in the order | ||
| 907 | from the row | ||
| 908 | |||
| 909 | =item col_idxs | ||
| 910 | |||
| 911 | equivalent of DBI's $sth->{ 'NAME_hash' } - A hash in which a | ||
| 912 | key is column name and value is an index of a column in a row. | ||
| 913 | |||
| 914 | =item rows | ||
| 915 | |||
| 916 | A reference to a list of all rows returned by | ||
| 917 | the select/uploaded from csv file | ||
| 918 | |||
| 919 | =item unkeyed_coll | ||
| 920 | |||
| 921 | collection of rows, for which not all columns being | ||
| 922 | part of a row key are defined. | ||
| 923 | |||
| 924 | =item keyed_rows | ||
| 925 | |||
| 926 | Additional structure used for performance when accessing single rows | ||
| 927 | using row keys. For each row key name it defines a hash. | ||
| 928 | Each key in this hash is a key value, hash value is a | ||
| 929 | reference to a row having this key value. | ||
| 930 | |||
| 931 | =back | ||
| 932 | |||
| 933 | =head1 SEE ALSO | ||
| 934 | |||
| 935 | The project is maintained on Source Forge L<http://lreport.sourceforge.net>. | ||
| 936 | You can find there links to some helpful documentation like tutorial. | ||
| 937 | |||
| 938 | =head1 AUTHORS | ||
| 939 | |||
| 940 | Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt> | ||
| 941 | |||
| 942 | =head1 COPYRIGHT | ||
| 943 | |||
| 944 | Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved. | ||
| 945 | |||
| 946 | You may distribute under the terms of either the GNU General Public License | ||
| 947 | or the Artistic License, as specified in the Perl README file. | ||
| 948 | |||
| 949 | =cut | ||
| 950 | |||
| 951 | |||
| 952 | 1; | ||
| 953 |