| 1 | package DBIx::DataAudit; | ||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use parent 'Class::Accessor'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 3 | $VERSION = '0.13'; | |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | DBIx::DataAudit - summarize column data for a table | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | use DBIx::DataAudit; | ||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | warn "Running audit for table $table"; | ||||||||||||||||||||||||||||||||||
| 18 | my $audit = DBIx::DataAudit->audit( dsn => 'dbi:SQLite:dbname=test.sqlite', table => 'test' ); | ||||||||||||||||||||||||||||||||||
| 19 | print $audit->as_text; | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | # or | ||||||||||||||||||||||||||||||||||
| 22 | print $audit->as_html; | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | This module provides a summary about the data contained in a table. It provides | ||||||||||||||||||||||||||||||||||
| 25 | the descriptive statistics for every column. It's surprising | ||||||||||||||||||||||||||||||||||
| 26 | how much bad data you find by looking at the minimum and maximum | ||||||||||||||||||||||||||||||||||
| 27 | values of a column alone. | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | It tries to get the information in one table scan. | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | =head1 HOW IT WORKS | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | The module works by constructing an SQL statement that collects the information | ||||||||||||||||||||||||||||||||||
| 34 | about the columns in a single full table scan. | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | =head1 COLUMN TRAITS | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | You can specify which information is collected about every column by specifying the traits. | ||||||||||||||||||||||||||||||||||
| 39 | The hierarchy of traits is as follows: | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | any < ordered < numeric | ||||||||||||||||||||||||||||||||||
| 42 | < string | ||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | The following traits are collected for every column by default: | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | =over 4 | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | =item * C<total> | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | Number of rows in the column | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | =item * C<values> | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | Number of distinct values in the column | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | =item * C<null> | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | Number of C<NULL> values for the column | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | =back | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | For columns that are recognized as ordered, the following additional traits are collected: | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | =over 4 | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | =item * C<min> | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | Minimum value for the column | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | =item * C<max> | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | Maximum value for the column | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | =back | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | For columns that are recognized as numeric, the following additional traits are collected: | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | =over 4 | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | =item * C<avg> | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | Average value for the column | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | =back | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | For columns that are recognized as string, the following additional traits are collected: | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | =over 4 | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | =item * C<blank> | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | Number of values that consist only of blanks (C<chr 32>) | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | =item * C<empty> | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | Number of values that consist only of the empty string (C<''>) | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | =item * C<missing> | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | Number of values that consist only of the empty string (C<''>), | ||||||||||||||||||||||||||||||||||
| 101 | are blank (C<chr 32>) or are C<NULL> | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | =back | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | =head1 GLOBAL VARIABLES | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | To customize some default behaviour, the some global variables | ||||||||||||||||||||||||||||||||||
| 110 | are defined. Read the source to find their names. | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | use vars qw'@default_traits %trait_type %trait_hierarchy $trait_inapplicable %sql_type_map'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | 3 | @default_traits = qw[min max count values null avg blank empty missing ]; | |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 3 | %trait_type = ( | |||||||||||||||||||||||||||||||||
| 119 | count => ['any','count(%s)'], | ||||||||||||||||||||||||||||||||||
| 120 | values => ['any','count(distinct %s)'], | ||||||||||||||||||||||||||||||||||
| 121 | null => ['any','sum(case when %s is null then 1 else 0 end)'], | ||||||||||||||||||||||||||||||||||
| 122 | min => ['ordered','min(%s)'], | ||||||||||||||||||||||||||||||||||
| 123 | max => ['ordered','max(%s)'], | ||||||||||||||||||||||||||||||||||
| 124 | avg => ['numeric','avg(%s)'], | ||||||||||||||||||||||||||||||||||
| 125 | #modus => ['any','sum(1)group by %s'], # find the element that occurs the most | ||||||||||||||||||||||||||||||||||
| 126 | # Possibly with only a single table scan | ||||||||||||||||||||||||||||||||||
| 127 | blank => ['string',"sum(case when trim(%s)='' then 1 else 0 end)"], | ||||||||||||||||||||||||||||||||||
| 128 | empty => ['string',"sum(case when %s='' then 1 else 0 end)"], | ||||||||||||||||||||||||||||||||||
| 129 | missing => ['string',"sum(case when trim(%s)='' then 1 when %s is null then 1 else 0 end)"], | ||||||||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 3 | %trait_hierarchy = ( | |||||||||||||||||||||||||||||||||
| 133 | any => [], | ||||||||||||||||||||||||||||||||||
| 134 | ordered => ['any'], | ||||||||||||||||||||||||||||||||||
| 135 | numeric => ['ordered','any'], | ||||||||||||||||||||||||||||||||||
| 136 | string => ['ordered','any'], | ||||||||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | 3 | $trait_inapplicable = 'NULL'; | |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 3 | %sql_type_map = ( | |||||||||||||||||||||||||||||||||
| 142 | BIGINT => 'numeric', | ||||||||||||||||||||||||||||||||||
| 143 | BOOLEAN => 'any', | ||||||||||||||||||||||||||||||||||
| 144 | CHAR => 'string', | ||||||||||||||||||||||||||||||||||
| 145 | 'CHARACTER VARYING' => 'string', | ||||||||||||||||||||||||||||||||||
| 146 | DATETIME => 'ordered', | ||||||||||||||||||||||||||||||||||
| 147 | DATE => 'ordered', | ||||||||||||||||||||||||||||||||||
| 148 | DECIMAL => 'numeric', | ||||||||||||||||||||||||||||||||||
| 149 | ENUM => 'ordered', | ||||||||||||||||||||||||||||||||||
| 150 | INET => 'any', | ||||||||||||||||||||||||||||||||||
| 151 | INTEGER => 'numeric', | ||||||||||||||||||||||||||||||||||
| 152 | INT => 'numeric', | ||||||||||||||||||||||||||||||||||
| 153 | NUMERIC => 'numeric', | ||||||||||||||||||||||||||||||||||
| 154 | SMALLINT => 'numeric', | ||||||||||||||||||||||||||||||||||
| 155 | TEXT => 'string', | ||||||||||||||||||||||||||||||||||
| 156 | TIME => 'ordered', | ||||||||||||||||||||||||||||||||||
| 157 | 'TIMESTAMP WITHOUT TIME ZONE' => 'ordered', | ||||||||||||||||||||||||||||||||||
| 158 | TIMESTAMP => 'ordered', | ||||||||||||||||||||||||||||||||||
| 159 | TINYINT => 'numeric', | ||||||||||||||||||||||||||||||||||
| 160 | 'UNSIGNED BIGINT' => 'numeric', | ||||||||||||||||||||||||||||||||||
| 161 | VARCHAR => 'string', | ||||||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | The class implements the following methods: | ||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | 3 | __PACKAGE__->mk_accessors(qw(table dbh dsn columns traits results where)); | |||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | =head2 C<< __PACKAGE__->audit ARGS >> | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | Performs the data audit. Valid arguments are: | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | =over 4 | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | =item * C<table> | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | Name of the table to audit. No default. | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | =item * C<traits> | ||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | Array reference to the traits. Default traits are | ||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||
| 186 | min max count null avg blank empty missing | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | =item * C<columns> | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | Names of the columns to audit. Default are all columns of the table. | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | =item * C<dbh> | ||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||
| 194 | Database handle. If missing, hopefully you have specified the C<dsn>. | ||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||
| 196 | =item * C<dsn> | ||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | DSN to use. Can be omitted if you pass in a valid C<dbh> instead. | ||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | =item * C<column_info> | ||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||
| 202 | Column information, in the same format as the DBI returns it. | ||||||||||||||||||||||||||||||||||
| 203 | By default, this will be read in via DBI. | ||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub audit { | ||||||||||||||||||||||||||||||||
|
Subroutine
audit: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 210 | 0 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | $args{traits} ||= [ @default_traits ]; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"traits"} ||= [@DBIx::DataAudit::default_traits]
MC/DC0%0/1
$args{"traits"} ||= [@DBIx::DataAudit::default_traits]
$args{"traits"}
Truth table0%0/2
A
A= $args{"traits"}
| |||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if (! @{$args{traits}}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | 0 | $args{traits} = [ @default_traits ]; | |||||||||||||||||||||||||||||||||
| 215 | }; | ||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | $args{dbh} ||= DBI->connect( $args{dsn}, undef, undef, {RaiseError => 1}); | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$args{"dbh"} ||= "DBI"->connect($args{"dsn"}, undef, undef, {"RaiseError", 1})
MC/DC0%0/2
$args{"dbh"} ||= "DBI"->connect($args{"dsn"}, undef, undef, {"RaiseError", 1})
$args{"dbh"}
"DBI"->connect($args{"dsn"}, undef, undef, {"RaiseError", 1})
Truth table0%0/3
A ||= B
A= $args{"dbh"}B= "DBI"->connect($args{"dsn"}, undef, undef, {"RaiseError", 1})
| |||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||
| 218 | 0 | my $self = \%args; | |||||||||||||||||||||||||||||||||
| 219 | 0 | bless $self => $class; | |||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | $self->{columns} ||= [$self->get_columns]; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"columns"} ||= [$self->get_columns]
MC/DC0%0/1
$self->{"columns"} ||= [$self->get_columns]
$self->{"columns"}
Truth table0%0/2
A
A= $self->{"columns"}
| |||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | if (! @{ $self->{columns}}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 222 | 0 | croak "Couldn't retrieve column information for table '$args{table}'. Does your DBD implement ->column_info?"; | |||||||||||||||||||||||||||||||||
| 223 | }; | ||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | $self->{column_info} ||= $self->collect_column_info; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"column_info"} ||= $self->collect_column_info
MC/DC0%0/2
$self->{"column_info"} ||= $self->collect_column_info
$self->{"column_info"}
$self->collect_column_info
Truth table0%0/3
A ||= B
A= $self->{"column_info"}B= $self->collect_column_info
| |||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||
| 226 | 0 | $self | |||||||||||||||||||||||||||||||||
| 227 | }; | ||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||
| 229 | =head2 C<< $audit->as_text RESULTS >> | ||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||
| 231 | Returns a table drawn as text with the results. | ||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | sub as_text { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_text: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 236 | 0 | my ($self,$results) = @_; | |||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||
| 238 | 0 | require Text::Table; | |||||||||||||||||||||||||||||||||
| 239 | 0 | my $data = $self->template_data($results); | |||||||||||||||||||||||||||||||||
| 240 | 0 | my $table = Text::Table->new( @{$data->{headings}} ); | |||||||||||||||||||||||||||||||||
| 241 | 0 | $table->load( @{$data->{rows}} ); | |||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||
| 243 | 0 | "Data analysis for $data->{table}:\n\n" . $table->table; | |||||||||||||||||||||||||||||||||
| 244 | }; | ||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | =head2 C<< $audit->as_html RESULTS, TEMPLATE >> | ||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||
| 248 | Returns a HTML page with the results. | ||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||
| 250 | You can pass in a custom resultset or C<undef> if you want | ||||||||||||||||||||||||||||||||||
| 251 | the module to determine the results. | ||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||
| 253 | You can pass in a custom (L<Template|Template Toolkit>) template | ||||||||||||||||||||||||||||||||||
| 254 | if you want fancier rendering. | ||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||
| 256 | =cut | ||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | sub as_html { | ||||||||||||||||||||||||||||||||
|
Subroutine
as_html: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 259 | 0 | my ($self,$results,$template) = @_; | |||||||||||||||||||||||||||||||||
| 260 | 0 | require Template; | |||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | $template ||= <<TEMPLATE; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$template ||= qq[<html><head><title>Data audit of table '[% table %]'</title></head><body>\n<h2>Data audit of table '[% table %]'</h2>\n<table width="100%">\n<thead>\n<tr>[% FOR h IN headings %]<th>[%h%]</th>[%END%]</tr>\n</thead>\n<tbody>\n[% FOR r IN rows %]\n<tr>[% FOR v IN r %]<td>[%v FILTER html_entity%]</td>[%END%]</tr>\n[% END %]\n</tbody>\n</table>\n</html>\n]
MC/DC0%0/1
$template ||= qq[<html><head><title>Data audit of table '[% table %]'</title></head><body>\n<h2>Data audit of table '[% table %]'</h2>\n<table width="100%">\n<thead>\n<tr>[% FOR h IN headings %]<th>[%h%]</th>[%END%]</tr>\n</thead>\n<tbody>\n[% FOR r IN rows %]\n<tr>[% FOR v IN r %]<td>[%v FILTER html_entity%]</td>[%END%]</tr>\n[% END %]\n</tbody>\n</table>\n</html>\n]
$template
Truth table0%0/2
A
A= $template
| |||||||||||||||||||||||||||||||||||
| 262 | <html><head><title>Data audit of table '[% table %]'</title></head><body> | ||||||||||||||||||||||||||||||||||
| 263 | <h2>Data audit of table '[% table %]'</h2> | ||||||||||||||||||||||||||||||||||
| 264 | <table width="100%"> | ||||||||||||||||||||||||||||||||||
| 265 | <thead> | ||||||||||||||||||||||||||||||||||
| 266 | <tr>[% FOR h IN headings %]<th>[%h%]</th>[%END%]</tr> | ||||||||||||||||||||||||||||||||||
| 267 | </thead> | ||||||||||||||||||||||||||||||||||
| 268 | <tbody> | ||||||||||||||||||||||||||||||||||
| 269 | [% FOR r IN rows %] | ||||||||||||||||||||||||||||||||||
| 270 | <tr>[% FOR v IN r %]<td>[%v FILTER html_entity%]</td>[%END%]</tr> | ||||||||||||||||||||||||||||||||||
| 271 | [% END %] | ||||||||||||||||||||||||||||||||||
| 272 | </tbody> | ||||||||||||||||||||||||||||||||||
| 273 | </table> | ||||||||||||||||||||||||||||||||||
| 274 | </html> | ||||||||||||||||||||||||||||||||||
| 275 | TEMPLATE | ||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||
| 277 | 0 | my $t = Template->new(); | |||||||||||||||||||||||||||||||||
| 278 | 0 | my $data = $self->template_data($results); | |||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | $t->process(\$template,$data,\my $result) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 281 | || croak $t->error; | ||||||||||||||||||||||||||||||||||
| 282 | 0 | $result | |||||||||||||||||||||||||||||||||
| 283 | }; | ||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||
| 285 | =head2 C<< $audit->template_data >> | ||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||
| 287 | Returns a hash with the following three keys, suitable | ||||||||||||||||||||||||||||||||||
| 288 | for using with whatever templating system you have: | ||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||
| 290 | =over 4 | ||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | =item * | ||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||
| 294 | C<table> - the name of the table | ||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | =item * | ||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||
| 298 | C<headings> - the headings of the columns | ||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | =item * | ||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||
| 302 | C<rows> - the values of the traits of every column | ||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||
| 304 | =back | ||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||
| 306 | =cut | ||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | sub template_data { | ||||||||||||||||||||||||||||||||
|
Subroutine
template_data: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 309 | 0 | my ($self,$results) = @_; | |||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | $results ||= $self->{results} || $self->run_audit; | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$results ||= $self->{"results"} || $self->run_audit
$self->{'results'} || $self->run_audit
MC/DC0%0/3
$results ||= $self->{"results"} || $self->run_audit
$results
$self->{'results'}
$self->run_audit
Truth table0%0/4
A ||= B || C
A= $resultsB= $self->{'results'}C= $self->run_audit
| |||||||||||||||||||||||||||||||||||
| 311 | 0 | my @results = @{ $results->[0] }; | |||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||
| 313 | 0 | my @headings = (@{ $self->traits }); | |||||||||||||||||||||||||||||||||
| 314 | 0 | my @rows; | |||||||||||||||||||||||||||||||||
| 315 | 0 | for my $column (@{ $self->columns }) { | |||||||||||||||||||||||||||||||||
| 316 | 0 | my @row = $column; | |||||||||||||||||||||||||||||||||
| 317 | 0 | for my $trait (@headings) { | |||||||||||||||||||||||||||||||||
| 318 | 0 | my $val = shift @results; | |||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | if (defined $val) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | if (length($val) > 20) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 321 | 0 | $val = substr($val,0,20); | |||||||||||||||||||||||||||||||||
| 322 | }; | ||||||||||||||||||||||||||||||||||
| 323 | 0 | $val =~ s/[\x00-\x1f]/./g; | |||||||||||||||||||||||||||||||||
| 324 | }; | ||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | push @row, defined $val ? $val : 'n/a'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 326 | }; | ||||||||||||||||||||||||||||||||||
| 327 | 0 | push @rows, \@row; | |||||||||||||||||||||||||||||||||
| 328 | }; | ||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||
| 330 | 0 | my $res = { | |||||||||||||||||||||||||||||||||
| 331 | table => $self->table, | ||||||||||||||||||||||||||||||||||
| 332 | headings => ['column',@headings], | ||||||||||||||||||||||||||||||||||
| 333 | rows => \@rows, | ||||||||||||||||||||||||||||||||||
| 334 | }; | ||||||||||||||||||||||||||||||||||
| 335 | }; | ||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||
| 337 | =head2 C<< $audit->run_audit >> | ||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||
| 339 | Actually runs the SQL in the database. | ||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||
| 341 | =cut | ||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | sub run_audit { | ||||||||||||||||||||||||||||||||
|
Subroutine
run_audit: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 344 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||
| 346 | 0 | my $sql = $self->get_sql; | |||||||||||||||||||||||||||||||||
| 347 | 0 | $self->{results} = $self->dbh->selectall_arrayref($sql,{}); | |||||||||||||||||||||||||||||||||
| 348 | }; | ||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||
| 350 | =head2 C<< $audit->column_type COLUMN >> | ||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||
| 352 | Returns the type for the column. The four valid types are C<any>, C<ordered>, C<numeric> and C<string>. | ||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||
| 354 | =cut | ||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | sub column_type { | ||||||||||||||||||||||||||||||||
|
Subroutine
column_type: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 357 | 0 | my ($self,$column) = @_; | |||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if (! $self->{column_info}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 359 | 0 | $self->{column_info} = $self->collect_column_info; | |||||||||||||||||||||||||||||||||
| 360 | }; | ||||||||||||||||||||||||||||||||||
| 361 | 0 | my $info = $self->{column_info}; | |||||||||||||||||||||||||||||||||
| 362 | map { | ||||||||||||||||||||||||||||||||||
| 363 | 0 | $_->{trait_type}; | |||||||||||||||||||||||||||||||||
| 364 | 0 | } grep { $_->{COLUMN_NAME} eq $column } @$info; | |||||||||||||||||||||||||||||||||
| 365 | }; | ||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||
| 367 | =head2 C<< $audit->get_columns TABLE >> | ||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||
| 369 | Returns the names of the columns for the table C<TABLE>. | ||||||||||||||||||||||||||||||||||
| 370 | By default, the value of C<TABLE> will be taken from the value | ||||||||||||||||||||||||||||||||||
| 371 | passed to the constructor C<audit>. | ||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||
| 373 | =cut | ||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | sub get_columns { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_columns: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 376 | 0 | my ($self,$table) = @_; | |||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | $table ||= $self->table; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$table ||= $self->table
MC/DC0%0/2
$table ||= $self->table
$table
$self->table
Truth table0%0/3
A ||= B
A= $tableB= $self->table
| |||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | if (! $self->{column_info}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 379 | 0 | $self->{column_info} = $self->collect_column_info; | |||||||||||||||||||||||||||||||||
| 380 | }; | ||||||||||||||||||||||||||||||||||
| 381 | 0 | my $info = $self->{column_info}; | |||||||||||||||||||||||||||||||||
| 382 | 0 | my @sorted = @$info; | |||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||
| 384 | # Order the columns in the "right" order, if possible | ||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | if (exists $sorted[0]->{ORDINAL_POSITION} && defined $sorted[0]->{ORDINAL_POSITION}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $sorted[0]{"ORDINAL_POSITION"} and defined $sorted[0]{"ORDINAL_POSITION"}
MC/DC0%0/2
exists $sorted[0]{"ORDINAL_POSITION"} and defined $sorted[0]{"ORDINAL_POSITION"}
exists $sorted[0]{"ORDINAL_POSITION"}
defined $sorted[0]{"ORDINAL_POSITION"}
Truth table0%0/3
A and B
A= exists $sorted[0]{"ORDINAL_POSITION"}B= defined $sorted[0]{"ORDINAL_POSITION"}
| |||||||||||||||||||||||||||||||||||
| 386 | 0 | @sorted = sort { $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION} } @sorted; | |||||||||||||||||||||||||||||||||
| 387 | }; | ||||||||||||||||||||||||||||||||||
| 388 | map { | ||||||||||||||||||||||||||||||||||
| 389 | 0 | $_->{COLUMN_NAME}; | |||||||||||||||||||||||||||||||||
| 390 | } @sorted; | ||||||||||||||||||||||||||||||||||
| 391 | }; | ||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||
| 393 | =head2 C<< $audit->collect_column_info TABLE >> | ||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||
| 395 | Collects the information about the columns for the table C<TABLE> | ||||||||||||||||||||||||||||||||||
| 396 | from the DBI. By default, C<TABLE> will be taken from the | ||||||||||||||||||||||||||||||||||
| 397 | value passed to the constructor C<audit>. | ||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||
| 399 | If your database driver does not implement the C<< ->column_info >> | ||||||||||||||||||||||||||||||||||
| 400 | method you are out of luck. A fatal error is raised by this method | ||||||||||||||||||||||||||||||||||
| 401 | if C<< ->column_info >> does not return anything. | ||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||
| 403 | This method will raise warnings if it encounters a data type that | ||||||||||||||||||||||||||||||||||
| 404 | it doesn't know yet. You can either patch the | ||||||||||||||||||||||||||||||||||
| 405 | global variable C<%sql_type_map> to add the type or submit a patch | ||||||||||||||||||||||||||||||||||
| 406 | to me to add the type and its interpretation. | ||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||
| 408 | =cut | ||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | sub collect_column_info { | ||||||||||||||||||||||||||||||||
|
Subroutine
collect_column_info: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 411 | 0 | my ($self,$table) = @_; | |||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | $table ||= $self->table; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$table ||= $self->table
MC/DC0%0/2
$table ||= $self->table
$table
$self->table
Truth table0%0/3
A ||= B
A= $tableB= $self->table
| |||||||||||||||||||||||||||||||||||
| 413 | 0 | my $schema; | |||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | if ($table =~ s/^(.*)\.//) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 415 | 0 | $schema = $1; | |||||||||||||||||||||||||||||||||
| 416 | }; | ||||||||||||||||||||||||||||||||||
| 417 | 0 | my $sth = $self->dbh->column_info(undef,$schema,$table,undef); | |||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | if (! $sth) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 419 | 0 | ▶ | if( $schema ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 420 | 0 | $schema= "$schema."; | |||||||||||||||||||||||||||||||||
| 421 | } else { | ||||||||||||||||||||||||||||||||||
| 422 | 0 | $schema= ''; | |||||||||||||||||||||||||||||||||
| 423 | }; | ||||||||||||||||||||||||||||||||||
| 424 | 0 | croak "Couldn't collect column information for table '$schema$table'. Does your DBD implement ->column_info?"; | |||||||||||||||||||||||||||||||||
| 425 | }; | ||||||||||||||||||||||||||||||||||
| 426 | 0 | my $info = $sth->fetchall_arrayref({}); | |||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | if( !@$info ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 429 | 0 | croak "'$schema$table' seems to have no columns. Does your DBD implement ->column_info?"; | |||||||||||||||||||||||||||||||||
| 430 | }; | ||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||
| 432 | 0 | for my $i (@$info) { | |||||||||||||||||||||||||||||||||
| 433 | 0 | my $sqltype = uc $i->{TYPE_NAME}; | |||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||
| 435 | # Fix for Pg - convert enum types to "ENUM": | ||||||||||||||||||||||||||||||||||
| 436 | 0 | ▶ | if (exists $i->{pg_enum_values} && defined $i->{pg_enum_values}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $i->{"pg_enum_values"} and defined $i->{"pg_enum_values"}
MC/DC0%0/2
exists $i->{"pg_enum_values"} and defined $i->{"pg_enum_values"}
exists $i->{"pg_enum_values"}
defined $i->{"pg_enum_values"}
Truth table0%0/3
A and B
A= exists $i->{"pg_enum_values"}B= defined $i->{"pg_enum_values"}
| |||||||||||||||||||||||||||||||||||
| 437 | 0 | $sqltype = 'ENUM'; | |||||||||||||||||||||||||||||||||
| 438 | }; | ||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | if (not exists $sql_type_map{ $sqltype }) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 441 | warn sprintf q{Unknown SQL data type '%s' for column "%s.%s"; some traits will be unavailable\n}, | ||||||||||||||||||||||||||||||||||
| 442 | 0 | $sqltype, $table, $i->{COLUMN_NAME}; | |||||||||||||||||||||||||||||||||
| 443 | }; | ||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | $i->{trait_type} = $sql_type_map{ $sqltype } || 'any'; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$DBIx::DataAudit::sql_type_map{$sqltype} || 'any'
MC/DC0%0/1
$DBIx::DataAudit::sql_type_map{$sqltype} || 'any'
$DBIx::DataAudit::sql_type_map{$sqltype}
Truth table0%0/2
A
A= $DBIx::DataAudit::sql_type_map{$sqltype}
| |||||||||||||||||||||||||||||||||||
| 445 | }; | ||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||
| 447 | 0 | $info | |||||||||||||||||||||||||||||||||
| 448 | }; | ||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||
| 450 | =head2 C<< $audit->get_sql TABLE >> | ||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||
| 452 | Creates the SQL statement to collect the information. | ||||||||||||||||||||||||||||||||||
| 453 | The default value for C<TABLE> will be the table passed | ||||||||||||||||||||||||||||||||||
| 454 | to the constructor C<audit>. | ||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||
| 456 | If you encounter errors from your SQL engine, you may want | ||||||||||||||||||||||||||||||||||
| 457 | to print the result of this method out. | ||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||
| 459 | =cut | ||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||
| 461 | 0 | ▶ | sub get_sql { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_sql: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 462 | 0 | my ($self,$table) = @_; | |||||||||||||||||||||||||||||||||
| 463 | 0 | ▶ | $table ||= $self->table; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$table ||= $self->table
MC/DC0%0/2
$table ||= $self->table
$table
$self->table
Truth table0%0/3
A ||= B
A= $tableB= $self->table
| |||||||||||||||||||||||||||||||||||
| 464 | 0 | my @columns = @{ $self->columns }; | |||||||||||||||||||||||||||||||||
| 465 | 0 | my @traits = @{$self->traits}; | |||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||
| 467 | 0 | my @resultset; | |||||||||||||||||||||||||||||||||
| 468 | 0 | for my $column (@columns) { | |||||||||||||||||||||||||||||||||
| 469 | 0 | for my $trait (@traits) { | |||||||||||||||||||||||||||||||||
| 470 | 0 | my $name = "${column}_${trait}"; | |||||||||||||||||||||||||||||||||
| 471 | 0 | $name =~ s/"//g; # unquote quoted columns | |||||||||||||||||||||||||||||||||
| 472 | 0 | ▶ | if ($self->trait_applies( $trait, $column )) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 473 | 0 | my $tmpl = $trait_type{$trait}->[1]; | |||||||||||||||||||||||||||||||||
| 474 | 0 | $tmpl =~ s/%s/$column/g; | |||||||||||||||||||||||||||||||||
| 475 | 0 | push @resultset, "$tmpl as $name"; | |||||||||||||||||||||||||||||||||
| 476 | } else { | ||||||||||||||||||||||||||||||||||
| 477 | 0 | push @resultset, "NULL as $name"; | |||||||||||||||||||||||||||||||||
| 478 | }; | ||||||||||||||||||||||||||||||||||
| 479 | }; | ||||||||||||||||||||||||||||||||||
| 480 | }; | ||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | my $where = $self->where ? "WHERE " . $self->where : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 482 | 0 | my $statement = sprintf "SELECT %s FROM %s\n%s", join("\n ,", @resultset), $table, $where; | |||||||||||||||||||||||||||||||||
| 483 | 0 | return $statement | |||||||||||||||||||||||||||||||||
| 484 | }; | ||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||
| 486 | =head2 C<< $audit->trait_applies TRAIT, COLUMN >> | ||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||
| 488 | Checks whether a trait applies to a column. | ||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||
| 490 | A trait applies to a column if the trait type is C<any> | ||||||||||||||||||||||||||||||||||
| 491 | or if it is the same type as the column type as returned | ||||||||||||||||||||||||||||||||||
| 492 | by C<get_column_type>. | ||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||
| 494 | The method will raise an error if it is passed an unknown | ||||||||||||||||||||||||||||||||||
| 495 | trait name. See the source code for how to add custom | ||||||||||||||||||||||||||||||||||
| 496 | traits. | ||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||
| 498 | =cut | ||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||
| 500 | 36 | ▶ | sub trait_applies { | ||||||||||||||||||||||||||||||||
|
Subroutine
trait_applies: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 501 | 36 | my ($self, $trait, $column) = @_; | |||||||||||||||||||||||||||||||||
| 502 | 36 | ▶ | if (not exists $trait_type{$trait}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 503 | 0 | carp "Unknown trait '$trait'"; | |||||||||||||||||||||||||||||||||
| 504 | }; | ||||||||||||||||||||||||||||||||||
| 505 | 36 | ▶ | my $trait_type = $trait_type{$trait}->[0] || ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$DBIx::DataAudit::trait_type{$trait}[0] || ''
MC/DC0%0/1
$DBIx::DataAudit::trait_type{$trait}[0] || ''
$DBIx::DataAudit::trait_type{$trait}[0]
Truth table50%1/2
A
A= $DBIx::DataAudit::trait_type{$trait}[0]
| |||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||
| 507 | 36 | ▶ | return 1 if ($trait_type eq 'any'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||
| 509 | 24 | (my $type) = $self->column_type($column); | |||||||||||||||||||||||||||||||||
| 510 | 24 | my @subtypes = @{ $trait_hierarchy{ $type } }; | |||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||
| 512 | 24 | return scalar grep { $trait_type eq $_ } ($type,@subtypes); | |||||||||||||||||||||||||||||||||
| 513 | }; | ||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||
| 515 | =head1 COMMAND LINE USAGE | ||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||
| 517 | You can use this mail from the command line if you need a quick check of data: | ||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||
| 519 | perl -MDBIx::DataAudit=dbi:SQLite:dbname=some/db.sqlite my_table [traits] | ||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||
| 521 | This could also incredibly useful if you want a breakdown of a csv-file: | ||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||
| 523 | perl -MDBIx::DataAudit=dbi:AnyData:dbname=some/db.sqlite my_table [traits] | ||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||
| 525 | Unfortunately, that does not work yet, as I haven't found a convenient | ||||||||||||||||||||||||||||||||||
| 526 | oneliner way to make a CSV file appear as database. | ||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||
| 528 | =cut | ||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||
| 530 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=5 SCAR=26.7
| |||||||||||||||||||||||||||||||||||
| 531 | 2 | my ($class, $dsn) = @_; | |||||||||||||||||||||||||||||||||
| 532 | 2 | (my $target) = caller; | |||||||||||||||||||||||||||||||||
| 533 | 2 | ▶ | if ($target eq 'main' and $dsn) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$target eq "main" and $dsn
MC/DC0%0/2
$target eq "main" and $dsn
$target eq "main"
$dsn
Truth table33%1/3
A and B
A= $target eq "main"B= $dsn
| |||||||||||||||||||||||||||||||||||
| 534 | 0 | my ($table,@traits) = @ARGV; | |||||||||||||||||||||||||||||||||
| 535 | 0 | my @tables = split /,/,$table; | |||||||||||||||||||||||||||||||||
| 536 | 0 | ▶ | if (! @traits) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 537 | 0 | @traits = @default_traits; | |||||||||||||||||||||||||||||||||
| 538 | }; | ||||||||||||||||||||||||||||||||||
| 539 | 0 | for my $table (@tables) { | |||||||||||||||||||||||||||||||||
| 540 | 0 | my $self = $class->audit(dsn => $dsn, table => $table, traits => \@traits); | |||||||||||||||||||||||||||||||||
| 541 | 0 | print "Data audit for table '$table'\n\n"; | |||||||||||||||||||||||||||||||||
| 542 | 0 | print $self->as_text; | |||||||||||||||||||||||||||||||||
| 543 | }; | ||||||||||||||||||||||||||||||||||
| 544 | }; | ||||||||||||||||||||||||||||||||||
| 545 | }; | ||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||
| 547 | 3 | 1; | |||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||
| 549 | __END__ | ||||||||||||||||||||||||||||||||||