DBR/Config/ScanDB.pm

filter: Statementstmt 94.3% 117 / 124 Branchbran 53.7% 43 / 80 Conditioncond 58.8% 10 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 7 totaltotal 73.7% 183 / 248 CC 52 SCAR 28.0
CC
52
Cov
77%
update_fields
36.1
scan
9.0
scan_pkeys
7.3
CRAP 16.4
« DBR/Util/Logger.pm ↑ Summary DBR/Config.pm »
1 # the contents of this file are Copyright (c) 2009 Daniel Norman
2 # This program is free software; you can redistribute it and/or
3 # modify it under the terms of the GNU General Public License as
4 # published by the Free Software Foundation.
5  
6 package DBR::Config::ScanDB;
7  
8 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use base 'DBR::Common';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use DBR::Config::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use DBR::Config::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod undocumented
14 17 my( $package ) = shift;
15 17 my %params = @_;
16 my $self = {
17 session => $params{session},
18 conf_instance => $params{conf_instance},
19 scan_instance => $params{scan_instance},
20 17 };
21  
22 17 bless( $self, $package );
23  
24 17 ▶ return $self->_error('session object must be specified') unless $self->{session};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"session"} 0 17 17
25 17 ▶ return $self->_error('conf_instance object must be specified') unless $self->{conf_instance};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"conf_instance"} 0 17 17
26 17 ▶ return $self->_error('scan_instance object must be specified') unless $self->{scan_instance};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"scan_instance"} 0 17 17
27  
28 $self->{schema_id} = $self->{scan_instance}->schema_id or
29 17 ▶ return $self->_error('Cannot scan an instance that has no schema');
Branch: 1 branch
BranchTrueFalseTotal
$self->{"schema_id"} = $self->{"scan_instance"}->schema_id or return $self->_error("Cannot scan an instance that has no schema") 17 0 17
30  
31 17 return( $self );
32 }
33  
34 17 ▶sub scan{
Subroutine scan: called CC=8 SCAR=22.0
Pod undocumented
35 17 my $self = shift;
36 17 my %params = @_;
37  
38 17 ▶ my $tables = $self->scan_tables() || die "failed to scan tables";
Condition50%1/2
$self->scan_tables || die('failed to scan tables')
l!l
170
MC/DC0%0/1
$self->scan_tables || die('failed to scan tables')
$self->scan_tables
Truth table50%1/2
A
A= $self->scan_tables
Aresult
11
00
39 17 my $pkeys = $self->scan_pkeys(); # || die "failed to scan primary keys";
40  
41 17 foreach my $table (@{$tables}){
42 41 ▶ print "Scanning $table\n" if $params{pretty};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"pretty"} 0 41 41
43
44 41 ▶ my $fields = $self->scan_fields($table) or return $self->_error( "failed to describe table" );
Branch: 1 branch
BranchTrueFalseTotal
my $fields = $self->scan_fields($table) or return $self->_error("failed to describe table") 41 0 41
45 41 ▶ my $pkey = $pkeys ? $pkeys->{$table} : $self->scan_pkeys( $table );
Branch: 1 branch
BranchTrueFalseTotal
$pkeys ? : 41 0 41
46  
47 41 ▶ $self->update_table($fields,$table,$pkey) or return $self->_error("failed to update table");
Branch: 1 branch
BranchTrueFalseTotal
$self->update_table($fields, $table, $pkey) or return $self->_error("failed to update table") 41 0 41
48 }
49  
50 #HACK - the scanner should load up the in-memory representation at the same time
51 DBR::Config::Schema->load(
52 session => $self->{session},
53 schema_id => $self->{schema_id},
54 instance => $self->{conf_instance},
55 17 ▶ ) or die "Failed to reload schema";
Branch: 1 branch
BranchTrueFalseTotal
"DBR::Config::Schema"->load("session", $self->{"session"}, "schema_id", $self->{"schema_id"}, "instance", $self->{"conf_instance"}) or die "Failed to reload schema" 17 0 17
56  
57 17 return 1;
58 }
59  
60  
61 17 ▶sub scan_pkeys {
Subroutine scan_pkeys: called CC=7 SCAR=19.8
Pod undocumented
62 17 my $self = shift;
63 17 my $table = shift; # undef for all
64 #print "SCAN_PKEYS called with table=[$table]\n";
65  
66 17 ▶ my $dbh = $self->{scan_instance}->connect('dbh') || die "failed to connect to scanned db";
Condition50%1/2
$self->{'scan_instance'}->connect('dbh') || die('failed to connect to scanned db')
l!l
170
MC/DC0%0/1
$self->{'scan_instance'}->connect('dbh') || die('failed to connect to scanned db')
$self->{'scan_instance'}->connect('dbh')
Truth table50%1/2
A
A= $self->{'scan_instance'}->connect('dbh')
Aresult
11
00
67  
68 17 my $sth;
69 17 local $dbh->{PrintError} = 0;
70 17 local $^W = 0;
71 17 eval { $sth= $dbh->primary_key_info(undef,undef,$table); };
72 17 ▶ return $self->_error('failed call to primary_key_info') unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 17 17
73
74 17 my %map = ();
75 17 ▶ while (my $row = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 41 17 58
76 41 ▶ next unless $row->{PK_NAME} eq 'PRIMARY KEY';
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"PK_NAME"} eq "PRIMARY KEY" 0 41 41
77 41 ▶ my $table = $row->{TABLE_NAME} or return $self->_error('no TABLE_NAME!');
Branch: 1 branch
BranchTrueFalseTotal
my $table = $row->{"TABLE_NAME"} or return $self->_error("no TABLE_NAME!") 41 0 41
78 41 ▶ my $field = $row->{COLUMN_NAME} or return $self->_error('no COLUMN_NAME!');
Branch: 1 branch
BranchTrueFalseTotal
my $field = $row->{"COLUMN_NAME"} or return $self->_error("no COLUMN_NAME!") 41 0 41
79 41 $map{$table}->{$field} = 1;
80 }
81  
82 17 $sth->finish();
83  
84 17 return \%map;
85 }
86  
87 17 ▶sub scan_tables{
Subroutine scan_tables: called CC=7 SCAR=19.6
Pod undocumented
88 17 my $self = shift;
89 17 ▶ my $dbh = $self->{scan_instance}->connect('dbh') || die "failed to connect to scanned db";
Condition50%1/2
$self->{'scan_instance'}->connect('dbh') || die('failed to connect to scanned db')
l!l
170
MC/DC0%0/1
$self->{'scan_instance'}->connect('dbh') || die('failed to connect to scanned db')
$self->{'scan_instance'}->connect('dbh')
Truth table50%1/2
A
A= $self->{'scan_instance'}->connect('dbh')
Aresult
11
00
90  
91 17 ▶ return $self->_error('failed call to table_info') unless
Branch: 1 branch
BranchTrueFalseTotal
unless my $sth = $dbh->table_info 0 17 17
92 my $sth = $dbh->table_info;
93  
94 17 my @tables;
95 17 ▶ while (my $row = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 90 17 107
96 90 ▶ my $name = $row->{TABLE_NAME} or return $self->_error('Table entry has no name!');
Branch: 1 branch
BranchTrueFalseTotal
my $name = $row->{"TABLE_NAME"} or return $self->_error("Table entry has no name!") 90 0 90
97  
98 90 ▶ next if ($name eq 'sqlite_sequence'); # Ugly
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "sqlite_sequence" 15 75 90
99  
100 75 ▶ if($row->{TABLE_TYPE} eq 'TABLE'){
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"TABLE_TYPE"} eq "TABLE") 41 34 75
101 41 push @tables, $name;
102 }
103 }
104  
105 17 $sth->finish();
106  
107 17 return \@tables;
108 }
109  
110 41 ▶sub scan_fields{
Subroutine scan_fields: called CC=4 SCAR=14.0
Pod undocumented
111 41 my $self = shift;
112 41 my $table = shift;
113  
114 41 ▶ my $dbh = $self->{scan_instance}->connect('dbh') || die "failed to connect to scanned db";
Condition50%1/2
$self->{'scan_instance'}->connect('dbh') || die('failed to connect to scanned db')
l!l
410
MC/DC0%0/1
$self->{'scan_instance'}->connect('dbh') || die('failed to connect to scanned db')
$self->{'scan_instance'}->connect('dbh')
Truth table50%1/2
A
A= $self->{'scan_instance'}->connect('dbh')
Aresult
11
00
115  
116 41 ▶ return $self->_error('failed call to column_info') unless
Branch: 1 branch
BranchTrueFalseTotal
unless my $sth = $dbh->column_info(undef, undef, $table, undef) 0 41 41
117 my $sth = $dbh->column_info( undef, undef, $table, undef );
118  
119 41 my @rows;
120 41 ▶ while (my $row = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 172 41 213
121 172 push @rows, $row;
122 }
123  
124 41 $sth->finish();
125  
126 41 return \@rows;
127 }
128  
129 41 ▶sub update_table{
Subroutine update_table: called CC=6 SCAR=18.9
Pod undocumented
130 41 my $self = shift;
131 41 my $fields = shift;
132 41 my $name = shift;
133 41 my $pkey = shift;
134  
135 41 ▶ my $dbh = $self->{conf_instance}->connect || die "failed to connect to config db";
Condition50%1/2
$self->{'conf_instance'}->connect || die('failed to connect to config db')
l!l
410
MC/DC0%0/1
$self->{'conf_instance'}->connect || die('failed to connect to config db')
$self->{'conf_instance'}->connect
Truth table50%1/2
A
A= $self->{'conf_instance'}->connect
Aresult
11
00
136  
137 return $self->_error('failed to select from dbr_tables') unless
138 my $tables = $dbh->select(
139 -table => 'dbr_tables',
140 -fields => 'table_id schema_id name',
141 -where => {
142 41 ▶ schema_id => ['d',$self->{schema_id}],
Branch: 1 branch
BranchTrueFalseTotal
unless my $tables = $dbh->select("-table", "dbr_tables", "-fields", "table_id schema_id name", "-where", {"schema_id", ["d", $self->{"schema_id"}], "name", $name}) 0 41 41
143 name => $name,
144 }
145 );
146  
147 41 my $table = $tables->[0];
148  
149 41 my $table_id;
150 41 ▶ if($table){ # update
Branch: 1 branch
BranchTrueFalseTotal
if ($table) { } 0 41 41
151 0 $table_id = $table->{table_id};
152 }else{
153 return $self->_error('failed to insert into dbr_tables') unless
154 $table_id = $dbh->insert(
155 -table => 'dbr_tables',
156 -fields => {
157 41 ▶ schema_id => ['d',$self->{schema_id}],
Branch: 1 branch
BranchTrueFalseTotal
unless $table_id = $dbh->insert("-table", "dbr_tables", "-fields", {"schema_id", ["d", $self->{"schema_id"}], "name", $name}) 0 41 41
158 name => $name,
159 }
160 );
161 }
162  
163 41 ▶ $self->update_fields($fields,$table_id,$pkey) or return $self->_error('Failed to update fields');
Branch: 1 branch
BranchTrueFalseTotal
$self->update_fields($fields, $table_id, $pkey) or return $self->_error("Failed to update fields") 41 0 41
164  
165 41 return 1;
166 }
167  
168  
169 41 ▶sub update_fields{
Subroutine update_fields: called CC=21 SCAR=35.9
Pod undocumented
170 41 my $self = shift;
171 41 my $fields = shift;
172 41 my $table_id = shift;
173 41 my $pkey_map = shift;
174  
175 41 ▶ my $dbh = $self->{conf_instance}->connect || die "failed to connect to config db";
Condition50%1/2
$self->{'conf_instance'}->connect || die('failed to connect to config db')
l!l
410
MC/DC0%0/1
$self->{'conf_instance'}->connect || die('failed to connect to config db')
$self->{'conf_instance'}->connect
Truth table50%1/2
A
A= $self->{'conf_instance'}->connect
Aresult
11
00
176  
177 41 ▶ return $self->_error('failed to select from dbr_fields') unless
Branch: 1 branch
BranchTrueFalseTotal
unless my $records = $dbh->select("-table", "dbr_fields", "-fields", "field_id table_id name data_type is_nullable is_signed max_value", "-where", {"table_id", ["d", $table_id]}) 0 41 41
178 my $records = $dbh->select(
179 -table => 'dbr_fields',
180 -fields => 'field_id table_id name data_type is_nullable is_signed max_value',
181 -where => {
182 table_id => ['d',$table_id]
183 }
184 );
185  
186 41 my %fieldmap;
187 0 map {$fieldmap{$_->{name}} = $_} @{$records};
188  
189 41 foreach my $field (@{$fields}) {
190 172 ▶ my $name = $field->{'COLUMN_NAME'} or return $self->_error('No COLUMN_NAME is present');
Branch: 1 branch
BranchTrueFalseTotal
my $name = $field->{"COLUMN_NAME"} or return $self->_error("No COLUMN_NAME is present") 172 0 172
191 172 ▶ my $type = $field->{'TYPE_NAME'} or return $self->_error('No TYPE_NAME is present' );
Branch: 1 branch
BranchTrueFalseTotal
my $type = $field->{"TYPE_NAME"} or return $self->_error("No TYPE_NAME is present") 172 0 172
192 172 my $size = $field->{'COLUMN_SIZE'};
193  
194 172 my $nullable = $field->{'NULLABLE'};
195 172 ▶ return $self->_error('No NULLABLE is present' ) unless defined($nullable);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nullable 0 172 172
196  
197 172 ▶ my $pkey = $field->{'mysql_is_pri_key'} || $pkey_map->{$name};
Condition66%2/3
$field->{'mysql_is_pri_key'} || $pkey_map->{$name}
l!l&&r!l&&!r
041131
MC/DC50%1/2
$field->{'mysql_is_pri_key'} || $pkey_map->{$name}
$field->{'mysql_is_pri_key'} $pkey_map->{$name}
Truth table66%2/3
A || B
A= $field->{'mysql_is_pri_key'}B= $pkey_map->{$name}
ABresult
1X1
011
000
198 172 my $extra = $field->{'mysql_type_name'};
199  
200 172 my $is_signed = 0;
201 172 ▶ if(defined $extra){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extra) 0 172 172
202 0 ▶ $is_signed = ($extra =~ / unsigned/i)?0:1;
Branch: 1 branch
BranchTrueFalseTotal
$extra =~ / unsigned/i ? : 0 0 0
203 0 ▶ $is_signed = 0 if $type =~ /unsigned/i; #Lame... SQLite sometimes returns the data type as 'int unsigned'
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /unsigned/i 0 0 0
204 }
205 172 $type =~ /^\s+|\s+$/g;
206 172 ($type) = split (/\s+/,$type);
207 172 ▶ my $typeid = DBR::Config::Field->get_type_id($type) or die( "Invalid type '$type'" );
Branch: 1 branch
BranchTrueFalseTotal
my $typeid = "DBR::Config::Field"->get_type_id($type) or die "Invalid type '${type}'" 172 0 172
208  
209 172 my $record = $fieldmap{$name};
210  
211 172 ▶ my $ref = {
Branch: 1 branch
BranchTrueFalseTotal
$nullable ? : 108 64 172
Condition100%2/2
$size || 0
l!l
38134
MC/DC100%1/1
$size || 0
$size
Truth table100%2/2
A
A= $size
Aresult
11
00
212 is_nullable => ['d', $nullable ? 1:0 ],
213 is_signed => ['d', $is_signed ],
214 data_type => ['d', $typeid ],
215 max_value => ['d', $size || 0 ],
216 };
217  
218 172 ▶ if(defined($pkey)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pkey) 41 131 172
219 41 ▶ $ref->{is_pkey} = ['d', $pkey ? 1:0 ],
Branch: 1 branch
BranchTrueFalseTotal
$pkey ? : 41 0 41
220 }
221  
222 172 ▶ if ($record) { # update
Branch: 1 branch
BranchTrueFalseTotal
if ($record) { } 0 172 172
223 return $self->_error('failed to insert into dbr_tables') unless
224 $dbh->update(
225 -table => 'dbr_fields',
226 -fields => $ref,
227 0 ▶ -where => { field_id => ['d',$record->{field_id}] },
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->update("-table", "dbr_fields", "-fields", $ref, "-where", {"field_id", ["d", $record->{"field_id"}]}) 0 0 0
228 );
229 } else {
230 172 $ref->{name} = $name;
231 172 $ref->{table_id} = ['d', $table_id ];
232  
233 172 ▶ return $self->_error('failed to insert into dbr_tables') unless
Branch: 1 branch
BranchTrueFalseTotal
unless my $field_id = $dbh->insert("-table", "dbr_fields", "-fields", $ref) 0 172 172
234 my $field_id = $dbh->insert(
235 -table => 'dbr_fields',
236 -fields => $ref,
237 );
238 }
239  
240 172 delete $fieldmap{$name};
241  
242 }
243  
244 41 foreach my $name (keys %fieldmap) {
245 0 my $record = $fieldmap{$name};
246  
247 return $self->_error('failed to delete from dbr_tables') unless
248 $dbh->delete(
249 -table => 'dbr_fields',
250 0 ▶ -where => { field_id => ['d',$record->{field_id}] },
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->delete("-table", "dbr_fields", "-where", {"field_id", ["d", $record->{"field_id"}]}) 0 0 0
251 );
252 }
253  
254 41 return 1;
255 }
256  
257 18 1;
« DBR/Util/Logger.pm ↑ Summary DBR/Config.pm »