DBIx-Class-AuditLog

 view release on metacpan or  search on metacpan

lib/DBIx/Class/AuditLog.pm  view on Meta::CPAN

package DBIx::Class::AuditLog;
$DBIx::Class::AuditLog::VERSION = '0.6.4';
use base qw/DBIx::Class/;

use strict;
use warnings;

# local $DBIx::Class::AuditLog::enabled = 0;
# can be set to temporarily disable audit logging
our $enabled = 1;

sub insert {
    my $self = shift;

    return $self->next::method(@_) if !$enabled || $self->in_storage;

    my $result = $self->next::method(@_);

    my ( $action, $table ) = $self->_action_setup( $result, 'insert' );

    if ($action) {
        my %column_data = $result->get_columns;
        $self->_store_changes( $action, $table, {}, \%column_data );
    }

    return $result;
}

sub update {
    my $self = shift;

    return $self->next::method(@_) if !$enabled;

    my $stored_row      = $self->get_from_storage;
    my %new_data        = $self->get_columns;
    my @changed_columns = keys %{ $_[0] || {} };

    my $result = $self->next::method(@_);

    return unless $stored_row; # update on deleted row - nothing to log

    my %old_data = $stored_row->get_columns;

    if (@changed_columns) {
        @new_data{@changed_columns} = map $self->get_column($_),
            @changed_columns;
    }

    foreach my $col ( $self->columns ) {
        if ( $self->_force_audit($col) ) {
            $old_data{$col} = $stored_row->get_column($col)
                unless defined $old_data{$col};
            $new_data{$col} = $self->get_column($col)
                unless defined $new_data{$col};
        }
    }

    # remove unwanted columns
    foreach my $key ( keys %new_data ) {
        next if $self->_force_audit($key);    # skip forced cols
        if (   defined $old_data{$key}
            && defined $new_data{$key}
            && $old_data{$key} eq $new_data{$key}
            || !defined $old_data{$key} && !defined $new_data{$key} )
        {
            delete $new_data{$key};           # remove unchanged cols
        }
    }

    if ( keys %new_data ) {
        my ( $action, $table )
            = $self->_action_setup( $stored_row, 'update' );

        if ($action) {
            $self->_store_changes( $action, $table, \%old_data, \%new_data );
        }
    }

    return $result;
}

sub delete {
    my $self = shift;

    return $self->next::method(@_) if !$enabled;

    my $stored_row = $self->get_from_storage;

    my $result = $self->next::method(@_);



( run in 2.618 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )