DBIx-DataAudit

 view release on metacpan or  search on metacpan

lib/DBIx/DataAudit.pm  view on Meta::CPAN

    TINYINT   => 'numeric',
    'UNSIGNED BIGINT'    => 'numeric',
    VARCHAR   => 'string',
);

=head1 METHODS

The class implements the following methods:

=cut

__PACKAGE__->mk_accessors(qw(table dbh dsn columns traits results where));

=head2 C<< __PACKAGE__->audit ARGS >>

Performs the data audit. Valid arguments are:

=over 4

=item * C<table>

Name of the table to audit. No default.

=item * C<traits>

Array reference to the traits. Default traits are

  min max count null avg blank empty missing

=item * C<columns>

Names of the columns to audit. Default are all columns of the table.

=item * C<dbh>

Database handle. If missing, hopefully you have specified the C<dsn>.

=item * C<dsn>

DSN to use. Can be omitted if you pass in a valid C<dbh> instead.

=item * C<column_info>

Column information, in the same format as the DBI returns it.
By default, this will be read in via DBI.

=back

=cut

sub audit {
    my ($class, %args) = @_;

    $args{traits} ||= [ @default_traits ];
    if (! @{$args{traits}}) {
        $args{traits} = [ @default_traits ];
    };
    $args{dbh}    ||= DBI->connect( $args{dsn}, undef, undef, {RaiseError => 1});

    my $self = \%args;
    bless $self => $class;
    $self->{columns} ||= [$self->get_columns];
    if (! @{ $self->{columns}}) {
        croak "Couldn't retrieve column information for table '$args{table}'. Does your DBD implement ->column_info?";
    };
    $self->{column_info} ||= $self->collect_column_info;

    $self
};

=head2 C<< $audit->as_text RESULTS >>

Returns a table drawn as text with the results.

=cut

sub as_text {
    my ($self,$results) = @_;

    require Text::Table;
    my $data = $self->template_data($results);
    my $table = Text::Table->new( @{$data->{headings}} );
    $table->load( @{$data->{rows}} );

    "Data analysis for $data->{table}:\n\n" . $table->table;
};

=head2 C<< $audit->as_html RESULTS, TEMPLATE >>

Returns a HTML page with the results.

You can pass in a custom resultset or C<undef> if you want
the module to determine the results.

You can pass in a custom (L<Template|Template Toolkit>) template
if you want fancier rendering.

=cut

sub as_html {
    my ($self,$results,$template) = @_;
    require Template;
    $template ||= <<TEMPLATE;
<html><head><title>Data audit of table '[% table %]'</title></head><body>
<h2>Data audit of table '[% table %]'</h2>
<table width="100%">
<thead>
<tr>[% FOR h IN headings %]<th>[%h%]</th>[%END%]</tr>
</thead>
<tbody>
[% FOR r IN rows %]
<tr>[% FOR v IN r %]<td>[%v FILTER html_entity%]</td>[%END%]</tr>
[% END %]
</tbody>
</table>
</html>
TEMPLATE

    my $t = Template->new();
    my $data = $self->template_data($results);



( run in 1.948 second using v1.01-cache-2.11-cpan-5a3173703d6 )