| 1 | ########################################################################## | ||
| 2 | # | ||
| 3 | # $Id: CollDiff.pm,v 1.13 2006/09/17 19:27:01 pkaluski Exp $ | ||
| 4 | # $Name: Stable_0_16 $ | ||
| 5 | # | ||
| 6 | # Object of this class compares 2 collections (LRpt::Collection objects). | ||
| 7 | # | ||
| 8 | # $Log: CollDiff.pm,v $ | ||
| 9 | # Revision 1.13 2006/09/17 19:27:01 pkaluski | ||
| 10 | # Performance tuning. Changes around compare_bef_aft_rows (which does nto exist any more). | ||
| 11 | # | ||
| 12 | # Revision 1.12 2006/09/10 18:23:01 pkaluski | ||
| 13 | # Added chunking. | ||
| 14 | # | ||
| 15 | # Revision 1.11 2006/02/10 22:32:16 pkaluski | ||
| 16 | # Major redesign in progress. Updated POD. Works. | ||
| 17 | # | ||
| 18 | # Revision 1.10 2006/01/21 20:39:47 pkaluski | ||
| 19 | # Improved lcsvdiff output format | ||
| 20 | # | ||
| 21 | # Revision 1.9 2006/01/14 12:52:33 pkaluski | ||
| 22 | # New tool design in progress | ||
| 23 | # | ||
| 24 | # Revision 1.8 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.7 2005/09/01 20:00:18 pkaluski | ||
| 28 | # Refined PODs. Separation between public and private methods still to be done | ||
| 29 | # | ||
| 30 | # Revision 1.6 2005/01/22 21:24:47 pkaluski | ||
| 31 | # Added pod documentation | ||
| 32 | # | ||
| 33 | # Revision 1.5 2004/12/21 21:21:22 pkaluski | ||
| 34 | # Columns ordering introduced for reporting rows different from expected | ||
| 35 | # | ||
| 36 | # Revision 1.4 2004/11/12 19:27:08 pkaluski | ||
| 37 | # Bunch of fixes of moderate problems | ||
| 38 | # | ||
| 39 | # Revision 1.3 2004/10/17 18:22:43 pkaluski | ||
| 40 | # Added test for unkeyed A-E comparison. The test pass. | ||
| 41 | # | ||
| 42 | # Revision 1.2 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.1.1.1 2004/10/02 11:30:56 pkaluski | ||
| 46 | # Changed the naming convention. All packages start with LRpt | ||
| 47 | # | ||
| 48 | ############################################################### | ||
| 49 | package LRpt::CollDiff; | ||
| 50 | use strict; | ||
| 51 | |||
| 52 | =head1 NAME | ||
| 53 | |||
| 54 | LRpt::CollDiff - A module for comparing 2 collections of rows | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | This class is a part of L<C<LRpt>|LRpt> library. | ||
| 59 | Object of this class compares 2 collections | ||
| 60 | (L<C<LRpt::Collection>|LRpt::Collection> objects). | ||
| 61 | Differences are stored inside the object and can be querried by object's | ||
| 62 | methods. They can also be printed on the standard output. | ||
| 63 | |||
| 64 | This manual page focus on implementation details. The intended audience | ||
| 65 | are developers who are willing to modify B<LReport> code or what to use | ||
| 66 | some of its modules in their code. If you are new to | ||
| 67 | B<LReport> please have a look at L<C<LRpt>|LRpt> manual page for | ||
| 68 | introduction. | ||
| 69 | |||
| 70 | =head2 GLOSSARY | ||
| 71 | |||
| 72 | The following terms are used in the code: | ||
| 73 | |||
| 74 | =over 4 | ||
| 75 | |||
| 76 | =item before collection | ||
| 77 | |||
| 78 | =item after collection | ||
| 79 | |||
| 80 | "before" refers to one of compared collections. Second collection is called | ||
| 81 | "after". The reason for such a naming instead of col1 and col2 is that the | ||
| 82 | code was written for comparing set of rows before the modification with | ||
| 83 | rows after the modification. Although the code will not break if you | ||
| 84 | give collections in the opposite order, it would be much easier for | ||
| 85 | you to work with this code if you follow this convention | ||
| 86 | |||
| 87 | =item missing/additional/not equal | ||
| 88 | |||
| 89 | Type of possible differences. If the row is "missing", it means that | ||
| 90 | there exists "before" row and there is no "after" row with the same key. | ||
| 91 | "additional" means that "after" row exists and respective "before" row | ||
| 92 | does not. "not equal" means that after and before rows exist but there | ||
| 93 | are some differences between them. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =head1 METHODS | ||
| 98 | |||
| 99 | In this sections you will find a more or less complete listing of all | ||
| 100 | methods provided by the package. Note that the package itself is not | ||
| 101 | public so none of those methods are guaranteed to be maintained in future | ||
| 102 | (including the package itself). | ||
| 103 | |||
| 104 | You may consider helpful having a look at the | ||
| 105 | L<INTERNAL DATA STRUCTURE|"INTERNAL DATA STRUCTURE"> of the | ||
| 106 | object. This section may help you to understand meaning of some methods. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | ############################################################################ | ||
| 111 | |||
| 112 | =head2 C<new> | ||
| 113 | |||
| 114 | my $cdiff = LRpt::CollDiff->new( 'before' => $b_coll, | ||
| 115 | 'after' => $a_coll ); | ||
| 116 | |||
| 117 | Constructor. Initializes fields used in later processing. The C<before> and | ||
| 118 | C<after> parameters should be references to collections to be compared | ||
| 119 | (L<C<LRpt::Collection>|LRpt::Collection> objects). | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | ########################################################################## | ||
| 124 | sub new | ||
| 125 | { | ||
| 126 | my $proto = shift; | ||
| 127 | my %params = @_; | ||
| 128 | |||
| 129 | my $class = ref( $proto ) || $proto; | ||
| 130 | |||
| 131 | my $self = {}; | ||
| 132 | bless( $self, $class ); | ||
| 133 | if( !$params{ 'before' } ){ | ||
| 134 | die "No before collection given"; | ||
| 135 | }else{ | ||
| 136 | $self->{ 'before' } = $params{ 'before' }; | ||
| 137 | } | ||
| 138 | if( !$params{ 'after' } ){ | ||
| 139 | die "No after collection given"; | ||
| 140 | }else{ | ||
| 141 | $self->{ 'after' } = $params{ 'after' }; | ||
| 142 | } | ||
| 143 | if( $params{ 'skip_cols' } ){ | ||
| 144 | $self->{ 'skip_cols' } = $params{ 'skip_cols' }; | ||
| 145 | } | ||
| 146 | return $self; | ||
| 147 | } | ||
| 148 | |||
| 149 | ###################################################################### | ||
| 150 | |||
| 151 | =head2 C<compare_collections> | ||
| 152 | |||
| 153 | $cdiff->compare_collections(); | ||
| 154 | |||
| 155 | The I<main> function. Compares collections given in a constructor. All | ||
| 156 | found differences are stored internally and may be retrieved by calling | ||
| 157 | other functions (TODO - list those functions). | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | ############################################################## | ||
| 162 | sub compare_collections | ||
| 163 | { | ||
| 164 | my $self = shift; | ||
| 165 | my $print_what = shift; | ||
| 166 | my $report_header = shift; | ||
| 167 | my $before_coll = $self->{ 'before' }; | ||
| 168 | my $after_coll = $self->{ 'after' }; | ||
| 169 | |||
| 170 | if( $report_header ){ | ||
| 171 | $self->{ 'report_header' } = $report_header; | ||
| 172 | } | ||
| 173 | |||
| 174 | if( $print_what ){ | ||
| 175 | $self->{ 'print_what' } = $print_what; | ||
| 176 | }else{ | ||
| 177 | $self->{ 'print_what' } = 'diffs'; | ||
| 178 | } | ||
| 179 | |||
| 180 | $self->compare_columns(); | ||
| 181 | # Find the sum of all keys | ||
| 182 | |||
| 183 | my @before_key_values = $before_coll->get_key_values(); | ||
| 184 | my @after_key_values = $after_coll->get_key_values(); | ||
| 185 | |||
| 186 | my %all_keys_hash = (); | ||
| 187 | @all_keys_hash{ @before_key_values } = 1; | ||
| 188 | @all_keys_hash{ @after_key_values } = 1; | ||
| 189 | |||
| 190 | my @all_keys = sort keys %all_keys_hash; | ||
| 191 | my $key = shift( @all_keys ); | ||
| 192 | while( defined $key ){ | ||
| 193 | my ( $after_row, @after_key_values ) = $self->get_after_row( $key ); | ||
| 194 | my ( $before_row, @before_key_values ) = $self->get_before_row( $key ); | ||
| 195 | |||
| 196 | # | ||
| 197 | # We expected a row and it does not exist/transaction removed | ||
| 198 | # the row | ||
| 199 | # | ||
| 200 | if( !$after_row ){ | ||
| 201 | $self->add_diff_missing( $key ); | ||
| 202 | if( $self->{ 'print_what' } ne 'nothing' ){ | ||
| 203 | $self->report_diff_missing( $key ); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | # | ||
| 207 | # We did not expect a row and it does exist/a transaction created | ||
| 208 | # a row | ||
| 209 | # | ||
| 210 | elsif( !$before_row ){ | ||
| 211 | $self->add_diff_additional( $key ); | ||
| 212 | if( $self->{ 'print_what' } ne 'nothing' ){ | ||
| 213 | $self->report_diff_additional( $key ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | # | ||
| 217 | # Expected row exists. Lets check if it's as expected/ | ||
| 218 | # what are differences. | ||
| 219 | # | ||
| 220 | else{ | ||
| 221 | $self->compare_rows( $key, $before_row, $after_row ); | ||
| 222 | } | ||
| 223 | |||
| 224 | if( @after_key_values or @before_key_values ){ | ||
| 225 | %all_keys_hash = (); | ||
| 226 | @all_keys_hash{ @before_key_values } = 1; | ||
| 227 | @all_keys_hash{ @after_key_values } = 1; | ||
| 228 | @all_keys_hash{ @all_keys } = 1; | ||
| 229 | @all_keys = sort keys %all_keys_hash; | ||
| 230 | if( $all_keys[ 0 ] eq $key ){ | ||
| 231 | shift( @all_keys ); # Skip the key currently used | ||
| 232 | } | ||
| 233 | } | ||
| 234 | $key = shift( @all_keys ); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | ######################################################################### | ||
| 239 | |||
| 240 | =head2 C<compare_columns> | ||
| 241 | |||
| 242 | $cdiff->compare_columns( $key_value ); | ||
| 243 | |||
| 244 | Checks if rows from 'before' and 'after' collections, having the same key | ||
| 245 | value, have the same columns. If this is not the case, a difference of | ||
| 246 | type 'diff_columns' is added. | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | ######################################################################### | ||
| 251 | sub compare_columns | ||
| 252 | { | ||
| 253 | my $self = shift; | ||
| 254 | |||
| 255 | my $before_cols_list = $self->{ 'before' }->get_columns(); | ||
| 256 | my $after_cols_list = $self->{ 'after' }->get_columns(); | ||
| 257 | |||
| 258 | my %before_cols = (); | ||
| 259 | my %after_cols = (); | ||
| 260 | my %all_cols = (); | ||
| 261 | @before_cols{ @$before_cols_list } = 1; | ||
| 262 | @after_cols{ @$after_cols_list } = 1; | ||
| 263 | @all_cols{ @$before_cols_list } = 1; | ||
| 264 | @all_cols{ @$after_cols_list } = 1; | ||
| 265 | |||
| 266 | $self->{ 'all_cols' } = [ keys %all_cols ]; | ||
| 267 | my %only_in_before = (); | ||
| 268 | my %only_in_after = (); | ||
| 269 | my %common = (); | ||
| 270 | |||
| 271 | foreach my $col ( keys %all_cols ){ | ||
| 272 | if( not exists $after_cols{ $col } ){ | ||
| 273 | $only_in_before{ $col } = 1; | ||
| 274 | }elsif( not exists $before_cols{ $col } ){ | ||
| 275 | $only_in_after{ $col } = 1; | ||
| 276 | }else{ | ||
| 277 | $common{ $col } = 1; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | my @missing = (); | ||
| 282 | my @additional = (); | ||
| 283 | my @equal = (); | ||
| 284 | foreach my $col ( @$before_cols_list ){ | ||
| 285 | push( @missing, $col ) if exists $only_in_before{ $col }; | ||
| 286 | push( @equal, $col ) if exists $common{ $col }; | ||
| 287 | } | ||
| 288 | foreach my $col ( @$after_cols_list ){ | ||
| 289 | push( @additional, $col ) if exists $only_in_after{ $col }; | ||
| 290 | } | ||
| 291 | my @cols_to_compare = (); | ||
| 292 | foreach my $col ( @equal ){ | ||
| 293 | if( not exists $self->{ 'skip_cols' }->{ $col } ){ | ||
| 294 | push( @cols_to_compare, $col ); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | $self->{ 'cols_to_compare' } = \@cols_to_compare; | ||
| 298 | if( @equal != @$before_cols_list or @equal != @$after_cols_list ){ | ||
| 299 | $self->print_line_of_text( "SCHEMA_DIFF_BEFORE: " . | ||
| 300 | join( "\t", @$before_cols_list ) ); | ||
| 301 | $self->print_line_of_text( "SCHEMA_DIFF_COMMON: " . | ||
| 302 | join( "\t", @equal ) ); | ||
| 303 | $self->print_line_of_text( "SCHEMA_DIFF_MISSING: " . | ||
| 304 | join( "\t", @missing ) ); | ||
| 305 | $self->print_line_of_text( "SCHEMA_DIFF_ADDITIONAL: " . | ||
| 306 | join( "\t", @additional ) ); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | ######################################################################### | ||
| 312 | |||
| 313 | =head2 C<compare_rows> | ||
| 314 | |||
| 315 | $cdiff->compare_rows( $key_name ); | ||
| 316 | |||
| 317 | Compares 'before' and 'after' rows having the same key value and report | ||
| 318 | differences between them. Finds and stores differences of 'diff_not_equal' | ||
| 319 | type. | ||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | ######################################################################### | ||
| 324 | sub compare_rows | ||
| 325 | { | ||
| 326 | my ( $self, $key, $before_row, $after_row ) = @_; | ||
| 327 | |||
| 328 | my $diff_found = 0; | ||
| 329 | foreach my $col ( @{ $self->{ 'cols_to_compare' } } ){ | ||
| 330 | if( $before_row->{ $col } ne $after_row->{ $col } ){ | ||
| 331 | $self->add_diff_not_equal( $key, | ||
| 332 | 'column' => $col, | ||
| 333 | 'before' => $before_row->{ $col }, | ||
| 334 | 'after' => $after_row->{ $col } ); | ||
| 335 | $diff_found = 1; | ||
| 336 | } | ||
| 337 | } | ||
| 338 | if( $self->{ 'print_what' } eq 'nothing' ) { | ||
| 339 | return; | ||
| 340 | } | ||
| 341 | if( $diff_found or $self->{ 'print_what' } eq "all" ){ | ||
| 342 | $self->report_diffs_not_equal( $key, $before_row, $after_row ); | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | ######################################################################### | ||
| 347 | |||
| 348 | =head2 C<add_diff_missing> | ||
| 349 | |||
| 350 | $cdiff->add_diff_missing( $key_value ); | ||
| 351 | |||
| 352 | Stores information that a row with the key C<$key_value> is 'missing'. | ||
| 353 | |||
| 354 | =cut | ||
| 355 | |||
| 356 | ######################################################################### | ||
| 357 | sub add_diff_missing | ||
| 358 | { | ||
| 359 | my $self = shift; | ||
| 360 | my $key = shift; | ||
| 361 | my %params = @_; | ||
| 362 | |||
| 363 | $self->{ 'differences' }->{ $key } = | ||
| 364 | { 'diff_type' => "missing", | ||
| 365 | 'row' => $self->get_before_row( $key ) }; | ||
| 366 | } | ||
| 367 | |||
| 368 | ######################################################################### | ||
| 369 | |||
| 370 | =head2 C<report_diff_missing> | ||
| 371 | |||
| 372 | $cdiff->report_diff_missing( $key_value ); | ||
| 373 | |||
| 374 | Stores information that a row with the key C<$key_value> is 'missing'. | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | ######################################################################### | ||
| 379 | sub report_diff_missing | ||
| 380 | { | ||
| 381 | my $self = shift; | ||
| 382 | my $key = shift; | ||
| 383 | my %params = @_; | ||
| 384 | |||
| 385 | my $line = ""; | ||
| 386 | if( $self->print_all() ){ | ||
| 387 | my $row = $self->get_before_row( $key ); | ||
| 388 | my $cols = $self->{ 'before' }->get_columns(); | ||
| 389 | $line = "DEL( $key ): " . join( "\t", @$row{ @$cols } ); | ||
| 390 | }else{ | ||
| 391 | $line = "DEL( $key )"; | ||
| 392 | } | ||
| 393 | $self->print_line_of_text( $line ); | ||
| 394 | } | ||
| 395 | |||
| 396 | ######################################################################### | ||
| 397 | |||
| 398 | =head2 C<print_all> | ||
| 399 | |||
| 400 | $cdiff->print_all(); | ||
| 401 | |||
| 402 | Returns 1 if the full report should be printed (differences and all rows). | ||
| 403 | |||
| 404 | =cut | ||
| 405 | |||
| 406 | ########################################################################## | ||
| 407 | sub print_all | ||
| 408 | { | ||
| 409 | my $self = shift; | ||
| 410 | if( $self->{ 'print_what' } eq "all" ){ | ||
| 411 | return 1; | ||
| 412 | }else{ | ||
| 413 | return 0; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | ######################################################################### | ||
| 418 | |||
| 419 | =head2 C<add_diff_additional> | ||
| 420 | |||
| 421 | $cdiff->add_diff_additional( $key_value ); | ||
| 422 | |||
| 423 | Stores information that a row with the key C<$key_value> is 'additional'. | ||
| 424 | |||
| 425 | =cut | ||
| 426 | |||
| 427 | ######################################################################### | ||
| 428 | sub add_diff_additional | ||
| 429 | { | ||
| 430 | my $self = shift; | ||
| 431 | my $key = shift; | ||
| 432 | my %params = @_; | ||
| 433 | |||
| 434 | $self->{ 'differences' }->{ $key } = | ||
| 435 | { 'diff_type' => "additional", | ||
| 436 | 'row' => $self->get_after_row( $key ) }; | ||
| 437 | } | ||
| 438 | |||
| 439 | ######################################################################### | ||
| 440 | |||
| 441 | =head2 C<report_diff_additional> | ||
| 442 | |||
| 443 | $cdiff->report_diff_additional( $key_value ); | ||
| 444 | |||
| 445 | Stores information that a row with the key C<$key_value> is 'additional'. | ||
| 446 | |||
| 447 | =cut | ||
| 448 | |||
| 449 | ######################################################################### | ||
| 450 | sub report_diff_additional | ||
| 451 | { | ||
| 452 | my $self = shift; | ||
| 453 | my $key = shift; | ||
| 454 | my %params = @_; | ||
| 455 | |||
| 456 | my $line = ""; | ||
| 457 | if( $self->print_all() ){ | ||
| 458 | my $row = $self->get_after_row( $key ); | ||
| 459 | my $cols = $self->{ 'after' }->get_columns(); | ||
| 460 | $line = "INS( $key ): " . join( "\t", @$row{ @$cols } ); | ||
| 461 | }else{ | ||
| 462 | $line = "INS( $key )"; | ||
| 463 | } | ||
| 464 | $self->print_line_of_text( $line ); | ||
| 465 | } | ||
| 466 | |||
| 467 | ######################################################################### | ||
| 468 | |||
| 469 | =head2 C<add_diff_not_equal> | ||
| 470 | |||
| 471 | $cdiff->add_diff_not_equal( $key_value, | ||
| 472 | 'column' => $column_name, | ||
| 473 | 'before' => $before_value, | ||
| 474 | 'after' => $after_value ); | ||
| 475 | |||
| 476 | Stores information that a row with the key C<$key_value> is 'not_equal'. | ||
| 477 | |||
| 478 | =cut | ||
| 479 | |||
| 480 | ######################################################################### | ||
| 481 | sub add_diff_not_equal | ||
| 482 | { | ||
| 483 | my $self = shift; | ||
| 484 | my $key = shift; | ||
| 485 | my %params = @_; | ||
| 486 | |||
| 487 | $self->{ 'differences' }->{ $key }->{ 'diff_type' } = "not_equal"; | ||
| 488 | $self->set_before_col_val( $key, | ||
| 489 | $params{ 'column' }, | ||
| 490 | $params{ 'before' } ); | ||
| 491 | $self->set_after_col_val( $key, | ||
| 492 | $params{ 'column' }, | ||
| 493 | $params{ 'after' } ); | ||
| 494 | } | ||
| 495 | |||
| 496 | ######################################################################### | ||
| 497 | |||
| 498 | =head2 C<report_diff_not_equal> | ||
| 499 | |||
| 500 | $cdiff->report_diff_not_equal( $key_value, | ||
| 501 | 'column' => $column_name, | ||
| 502 | 'before' => $before_value, | ||
| 503 | 'after' => $after_value ); | ||
| 504 | |||
| 505 | Stores information that a row with the key C<$key_value> is 'not_equal'. | ||
| 506 | |||
| 507 | =cut | ||
| 508 | |||
| 509 | ######################################################################### | ||
| 510 | sub report_diffs_not_equal | ||
| 511 | { | ||
| 512 | my ( $self, $key, $before_row, $after_row ) = @_; | ||
| 513 | |||
| 514 | my $cols = $self->{ 'after' }->get_columns(); | ||
| 515 | my %diff_cols = (); | ||
| 516 | @diff_cols{ $self->get_diff_columns( $key ) } = 1; | ||
| 517 | my $line = ""; | ||
| 518 | if( $self->print_all() ){ | ||
| 519 | $line = "ROW( $key ): " . join( "\t", @$after_row{ @$cols } ); | ||
| 520 | $self->print_line_of_text( $line ); | ||
| 521 | } | ||
| 522 | foreach my $col ( @$cols ){ | ||
| 523 | if( exists $diff_cols{ $col } ){ | ||
| 524 | $line = "UPD( $key ): " . $col . ": " . | ||
| 525 | $before_row->{ $col } . " ==#> " . | ||
| 526 | $after_row->{ $col }; | ||
| 527 | $self->print_line_of_text( $line ); | ||
| 528 | } | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | ######################################################################### | ||
| 533 | |||
| 534 | =head2 C<add_diff_columns> | ||
| 535 | |||
| 536 | $cdiff->add_diff_columns( $key_value, | ||
| 537 | 'column' => $column_name, | ||
| 538 | 'before' => $before_value, | ||
| 539 | 'after' => $after_value ); | ||
| 540 | |||
| 541 | Stores information that compared rows do not have the same sets of columns. | ||
| 542 | |||
| 543 | =cut | ||
| 544 | |||
| 545 | ######################################################################### | ||
| 546 | sub add_diff_columns | ||
| 547 | { | ||
| 548 | my $self = shift; | ||
| 549 | my $key = shift; | ||
| 550 | my %params = @_; | ||
| 551 | |||
| 552 | $self->{ 'differences' }->{ $key } = | ||
| 553 | { 'diff_type' => "different_columns", | ||
| 554 | 'only_in_after' => $params{ 'only_in_after' }, | ||
| 555 | 'only_in_before'=> $params{ 'only_in_before' } }; | ||
| 556 | } | ||
| 557 | |||
| 558 | |||
| 559 | ######################################################################### | ||
| 560 | |||
| 561 | =head2 C<set_before_col_val> | ||
| 562 | |||
| 563 | $cdiff->set_before_col_val( $key_value, | ||
| 564 | $column_name, | ||
| 565 | $value ); | ||
| 566 | |||
| 567 | Stores information about value in 'before' row, which is different from value | ||
| 568 | in 'after' row. | ||
| 569 | |||
| 570 | =cut | ||
| 571 | |||
| 572 | ######################################################################### | ||
| 573 | sub set_before_col_val | ||
| 574 | { | ||
| 575 | my $self = shift; | ||
| 576 | my ( $key, $col, $value ) = @_; | ||
| 577 | $self->{ 'differences' }->{ $key }->{ 'diffs' }->{ $col }->{ 'before' } = | ||
| 578 | $value; | ||
| 579 | $self->{ 'differences' }->{ $key }->{ 'before_row' } = | ||
| 580 | $self->get_before_row( $key ); | ||
| 581 | } | ||
| 582 | |||
| 583 | |||
| 584 | ######################################################################### | ||
| 585 | |||
| 586 | =head2 C<set_after_col_val> | ||
| 587 | |||
| 588 | $cdiff->set_after_col_val( $key_value, | ||
| 589 | $column_name, | ||
| 590 | $value ); | ||
| 591 | |||
| 592 | Stores information about value in 'after' row, which is different from value | ||
| 593 | in 'before' row. | ||
| 594 | |||
| 595 | =cut | ||
| 596 | |||
| 597 | ######################################################################### | ||
| 598 | sub set_after_col_val | ||
| 599 | { | ||
| 600 | my $self = shift; | ||
| 601 | my ( $key, $col, $value ) = @_; | ||
| 602 | $self->{ 'differences' }->{ $key }->{ 'diffs' }->{ $col }->{ 'after' } = | ||
| 603 | $value; | ||
| 604 | $self->{ 'differences' }->{ $key }->{ 'after_row' } = | ||
| 605 | $self->get_after_row( $key ); | ||
| 606 | } | ||
| 607 | |||
| 608 | |||
| 609 | ######################################################################### | ||
| 610 | |||
| 611 | =head2 C<get_before_row> | ||
| 612 | |||
| 613 | $cdiff->get_before_row( $key_value ); | ||
| 614 | |||
| 615 | Gets before row with the given key value. | ||
| 616 | |||
| 617 | =cut | ||
| 618 | |||
| 619 | ######################################################################### | ||
| 620 | sub get_before_row | ||
| 621 | { | ||
| 622 | my $self = shift; | ||
| 623 | my $key = shift; | ||
| 624 | return $self->{ 'before' }->get_row( $key ); | ||
| 625 | } | ||
| 626 | |||
| 627 | |||
| 628 | ######################################################################### | ||
| 629 | |||
| 630 | =head2 C<get_after_row> | ||
| 631 | |||
| 632 | $cdiff->get_after_row( $key_value ); | ||
| 633 | |||
| 634 | Gets after row with the given key value. | ||
| 635 | |||
| 636 | =cut | ||
| 637 | |||
| 638 | ######################################################################### | ||
| 639 | sub get_after_row | ||
| 640 | { | ||
| 641 | my $self = shift; | ||
| 642 | my $key = shift; | ||
| 643 | return $self->{ 'after' }->get_row( $key ); | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | ######################################################################### | ||
| 648 | |||
| 649 | =head2 C<get_diff_type> | ||
| 650 | |||
| 651 | $cdiff->get_diff_type( $key_value ); | ||
| 652 | |||
| 653 | Gets type of the difference between 'before' and 'after' rows of a | ||
| 654 | given key value. | ||
| 655 | |||
| 656 | =cut | ||
| 657 | |||
| 658 | ######################################################################### | ||
| 659 | sub get_diff_type | ||
| 660 | { | ||
| 661 | my $self = shift; | ||
| 662 | my $key = shift; | ||
| 663 | return $self->{ 'differences' }->{ $key }->{ 'diff_type' }; | ||
| 664 | } | ||
| 665 | |||
| 666 | |||
| 667 | ######################################################################### | ||
| 668 | |||
| 669 | =head2 C<get_diff_columns> | ||
| 670 | |||
| 671 | $cdiff->get_diff_columns( $key_value ); | ||
| 672 | |||
| 673 | Gets all columns, which values are different for 'after' and 'before' rows | ||
| 674 | of a given key value. Columns are ordered by their order in the database. | ||
| 675 | |||
| 676 | =cut | ||
| 677 | |||
| 678 | ######################################################################### | ||
| 679 | sub get_diff_columns | ||
| 680 | { | ||
| 681 | my $self = shift; | ||
| 682 | my $key = shift; | ||
| 683 | if( not exists $self->{ 'differences' }->{ $key } ){ | ||
| 684 | return (); | ||
| 685 | }else{ | ||
| 686 | my @diff_cols = | ||
| 687 | keys %{ $self->{ 'differences' }->{ $key }->{ 'diffs' } }; | ||
| 688 | return @{ $self->{ 'after' }->order_columns( \@diff_cols ) }; | ||
| 689 | } | ||
| 690 | } | ||
| 691 | |||
| 692 | |||
| 693 | ######################################################################### | ||
| 694 | |||
| 695 | =head2 C<get_key_columns> | ||
| 696 | |||
| 697 | $cdiff->get_key_columns(); | ||
| 698 | |||
| 699 | Get names of columns, which are a part of a key. | ||
| 700 | |||
| 701 | =cut | ||
| 702 | |||
| 703 | ######################################################################### | ||
| 704 | sub get_key_columns | ||
| 705 | { | ||
| 706 | my $self = shift; | ||
| 707 | return $self->{ 'after' }->get_key_columns(); | ||
| 708 | } | ||
| 709 | |||
| 710 | |||
| 711 | ######################################################################### | ||
| 712 | |||
| 713 | =head2 C<has_any_diff> | ||
| 714 | |||
| 715 | my $result = $cdiff->has_any_diff(); | ||
| 716 | |||
| 717 | Returns 1 if there are any differences between compared collections | ||
| 718 | (L<C<LRpt::Collection>|LRpt::Collection>). | ||
| 719 | 0 otherwise. | ||
| 720 | |||
| 721 | =cut | ||
| 722 | |||
| 723 | ######################################################################### | ||
| 724 | sub has_any_diff | ||
| 725 | { | ||
| 726 | my $self = shift; | ||
| 727 | |||
| 728 | if( !exists $self->{ 'differences' } ){ | ||
| 729 | return 0; | ||
| 730 | } | ||
| 731 | if( keys %{ $self->{ 'differences' } } ){ | ||
| 732 | return 1; | ||
| 733 | }else{ | ||
| 734 | return 0; | ||
| 735 | } | ||
| 736 | } | ||
| 737 | |||
| 738 | |||
| 739 | ######################################################################### | ||
| 740 | |||
| 741 | =head2 C<has_diff> | ||
| 742 | |||
| 743 | my $result = $cdiff->has_diff( $diff_type, $key_value, $col_name ); | ||
| 744 | |||
| 745 | Returns 1 if there is a difference on a given column of a given type for | ||
| 746 | rows with a given key. 0 otherwise. | ||
| 747 | |||
| 748 | =cut | ||
| 749 | |||
| 750 | ######################################################################### | ||
| 751 | sub has_diff | ||
| 752 | { | ||
| 753 | my $self = shift; | ||
| 754 | my $diff_type = shift; | ||
| 755 | my $key = shift; | ||
| 756 | my $col_name = shift; | ||
| 757 | |||
| 758 | if( !$self->has_any_diff() ){ | ||
| 759 | return 0; | ||
| 760 | } | ||
| 761 | |||
| 762 | if( !exists $self->{ 'differences' }->{ $key } ){ | ||
| 763 | return 0; | ||
| 764 | } | ||
| 765 | if( $self->get_diff_type( $key ) eq $diff_type ){ | ||
| 766 | if( exists $self->{ 'differences' }->{ $key }-> | ||
| 767 | { 'diffs' }->{ $col_name } ){ | ||
| 768 | return 1; | ||
| 769 | } | ||
| 770 | } | ||
| 771 | return 0; | ||
| 772 | } | ||
| 773 | |||
| 774 | |||
| 775 | ######################################################################### | ||
| 776 | |||
| 777 | =head2 C<get_not_equal_keys> | ||
| 778 | |||
| 779 | my @key_values = $cdiff->get_not_equal_keys(); | ||
| 780 | |||
| 781 | Get keys of all rows which have 'not_equal' type of difference. | ||
| 782 | |||
| 783 | =cut | ||
| 784 | |||
| 785 | ######################################################################### | ||
| 786 | sub get_not_equal_keys | ||
| 787 | { | ||
| 788 | my $self = shift; | ||
| 789 | my @not_equal = (); | ||
| 790 | foreach my $key ( keys %{ $self->{ 'differences' } } ){ | ||
| 791 | if( $self->get_diff_type( $key ) eq "not_equal" ){ | ||
| 792 | push( @not_equal, $key ); | ||
| 793 | } | ||
| 794 | } | ||
| 795 | return @not_equal; | ||
| 796 | } | ||
| 797 | |||
| 798 | |||
| 799 | ######################################################################### | ||
| 800 | |||
| 801 | =head2 C<get_missing_keys> | ||
| 802 | |||
| 803 | my @key_values = $cdiff->get_missing_keys(); | ||
| 804 | |||
| 805 | Get keys of all rows which have 'missing' type of difference. | ||
| 806 | |||
| 807 | =cut | ||
| 808 | |||
| 809 | ######################################################################### | ||
| 810 | sub get_missing_keys | ||
| 811 | { | ||
| 812 | my $self = shift; | ||
| 813 | my @missing = (); | ||
| 814 | foreach my $key ( keys %{ $self->{ 'differences' } } ){ | ||
| 815 | if( $self->get_diff_type( $key ) eq "missing" ){ | ||
| 816 | push( @missing, $key ); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | return @missing; | ||
| 820 | } | ||
| 821 | |||
| 822 | |||
| 823 | ######################################################################### | ||
| 824 | |||
| 825 | =head2 C<get_additional_keys> | ||
| 826 | |||
| 827 | my @key_values = $cdiff->get_additional_keys(); | ||
| 828 | |||
| 829 | Get keys of all rows which have 'additional' type of difference. | ||
| 830 | |||
| 831 | =cut | ||
| 832 | |||
| 833 | ######################################################################### | ||
| 834 | sub get_additional_keys | ||
| 835 | { | ||
| 836 | my $self = shift; | ||
| 837 | my @additional = (); | ||
| 838 | foreach my $key ( keys %{ $self->{ 'differences' } } ){ | ||
| 839 | if( $self->get_diff_type( $key ) eq "additional" ){ | ||
| 840 | push( @additional, $key ); | ||
| 841 | } | ||
| 842 | } | ||
| 843 | return @additional; | ||
| 844 | } | ||
| 845 | |||
| 846 | |||
| 847 | ######################################################################### | ||
| 848 | |||
| 849 | =head2 C<is_missing> | ||
| 850 | |||
| 851 | my $result = $cdiff->is_missing( $key_value ); | ||
| 852 | |||
| 853 | Returns 1 if a row of a given key is 'missing'. | ||
| 854 | |||
| 855 | =cut | ||
| 856 | |||
| 857 | ######################################################################### | ||
| 858 | sub is_missing | ||
| 859 | { | ||
| 860 | my $self = shift; | ||
| 861 | my $key = shift; | ||
| 862 | if( !exists $self->{ 'differences' }->{ $key } ){ | ||
| 863 | return 0; | ||
| 864 | } | ||
| 865 | if( $self->get_diff_type( $key ) eq "missing" ){ | ||
| 866 | return 1; | ||
| 867 | } | ||
| 868 | return 0; | ||
| 869 | } | ||
| 870 | |||
| 871 | |||
| 872 | ######################################################################### | ||
| 873 | |||
| 874 | =head2 C<is_additional> | ||
| 875 | |||
| 876 | my $result = $cdiff->is_additional( $key_value ); | ||
| 877 | |||
| 878 | Returns 1 if a row of a given key is 'additional'. | ||
| 879 | |||
| 880 | =cut | ||
| 881 | |||
| 882 | ######################################################################### | ||
| 883 | sub is_additional | ||
| 884 | { | ||
| 885 | my $self = shift; | ||
| 886 | my $key = shift; | ||
| 887 | if( !exists $self->{ 'differences' }->{ $key } ){ | ||
| 888 | return 0; | ||
| 889 | } | ||
| 890 | if( $self->get_diff_type( $key ) eq "additional" ){ | ||
| 891 | return 1; | ||
| 892 | } | ||
| 893 | return 0; | ||
| 894 | } | ||
| 895 | |||
| 896 | |||
| 897 | ######################################################################### | ||
| 898 | |||
| 899 | =head2 C<get_missing_row> | ||
| 900 | |||
| 901 | my $row = $cdiff->get_missing_row( $key_value ); | ||
| 902 | |||
| 903 | Returns a 'missing' row for a given key value. If there is no such | ||
| 904 | 'missing' row, method dies. | ||
| 905 | |||
| 906 | =cut | ||
| 907 | |||
| 908 | ######################################################################### | ||
| 909 | sub get_missing_row | ||
| 910 | { | ||
| 911 | my $self = shift; | ||
| 912 | my $key = shift; | ||
| 913 | |||
| 914 | if( !$self->is_missing( $key ) ){ | ||
| 915 | die "No missing row for key $key"; | ||
| 916 | } | ||
| 917 | return $self->{ 'differences' }->{ $key }->{ 'row' } | ||
| 918 | } | ||
| 919 | |||
| 920 | |||
| 921 | ######################################################################### | ||
| 922 | |||
| 923 | =head2 C<get_additional_row> | ||
| 924 | |||
| 925 | my $row = $cdiff->get_additional_row( $key_value ); | ||
| 926 | |||
| 927 | Returns an 'additional' row for a given key value. If there is no such | ||
| 928 | 'additional' row, the method dies. | ||
| 929 | |||
| 930 | =cut | ||
| 931 | |||
| 932 | ######################################################################### | ||
| 933 | sub get_additional_row | ||
| 934 | { | ||
| 935 | my $self = shift; | ||
| 936 | my $key = shift; | ||
| 937 | |||
| 938 | if( !$self->is_additional( $key ) ){ | ||
| 939 | die "No missing row for key $key"; | ||
| 940 | } | ||
| 941 | return $self->{ 'differences' }->{ $key }->{ 'row' } | ||
| 942 | } | ||
| 943 | |||
| 944 | |||
| 945 | ######################################################################### | ||
| 946 | |||
| 947 | =head2 C<get_additional_rows> | ||
| 948 | |||
| 949 | my @rows = $cdiff->get_additional_rows(); | ||
| 950 | |||
| 951 | Returns all 'additional' rows. | ||
| 952 | |||
| 953 | =cut | ||
| 954 | |||
| 955 | ######################################################################### | ||
| 956 | sub get_additional_rows | ||
| 957 | { | ||
| 958 | my $self = shift; | ||
| 959 | my @a_keys = $self->get_additional_keys(); | ||
| 960 | my @a_rows = (); | ||
| 961 | |||
| 962 | foreach my $key ( @a_keys ){ | ||
| 963 | push( @a_rows, $self->get_additional_row( $key ) ); | ||
| 964 | } | ||
| 965 | return @a_rows; | ||
| 966 | } | ||
| 967 | |||
| 968 | |||
| 969 | ######################################################################### | ||
| 970 | |||
| 971 | =head2 C<remove_diff_additional> | ||
| 972 | |||
| 973 | $cdiff->remove_diff_additional( $row ); | ||
| 974 | |||
| 975 | Removes from internal structures an information that a row reffered to | ||
| 976 | by C<$row> is an 'additional' row. | ||
| 977 | |||
| 978 | =cut | ||
| 979 | |||
| 980 | ######################################################################### | ||
| 981 | sub remove_diff_additional | ||
| 982 | { | ||
| 983 | my $self = shift; | ||
| 984 | my $row = shift; | ||
| 985 | my $key = $self->{ 'before' }->get_key_value( $row ); | ||
| 986 | if( not exists $self->{ 'differences' }->{ $key } ){ | ||
| 987 | die "Removing not existing difference of type additional for key $key"; | ||
| 988 | }else{ | ||
| 989 | if( $self->{ 'differences' }->{ $key }->{ 'diff_type' } | ||
| 990 | eq "additional" ) | ||
| 991 | { | ||
| 992 | delete $self->{ 'differences' }->{ $key }; | ||
| 993 | }else{ | ||
| 994 | die "Diff type for $key is not 'additional'"; | ||
| 995 | } | ||
| 996 | } | ||
| 997 | } | ||
| 998 | |||
| 999 | |||
| 1000 | ######################################################################### | ||
| 1001 | |||
| 1002 | =head2 C<has_missing_diffs> | ||
| 1003 | |||
| 1004 | my $result = $cdiff->has_missing_diffs(); | ||
| 1005 | |||
| 1006 | Returns 1 if there is any difference of type 'missing'. 0 otherwise. | ||
| 1007 | |||
| 1008 | =cut | ||
| 1009 | |||
| 1010 | ######################################################################### | ||
| 1011 | sub has_missing_diffs | ||
| 1012 | { | ||
| 1013 | my $self = shift; | ||
| 1014 | my @keys = $self->get_missing_keys(); | ||
| 1015 | if( ! @keys ){ | ||
| 1016 | return 0; | ||
| 1017 | } | ||
| 1018 | return 1; | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | |||
| 1022 | ######################################################################### | ||
| 1023 | |||
| 1024 | =head2 C<has_additional_diffs> | ||
| 1025 | |||
| 1026 | my $result = $cdiff->has_additional_diffs(); | ||
| 1027 | |||
| 1028 | Returns 1 if there is any difference of type 'additional'. 0 otherwise. | ||
| 1029 | |||
| 1030 | =cut | ||
| 1031 | |||
| 1032 | ######################################################################### | ||
| 1033 | sub has_additional_diffs | ||
| 1034 | { | ||
| 1035 | my $self = shift; | ||
| 1036 | my @keys = $self->get_additional_keys(); | ||
| 1037 | if( ! @keys ){ | ||
| 1038 | return 0; | ||
| 1039 | } | ||
| 1040 | return 1; | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | |||
| 1044 | ######################################################################### | ||
| 1045 | |||
| 1046 | =head2 C<has_not_equal_diffs> | ||
| 1047 | |||
| 1048 | my $result = $cdiff->has_not_equal_diffs(); | ||
| 1049 | |||
| 1050 | Returns 1 if there is any difference of type 'not_equal'. 0 otherwise. | ||
| 1051 | |||
| 1052 | =cut | ||
| 1053 | |||
| 1054 | ######################################################################### | ||
| 1055 | sub has_not_equal_diffs | ||
| 1056 | { | ||
| 1057 | my $self = shift; | ||
| 1058 | my @keys = $self->get_not_equal_keys(); | ||
| 1059 | if( ! @keys ){ | ||
| 1060 | return 0; | ||
| 1061 | } | ||
| 1062 | return 1; | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | |||
| 1066 | ######################################################################### | ||
| 1067 | |||
| 1068 | =head2 C<get_before_fname> | ||
| 1069 | |||
| 1070 | my $result = $cdiff->get_before_fname(); | ||
| 1071 | |||
| 1072 | Returns file name which was a source of data for before collection. | ||
| 1073 | |||
| 1074 | =cut | ||
| 1075 | |||
| 1076 | ######################################################################### | ||
| 1077 | sub get_before_fname | ||
| 1078 | { | ||
| 1079 | my $self = shift; | ||
| 1080 | return $self->{ 'before' }->{ 'data_file' }; | ||
| 1081 | } | ||
| 1082 | |||
| 1083 | |||
| 1084 | ######################################################################### | ||
| 1085 | |||
| 1086 | =head2 C<get_after_fname> | ||
| 1087 | |||
| 1088 | my $result = $cdiff->get_after_fname(); | ||
| 1089 | |||
| 1090 | Returns file name which was a source of data for after collection. | ||
| 1091 | |||
| 1092 | =cut | ||
| 1093 | |||
| 1094 | ######################################################################### | ||
| 1095 | sub get_after_fname | ||
| 1096 | { | ||
| 1097 | my $self = shift; | ||
| 1098 | return $self->{ 'after' }->{ 'data_file' }; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | sub print_line_of_text | ||
| 1102 | { | ||
| 1103 | my $self = shift; | ||
| 1104 | my $text = shift; | ||
| 1105 | |||
| 1106 | if( not exists $self->{ 'header_printed' } ){ | ||
| 1107 | if( $self->{ 'report_header' } ){ | ||
| 1108 | print "". $self->{ 'report_header' } . | ||
| 1109 | " " . $self->get_before_fname() . | ||
| 1110 | " " . $self->get_after_fname() . "\n"; | ||
| 1111 | } | ||
| 1112 | my $cols = $self->{ 'after' }->get_columns(); | ||
| 1113 | print "SCHEMA: " . join( "\t", @$cols ) . "\n"; | ||
| 1114 | $self->{ 'header_printed' } = 1; | ||
| 1115 | } | ||
| 1116 | print "$text\n"; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | |||
| 1120 | |||
| 1121 | 1; | ||
| 1122 | __END__ |