Compare.pm

filter: Statementstmt 4.8% 18 / 370 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 13.6% 6 / 44 Podpod 0.0% 0 / 1 totaltotal 3.8% 24 / 620 CC 101 SCAR 37.0
CC
101
Cov
3%
compare
156.0
deep_compare
72.0
find_field_diffs
72.0
CRAP 40.6
↑ Summary Compare/Pg.pm »
1 package DBIx::Compare;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '1.7';
9  
10 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $SIG{__WARN__} = \&trap_warn;
12 }
13 0 ▶sub trap_warn {
Subroutine trap_warn: not called CC=2 SCAR=17.9
Pod undocumented
14 0 my $signal = shift;
15 0 ▶ if ($ENV{ _VERBOSE_ }){
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'_VERBOSE_'}) { } 0 0 0
16 0 warn $signal;
17 } else {
18 0 return;
19 }
20 }
21  
22 { package db_comparison;
23  
24 0 ▶ sub new {
Subroutine new: not called CC=4 SCAR=30.0
25 0 my $class = shift;
26 0 my $self = { };
27 0 bless $self, $class;
28 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
29 0 $self->set_dbh(@_);
30 0 ▶ if ($class eq 'db_comparison'){
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "db_comparison") 0 0 0
31 0 ▶ if (my $new_class = $self->guess_class){
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_class = $self->guess_class) 0 0 0
32 0 bless $self, $new_class;
33 }
34 }
35 } else {
36 0 die "DBIx::Compare ERROR: Need two database handles to compare\n";
37 }
38 0 return $self;
39 }
40 0 ▶ sub guess_class {
Subroutine guess_class: not called CC=4 SCAR=25.8
41 0 my $self = shift;
42 0 ▶ if (my $dbd = $self->get_db_driver){
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbd = $self->get_db_driver) 0 0 0
43 1 ▶ use Module::List qw(list_modules);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 0 my $want_this = 'DBIx::Compare::'.$dbd;
45 0 my $class = lc($dbd)."_comparison";
46
47 0 my $hModules = list_modules('DBIx::Compare::',{list_modules=>1});
48 0 for my $module (keys %$hModules){
49 0 ▶ if ($want_this =~ /^$module/){ # i.e. driver SQLite2 will match DBIx::Compare::SQLite
Branch: 1 branch
BranchTrueFalseTotal
if ($want_this =~ /^$module/) 0 0 0
50 0 eval "require $module";
51 0 return $class;
52 }
53 }
54 0 warn "DBIx::Compare ERROR; No plug-in for driver '$dbd'\nSome comparisons may not work\n";
55 }
56 0 return;
57 }
58 0 ▶ sub set_dbh {
Subroutine set_dbh: not called CC=5 SCAR=34.0
59 0 my ($self,$dbh1,$dbh2) = @_;
60 0 ▶ die "DBIx::Compare ERROR: Need two database handles to compare\n" unless ($dbh2);
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh2 0 0 0
61 0 ▶ die "DBIx::Compare ERROR: Not a database handle\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh1->isa("DBI::db") and $dbh2->isa("DBI::db") 0 0 0
Condition0%0/3
$dbh1->isa("DBI::db") and $dbh2->isa("DBI::db")
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh1->isa("DBI::db") and $dbh2->isa("DBI::db")
$dbh1->isa("DBI::db") $dbh2->isa("DBI::db")
Truth table0%0/3
A and B
A= $dbh1->isa("DBI::db")B= $dbh2->isa("DBI::db")
ABresult
0X0
100
111
62 unless (($dbh1->isa('DBI::db')) && ($dbh2->isa('DBI::db')));
63
64 0 $self->{ _db1 }{ _dbh } = $dbh1;
65 0 $self->{ _db1 }{ _Name } = $dbh1->{ Name };
66 0 $self->{ _db2 }{ _dbh } = $dbh2;
67 0 $self->{ _db2 }{ _Name } = $dbh2->{ Name };
68
69 0 $self->set_parsed_name($dbh1);
70 0 $self->set_parsed_name($dbh2);
71  
72 0 my $dbd1 = $dbh1->{ Driver }{ Name };
73 0 my $dbd2 = $dbh2->{ Driver }{ Name };
74 0 ▶ if ($dbd1 eq $dbd2){
Branch: 1 branch
BranchTrueFalseTotal
if ($dbd1 eq $dbd2) { } 0 0 0
75 0 $self->{ _DB_Driver } = $dbd1;
76 } else {
77 0 warn "DBIx::Compare ERROR; Database drivers need to be the same for some comparisons\n";
78 }
79 }
80 # Allow certain forms of dsn:
81 # "...[database|dbname]=mydb..." or
82 # "mydb:..." or
83 # "mydb;..."
84 # "mydb"
85 #
86 0 ▶ sub set_parsed_name {
Subroutine set_parsed_name: not called CC=7 SCAR=40.3
87 0 my ($self,$dbh) = @_;
88 0 my $parsed_name = undef;
89 0 my $name = $dbh->{ Name };
90 0 ▶ if ($name =~ m/((database|dbname)(\w)*=(\w)+((-)*(\w)*)*)/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($name =~ /((database|dbname)(\w)*=(\w)+((-)*(\w)*)*)/) { } 0 0 0
elsif ($name =~ /^((\w)+((-)*(\w)*)*):/) { } 0 0 0
elsif ($name =~ /^((\w)+((-)*(\w)*)*);/) { } 0 0 0
elsif ($name =~ /^((\w)+((-)*(\w)*)*$)/) { } 0 0 0
91 0 $parsed_name = $1;
92 0 $parsed_name =~ s/(database|dbname)(\w)*=//;
93 } elsif ($name =~ m/^((\w)+((-)*(\w)*)*):/) {
94 0 $parsed_name = $1;
95 } elsif ($name =~ m/^((\w)+((-)*(\w)*)*);/) {
96 0 $parsed_name = $1;
97 } elsif ($name =~ m/^((\w)+((-)*(\w)*)*$)/) {
98 0 $parsed_name = $1;
99 }
100  
101 0 ▶ if (!$parsed_name || ($parsed_name =~ m/(\w)+=(\w)+/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parsed_name or $parsed_name =~ /(\w)+=(\w)+/) 0 0 0
Condition0%0/3
not $parsed_name or $parsed_name =~ /(\w)+=(\w)+/
l!l&&r!l&&!r
000
MC/DC0%0/2
not $parsed_name or $parsed_name =~ /(\w)+=(\w)+/
not $parsed_name $parsed_name =~ /(\w)+=(\w)+/
Truth table0%0/3
not(A) or B
A= not $parsed_nameB= $parsed_name =~ /(\w)+=(\w)+/
ABresult
1X1
011
000
102 0 die "DBIx::Compare ERROR; Cannot extract database name from connection string: $name\n";
103 }
104 0 $self->{ $name }{ _parsed_Name } = $parsed_name;
105 }
106 0 ▶ sub get_parsed_name {
Subroutine get_parsed_name: not called CC=1 SCAR=6.9
107 0 my ($self,$dbh) = @_;
108 0 return $self->{ $dbh->{ Name } }{ _parsed_Name };
109 }
110 0 ▶ sub verbose {
Subroutine verbose: not called CC=1 SCAR=6.9
111 0 $ENV{ _VERBOSE_ }++;
112 }
113 0 ▶ sub be_verbose {
Subroutine be_verbose: not called CC=1 SCAR=6.9
114 0 $ENV{ _VERBOSE_ };
115 }
116 0 ▶ sub get_dbh {
Subroutine get_dbh: not called CC=1 SCAR=6.9
117 0 my $self = shift;
118 0 return ($self->{ _db1 }{ _dbh },$self->{ _db2 }{ _dbh });
119 }
120 0 ▶ sub get_db_names {
Subroutine get_db_names: not called CC=1 SCAR=6.9
121 0 my $self = shift;
122 0 return ($self->{ _db1 }{ _Name },$self->{ _db2 }{ _Name });
123 }
124 # DBD will not be set if plug-in not available
125 0 ▶ sub get_db_driver {
Subroutine get_db_driver: not called CC=1 SCAR=6.9
126 0 my $self = shift;
127 0 return $self->{ _DB_Driver };
128 }
129 0 ▶ sub get_tables {
Subroutine get_tables: not called CC=4 SCAR=30.0
130 0 my $self = shift;
131 0 ▶ unless (defined $self->{ _db1 }{ _Tables } && $self->{ _db2 }{ _Tables }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_db1"}{"_Tables"} and $self->{"_db2"}{"_Tables"}) 0 0 0
Condition0%0/3
defined $self->{"_db1"}{"_Tables"} and $self->{"_db2"}{"_Tables"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"_db1"}{"_Tables"} and $self->{"_db2"}{"_Tables"}
defined $self->{"_db1"}{"_Tables"} $self->{"_db2"}{"_Tables"}
Truth table0%0/3
A and B
A= defined $self->{"_db1"}{"_Tables"}B= $self->{"_db2"}{"_Tables"}
ABresult
0X0
100
111
132 0 my ($dbh1,$dbh2) = $self->get_dbh;
133 0 $self->{ _db1 }{ _Tables } = $self->fetch_multisinglefield("show tables",$dbh1);
134 0 $self->{ _db2 }{ _Tables } = $self->fetch_multisinglefield("show tables",$dbh2);
135 }
136 0 ▶ if (wantarray()){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
137 0 return ( $self->{ _db1 }{ _Tables },$self->{ _db2 }{ _Tables } );
138 } else {
139 0 return $self->{ _db1 }{ _Tables };
140 }
141 }
142 #ĂŠlist of tables in each db
143 0 ▶ sub common_tables {
Subroutine common_tables: not called CC=3 SCAR=24.8
144 0 my $self = shift;
145 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
146 0 $self->{ _common_tables } = shift;
147 } else {
148 0 ▶ $self->compare_table_lists unless (defined $self->{ _common_tables });
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_common_tables"} 0 0 0
149 0 return $self->{ _common_tables };
150 }
151 }
152 # list of tables in each db with same row count
153 0 ▶ sub similar_tables {
Subroutine similar_tables: not called CC=3 SCAR=24.8
154 0 my $self = shift;
155 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
156 0 $self->{ _similar_tables } = shift;
157 } else {
158 0 ▶ $self->compare_row_counts unless (defined $self->{ _similar_tables });
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_similar_tables"} 0 0 0
159 0 return $self->{ _similar_tables };
160 }
161 }
162 0 ▶ sub get_differences {
Subroutine get_differences: not called CC=4 SCAR=30.0
163 0 my $self = shift;
164 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
165 0 my $type = shift;
166 0 ▶ unless (defined $self->{ _error_list }{ $type }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_error_list"}{$type}) 0 0 0
167 0 $self->{ _error_list }{ $type } = [];
168 }
169 0 return $self->{ _error_list }{ $type };
170 } else {
171 0 ▶ unless (defined $self->{ _error_list }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_error_list"}) 0 0 0
172 0 $self->{ _error_list } = {};
173 }
174 0 return $self->{ _error_list };
175 }
176 }
177 0 ▶ sub add_errors {
Subroutine add_errors: not called CC=1 SCAR=6.9
178 0 my $self = shift;
179 0 my $aErrors = $self->get_differences(shift);
180 0 push (@$aErrors, @_);
181 }
182 0 ▶ sub deep_compare {
Subroutine deep_compare: not called CC=8 SCAR=42.8
183 0 my $self = shift;
184  
185 0 my ($dbh1,$dbh2) = $self->get_dbh;
186 0 my $same = 1;
187
188 0 my @aTables;
189 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
190 0 @aTables = @_;
191 } else {
192 0 $same = $self->compare; # sets minimal similar tables
193 0 ▶ warn "Only running deep_compare() on similar tables\n" unless ($same);
Branch: 1 branch
BranchTrueFalseTotal
unless $same 0 0 0
194 0 @aTables = @{ $self->similar_tables };
195 }
196
197 0 TABLE:for my $table (@aTables){
198 0 my $primary_key = $self->get_primary_keys($table,$dbh1);
199 # recursively calls compare_field_lists() and common_tables()
200 # if relevant $self fields are not already filled
201 0 my $fields = $self->field_list($table); # common fields
202 0 my $statement = "select $fields from $table order by $primary_key";
203  
204 0 my $sth1 = $dbh1->prepare($statement);
205 0 $sth1->execute();
206 0 my $sth2 = $dbh2->prepare($statement);
207 0 $sth2->execute();
208
209 0 my $row = 0;
210
211 0 ▶ ROW:while(my $aResult_Row1 = $sth1->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $aResult_Row1 = $sth1->fetchrow_arrayref) 0 0 0
212 0 $row++;
213 0 my $aResult_Row2 = $sth2->fetchrow_arrayref();
214 0 ▶ my @aResult_Row1 = map { $_ ? $_ : '' } @$aResult_Row1;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
215 0 ▶ my @aResult_Row2 = map { $_ ? $_ : '' } @$aResult_Row2;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
216 0 ▶ unless (join(',',@aResult_Row1) eq join(',',@aResult_Row2)){
Branch: 1 branch
BranchTrueFalseTotal
unless (join(",", @aResult_Row1) eq join(",", @aResult_Row2)) 0 0 0
217 0 warn "Discrepancy in table '$table' at row $row\n";
218 0 $self->add_errors("Discrepancy in table $table",$row);
219 0 $same = undef;
220 0 next TABLE;
221 }
222 }
223 0 $sth1->finish(); # we're done with this query
224 0 $sth2->finish(); # we're done with this query
225 }
226 0 return $same;
227 }
228 0 ▶ sub compare {
Subroutine compare: not called CC=12 SCAR=50.5
229 0 my $self = shift;
230
231 0 my $tables = $self->compare_table_lists;
232 0 my $fields = $self->compare_table_fields;
233 0 my $rows = $self->compare_row_counts;
234 0 my $stats = $self->compare_table_stats;
235
236 0 ▶ if ($tables && $fields && $rows && $stats){
Branch: 1 branch
BranchTrueFalseTotal
if ($tables and $fields and $rows and $stats) { } 0 0 0
Condition0%0/9
$tables and $fields and $rows and $stats
!ll&&!rl&&r
000
$tables and $fields and $rows
!ll&&!rl&&r
000
$tables and $fields
!ll&&!rl&&r
000
MC/DC0%0/4
$tables and $fields and $rows and $stats
$tables $fields $rows $stats
Truth table0%0/5
A and B and C and D
A= $tablesB= $fieldsC= $rowsD= $stats
ABCDresult
0XXX0
10XX0
110X0
11100
11111
237 0 return 1;
238 0 warn "No differences were found\n";
239 } else {
240 0 ▶ unless ($tables){
Branch: 1 branch
BranchTrueFalseTotal
unless ($tables) 0 0 0
241 0 warn "Table lists are different\n".
242 "\tComparing the common tables...\n";
243 }
244 0 ▶ unless ($rows){
Branch: 1 branch
BranchTrueFalseTotal
unless ($rows) 0 0 0
245 0 warn "Row counts in some tables are different\n".
246 "\tComparing the content of tables with the same row count...\n";
247 }
248 0 ▶ unless ($fields){
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields) 0 0 0
249 0 warn "Table field names are different\n".
250 "\tComparing the common fields...\n";
251 }
252 0 ▶ unless ($stats){
Branch: 1 branch
BranchTrueFalseTotal
unless ($stats) 0 0 0
253 0 warn "Some field values are different\n";
254 }
255 0 my $hDiffs = $self->get_differences;
256 0 ▶ if (%$hDiffs){
Branch: 1 branch
BranchTrueFalseTotal
if (%$hDiffs) 0 0 0
257 0 ▶ while (my ($type,$aErrors) = each %$hDiffs){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hDiffs) 0 0 0
258 0 warn "$type:\n";
259 0 for my $error (@$aErrors){
260 0 warn "\t$error\n";
261 }
262 }
263 }
264 0 return;
265 }
266 }
267 0 ▶ sub compare_table_stats {
Subroutine compare_table_stats: not called CC=5 SCAR=34.0
268 0 my $self = shift;
269
270 0 my $aTables = $self->similar_tables;
271 0 my @aNew_Similar_Tables = ();
272 0 my $similar = 1;
273
274 0 TABLE:for my $table (@$aTables){
275 0 my @aFields = $self->field_list($table);
276 0 my @aBad_Fields = ();
277
278 0 FIELD:for my $field (@aFields){
279 0 ▶ unless ($self->compare_field_stats($table,$field)){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->compare_field_stats($table, $field)) 0 0 0
280 0 push(@aBad_Fields,$field);
281 }
282 }
283 0 ▶ if (@aBad_Fields){
Branch: 1 branch
BranchTrueFalseTotal
if (@aBad_Fields) { } 0 0 0
284 0 $self->add_errors("Bad fields in table $table",@aBad_Fields);
285 0 $similar = undef;
286 } else {
287 0 push (@aNew_Similar_Tables,$table);
288 }
289 }
290 # reset similar tables
291 0 $self->similar_tables(\@aNew_Similar_Tables);
292 0 return $similar;
293 }
294 0 ▶ sub compare_field_stats {
Subroutine compare_field_stats: not called CC=7 SCAR=40.3
295 0 my ($self,$table,$field) = @_;
296
297 0 my ($type1,$type2) = $self->get_field_type($table,$field);
298 0 ▶ if (($type1 =~ /date|time|interval/i)&&($type2 =~ /date|time|interval/i)){
Branch: 3 branches
BranchTrueFalseTotal
if ($type1 =~ /date|time|interval/i and $type2 =~ /date|time|interval/i) { } 0 0 0
elsif ($type1 =~ /int|double|real|float|num|dec/i and $type2 =~ /int|double|real|float|num|dec/i) { } 0 0 0
elsif ($type1 =~ /char|text|lob|byte|binary/i and $type2 =~ /char|text|lob|byte|binary/i) { } 0 0 0
Condition0%0/3
$type1 =~ /date|time|interval/i and $type2 =~ /date|time|interval/i
!ll&&!rl&&r
000
MC/DC0%0/2
$type1 =~ /date|time|interval/i and $type2 =~ /date|time|interval/i
$type1 =~ /date|time|interval/i $type2 =~ /date|time|interval/i
Truth table0%0/3
A and B
A= $type1 =~ /date|time|interval/iB= $type2 =~ /date|time|interval/i
ABresult
0X0
100
111
299 0 return $self->compare_datetime_field($table,$field);
300 ▶ } elsif (($type1 =~ /int|double|real|float|num|dec/i) && ($type2 =~ /int|double|real|float|num|dec/i)){
Condition0%0/3
$type1 =~ /int|double|real|float|num|dec/i and $type2 =~ /int|double|real|float|num|dec/i
!ll&&!rl&&r
000
MC/DC0%0/2
$type1 =~ /int|double|real|float|num|dec/i and $type2 =~ /int|double|real|float|num|dec/i
$type1 =~ /int|double|real|float|num|dec/i $type2 =~ /int|double|real|float|num|dec/i
Truth table0%0/3
A and B
A= $type1 =~ /int|double|real|float|num|dec/iB= $type2 =~ /int|double|real|float|num|dec/i
ABresult
0X0
100
111
301 0 return $self->compare_numeric_field($table,$field);
302 ▶ } elsif (($type1 =~ /char|text|lob|byte|binary/i)&&($type2 =~ /char|text|lob|byte|binary/i)){
Condition0%0/3
$type1 =~ /char|text|lob|byte|binary/i and $type2 =~ /char|text|lob|byte|binary/i
!ll&&!rl&&r
000
MC/DC0%0/2
$type1 =~ /char|text|lob|byte|binary/i and $type2 =~ /char|text|lob|byte|binary/i
$type1 =~ /char|text|lob|byte|binary/i $type2 =~ /char|text|lob|byte|binary/i
Truth table0%0/3
A and B
A= $type1 =~ /char|text|lob|byte|binary/iB= $type2 =~ /char|text|lob|byte|binary/i
ABresult
0X0
100
111
303 0 return $self->compare_string_field($table,$field);
304 } else {
305 0 $self->add_errors("Could not compare field types",($type1,$type2));
306 0 return;
307 }
308 }
309 0 ▶ sub do_compare_field {
Subroutine do_compare_field: not called CC=4 SCAR=30.0
310 0 my ($self,$statement) = @_;
311 0 my ($dbh1,$dbh2) = $self->get_dbh;
312 0 my $aResult1 = $self->sql_fetcharray_singlerow($statement,$dbh1);
313 0 my $aResult2 = $self->sql_fetcharray_singlerow($statement,$dbh2);
314 # avoid unnecessary warnings
315 0 ▶ my @aResult1 = map { $_ ? $_ : '' } @$aResult1;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
316 0 ▶ my @aResult2 = map { $_ ? $_ : '' } @$aResult2;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
317 0 ▶ if (join(',',@aResult1) eq join(',',@aResult2)){
Branch: 1 branch
BranchTrueFalseTotal
if (join(',', @aResult1) eq join(',', @aResult2)) { } 0 0 0
318 0 return 1;
319 } else {
320 0 return;
321 }
322 }
323 0 ▶ sub set_field_info {
Subroutine set_field_info: not called CC=3 SCAR=24.8
324 0 my ($self,$table) = @_;
325 0 my @aDBH = $self->get_dbh;
326
327 0 for my $dbh (@aDBH){
328 0 my $db_name = $dbh->{ Name };
329 0 my $ahResults = $self->fetchhash_multirow("DESCRIBE $table",$dbh);
330 0 my @aFields = ();
331 0 for my $hResult (@$ahResults){
332 0 my $field = $$hResult{ Field };
333 0 push (@aFields, $field);
334 0 my $type = $$hResult{ Type };
335 0 $self->{ $db_name }{ $table }{ Fields }{ $field } = $type;
336 }
337 0 my @aSorted_Fields = sort @aFields;
338 0 $self->{ $db_name }{ $table }{ Sorted_Fields } = \@aSorted_Fields;
339 }
340 }
341 0 ▶ sub get_fields {
Subroutine get_fields: not called CC=3 SCAR=24.8
342 0 my ($self,$table,$dbh) = @_;
343 0 my $db_name = $dbh->{ Name };
344 0 ▶ unless ((defined $self->{ $db_name }{ $table }) && (defined $self->{ $db_name }{ $table }{ Sorted_Fields })){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$db_name}{$table} and defined $self->{$db_name}{$table}{"Sorted_Fields"}) 0 0 0
Condition0%0/3
defined $self->{$db_name}{$table} and defined $self->{$db_name}{$table}{"Sorted_Fields"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{$db_name}{$table} and defined $self->{$db_name}{$table}{"Sorted_Fields"}
defined $self->{$db_name}{$table} defined $self->{$db_name}{$table}{"Sorted_Fields"}
Truth table0%0/3
A and B
A= defined $self->{$db_name}{$table}B= defined $self->{$db_name}{$table}{"Sorted_Fields"}
ABresult
0X0
100
111
345 0 $self->set_field_info($table);
346 }
347 0 return $self->{ $db_name }{ $table }{ Sorted_Fields };
348 }
349 0 ▶ sub get_field_type {
Subroutine get_field_type: not called CC=5 SCAR=34.0
350 0 my ($self,$table,$field) = @_;
351 0 my ($db1,$db2) = $self->get_db_names;
352 0 ▶ unless ((defined $self->{ $db1 }{ $table }) && (defined $self->{ $db2 }{ $table })
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$db1}{$table} and defined $self->{$db2}{$table} and defined $self->{$db1}{$table}{"Fields"} and defined $self->{$db2}{$table}{"Fields"}) 0 0 0
Condition0%0/9
defined $self->{$db1}{$table} and defined $self->{$db2}{$table} and defined $self->{$db1}{$table}{"Fields"} and defined $self->{$db2}{$table}{"Fields"}
!ll&&!rl&&r
000
defined $self->{$db1}{$table} and defined $self->{$db2}{$table} and defined $self->{$db1}{$table}{"Fields"}
!ll&&!rl&&r
000
defined $self->{$db1}{$table} and defined $self->{$db2}{$table}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $self->{$db1}{$table} and defined $self->{$db2}{$table} and defined $self->{$db1}{$table}{"Fields"} and defined $self->{$db2}{$table}{"Fields"}
defined $self->{$db1}{$table} defined $self->{$db2}{$table} defined $self->{$db1}{$table}{"Fields"} defined $self->{$db2}{$table}{"Fields"}
Truth table0%0/5
A and B and C and D
A= defined $self->{$db1}{$table}B= defined $self->{$db2}{$table}C= defined $self->{$db1}{$table}{"Fields"}D= defined $self->{$db2}{$table}{"Fields"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
353 && (defined $self->{ $db1 }{ $table }{ Fields }) && (defined $self->{ $db2 }{ $table }{ Fields })){
354 0 $self->set_field_info($table);
355 }
356 0 my $type1 = $self->{ $db1 }{ $table }{ Fields }{ $field };
357 0 my $type2 = $self->{ $db2 }{ $table }{ Fields }{ $field };
358 0 return ($type1,$type2);
359 }
360 0 ▶ sub compare_table_fields {
Subroutine compare_table_fields: not called CC=3 SCAR=24.8
361 0 my $self = shift;
362 0 my $aTables = $self->common_tables;
363 0 my $diffs = 1;
364 0 for my $table (@$aTables){
365 0 my $same = $self->compare_field_lists($table);
366 0 ▶ $diffs = undef unless ($same);
Branch: 1 branch
BranchTrueFalseTotal
unless $same 0 0 0
367 }
368 0 return $diffs;
369 }
370 0 ▶ sub compare_field_lists {
Subroutine compare_field_lists: not called CC=2 SCAR=17.9
371 0 my ($self,$table) = @_;
372
373 0 my ($dbh1,$dbh2) = $self->get_dbh;
374 0 my $aFields1 = $self->get_fields($table,$dbh1);
375 0 my $aFields2 = $self->get_fields($table,$dbh2);
376
377 0 ▶ if (join(',',@$aFields1) eq join(',',@$aFields2)){
Branch: 1 branch
BranchTrueFalseTotal
if (join(',', @$aFields1) eq join(',', @$aFields2)) { } 0 0 0
378 0 $self->field_list($table,$aFields1);
379 0 return 1;
380 } else {
381 0 $self->find_field_diffs($table,$aFields1,$aFields2);
382 0 return;
383 }
384 }
385 0 ▶ sub find_field_diffs {
Subroutine find_field_diffs: not called CC=8 SCAR=42.8
386 0 my ($self,$table,$aFields1,$aFields2) = @_;
387  
388 0 my ($dbh1,$dbh2) = $self->get_dbh;
389
390 0 my (%hFields1,%hFields2,@aNotIn1,@aNotIn2,@aInBoth);
391
392 0 for my $field1 (@$aFields1){
393 0 $hFields1{ $field1 }++;
394 }
395 0 for my $field2 (@$aFields2){
396 0 $hFields2{ $field2 }++;
397 0 ▶ if (defined $hFields1{ $field2 }){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hFields1{$field2}) { } 0 0 0
398 0 push(@aInBoth,$field2);
399 } else {
400 0 push(@aNotIn1,$field2);
401 }
402 }
403 0 for my $field1 (@$aFields1){
404 0 ▶ unless (defined $hFields2{ $field1 }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hFields2{$field1}) 0 0 0
405 0 push(@aNotIn2,$field1);
406 }
407 }
408 0 $self->field_list($table,\@aInBoth);
409 0 my ($db1,$db2) = $self->get_db_names;
410 0 ▶ $self->add_errors("Fields unique to $db1\.$table",@aNotIn2) if (@aNotIn2);
Branch: 1 branch
BranchTrueFalseTotal
if @aNotIn2 0 0 0
411 0 ▶ $self->add_errors("Fields unique to $db2\.$table",@aNotIn1) if (@aNotIn1);
Branch: 1 branch
BranchTrueFalseTotal
if @aNotIn1 0 0 0
412 }
413 0 ▶ sub field_list {
Subroutine field_list: not called CC=4 SCAR=30.0
414 0 my $self = shift;
415 0 my $table = shift;
416 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
417 0 my $aInBoth = shift;
418 0 $self->{ _field_lists }{ $table } = $aInBoth;
419 0 $self->{ _fields_strings }{ $table } = join(',',@$aInBoth);
420 } else {
421 0 ▶ $self->compare_field_lists($table) unless (defined $self->{ _field_lists }{ $table });
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_field_lists"}{$table} 0 0 0
422 0 ▶ if (wantarray()){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
423 0 @{ $self->{ _field_lists }{ $table } };
424 } else {
425 0 $self->{ _fields_strings }{ $table };
426 }
427 }
428 }
429 0 ▶ sub compare_table_lists {
Subroutine compare_table_lists: not called CC=2 SCAR=17.9
430 0 my $self = shift;
431 0 my ($aTables1,$aTables2) = $self->get_tables;
432 0 ▶ if (join(',',@$aTables1) eq join(',',@$aTables2)){
Branch: 1 branch
BranchTrueFalseTotal
if (join(',', @$aTables1) eq join(',', @$aTables2)) { } 0 0 0
433 0 $self->common_tables($aTables1);
434 0 return 1;
435 } else {
436 0 $self->find_table_diffs($aTables1,$aTables2);
437 0 return;
438 }
439 }
440 0 ▶ sub find_table_diffs {
Subroutine find_table_diffs: not called CC=8 SCAR=42.8
441 0 my ($self,$aTables1,$aTables2) = @_;
442
443 0 my ($dbh1,$dbh2) = $self->get_dbh;
444
445 0 my (%hTables1,%hTables2,@aNotIn1,@aNotIn2,@aInBoth);
446
447 0 for my $table1 (@$aTables1){
448 0 $hTables1{ $table1 }++;
449 }
450 0 for my $table2 (@$aTables2){
451 0 $hTables2{ $table2 }++;
452 0 ▶ if (defined $hTables1{ $table2 }){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hTables1{$table2}) { } 0 0 0
453 0 push(@aInBoth,$table2);
454 } else {
455 0 push(@aNotIn1,$table2);
456 }
457 }
458 0 for my $table1 (@$aTables1){
459 0 ▶ unless (defined $hTables2{ $table1 }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hTables2{$table1}) 0 0 0
460 0 push(@aNotIn2,$table1);
461 }
462 }
463 0 $self->common_tables(\@aInBoth);
464 0 my ($db1,$db2) = $self->get_db_names;
465 0 ▶ $self->add_errors("Tables unique to $db1",@aNotIn2) if (@aNotIn2);
Branch: 1 branch
BranchTrueFalseTotal
if @aNotIn2 0 0 0
466 0 ▶ $self->add_errors("Tables unique to $db2",@aNotIn1) if (@aNotIn1);
Branch: 1 branch
BranchTrueFalseTotal
if @aNotIn1 0 0 0
467 }
468
469 0 ▶ sub compare_row_counts {
Subroutine compare_row_counts: not called CC=5 SCAR=34.0
470 0 my $self = shift;
471 0 my ($dbh1,$dbh2) = $self->get_dbh;
472 0 my ($aTables,@aErrors,@aOK_Tables);
473 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
474 0 $aTables = [shift];
475 } else {
476 0 $aTables = $self->common_tables;
477 }
478 0 TABLE:for my $table (@$aTables){
479 0 ▶ if ($self->row_count($table,$dbh1) != $self->row_count($table,$dbh2)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->row_count($table, $dbh1) != $self->row_count($table, $dbh2)) { } 0 0 0
480 0 push(@aErrors,$table);
481 } else {
482 0 push(@aOK_Tables,$table);
483 }
484 }
485 0 $self->similar_tables(\@aOK_Tables);
486 0 ▶ if (@aErrors){
Branch: 1 branch
BranchTrueFalseTotal
if (@aErrors) { } 0 0 0
487 0 $self->add_errors('Row count',@aErrors);
488 0 return;
489 } else {
490 0 return 1;
491 }
492 }
493 0 ▶ sub get_primary_keys {
Subroutine get_primary_keys: not called CC=4 SCAR=30.0
494 0 my ($self,$table,$dbh) = @_;
495 0 my $db = $dbh->{ Name };
496 0 ▶ unless (defined $self->{ $db }{ $table }{ _primary_keys }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{$db}{$table}{"_primary_keys"}) 0 0 0
497 0 $self->set_primary_keys($table,$dbh);
498 }
499 0 my $aKeys = $self->{ $db }{ $table }{ _primary_keys };
500 0 ▶ if (@$aKeys){
Branch: 1 branch
BranchTrueFalseTotal
if (@$aKeys) { } 0 0 0
501 0 ▶ if (wantarray()){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
502 0 return @$aKeys;
503 } else {
504 0 return join(',',@$aKeys);
505 }
506 } else {
507 0 return;
508 }
509 }
510 0 ▶ sub set_primary_keys {
Subroutine set_primary_keys: not called CC=1 SCAR=6.9
511 0 my ($self,$table,$dbh) = @_;
512 0 my $db = $dbh->{ Name };
513 0 my $db_name = $self->get_parsed_name($dbh);
514 0 my @aKeys = $dbh->primary_key( $db_name,undef,$table );
515 0 $self->{ $db }{ $table }{ _primary_keys } = \@aKeys;
516 }
517 0 ▶ sub row_count {
Subroutine row_count: not called CC=1 SCAR=6.9
518 0 my ($self,$table,$dbh) = @_;
519 0 return $self->fetch_singlefield("select count(*) from $table",$dbh); # shifting $dbh
520 }
521 0 ▶ sub fetchhash_multirow {
Subroutine fetchhash_multirow: not called CC=3 SCAR=24.8
522 0 my ($self,$statement,$dbh) = @_;
523 0 my @ahResults_Rows;
524 0 eval {
525 0 my $sth = $dbh->prepare($statement);
526 0 $sth->execute();
527 0 ▶ while(my $hResult_Row = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hResult_Row = $sth->fetchrow_hashref) 0 0 0
528 0 push @ahResults_Rows, $hResult_Row;
529 }
530 0 $sth->finish(); # we're done with this query
531 };
532 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
533 0 die $@;
534 } else {
535 0 return \@ahResults_Rows;
536 }
537 }
538 0 ▶ sub fetch_multisinglefield {
Subroutine fetch_multisinglefield: not called CC=3 SCAR=24.8
539 0 my ($self,$statement,$dbh) = @_;
540 0 my @aValues;
541 my $value;
542 0 eval {
543 0 my $sth = $dbh->prepare($statement);
544 0 $sth->execute();
545 0 $sth->bind_columns(undef, \$value);
546 0 ▶ while($sth->fetch()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 0 0 0
547 0 push @aValues, $value;
548 }
549 0 $sth->finish();
550 };
551 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
552 0 die $@;
553 } else {
554 0 return \@aValues;
555 }
556 }
557 0 ▶ sub fetch_singlefield {
Subroutine fetch_singlefield: not called CC=2 SCAR=17.9
558 0 my ($self,$statement,$dbh) = @_;
559 0 my $value;
560 0 eval {
561 0 my $sth = $dbh->prepare($statement);
562 0 $sth->execute();
563 0 $sth->bind_columns(undef, \$value);
564 0 $sth->fetch();
565 0 $sth->finish();
566 };
567 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
568 0 die $@;
569 } else {
570 0 return $value;
571 }
572 }
573 0 ▶ sub sql_fetcharray_singlerow {
Subroutine sql_fetcharray_singlerow: not called CC=2 SCAR=17.9
574 0 my ($self,$statement,$dbh) = @_;
575 0 my $aResult_Row;
576 0 eval {
577 0 my $sth = $dbh->prepare($statement);
578 0 $sth->execute();
579 0 $aResult_Row = $sth->fetchrow_arrayref();
580 0 $sth->finish();
581 };
582 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
583 0 die $@;
584 } else {
585 0 return $aResult_Row;
586 }
587 }
588 }
589  
590 1 1;
591  
592 __END__
↑ Summary Compare/Pg.pm »