App::Hachero::Plugin::Output::DBIC - writes results to databases via DBIx::Class


App-Hachero documentation Contained in the App-Hachero distribution.

Index


Code Index:

NAME

Top

App::Hachero::Plugin::Output::DBIC - writes results to databases via DBIx::Class

SYNOPSYS

Top

  ---
  plugins:
    - module: Output::DBIC
      config:
        update_mode: [count_up|overwrite]
        connect_info:
            - dbi:mysql:dbhost=db.local;dbname=logdb
            - your_name
            - your_password




DESCRIPTION

Top

writes results to databases via DBIx::Class

implemented hooks

* output

AUTHOR

Top

Takaaki Mizuno <cpan@takaaki.info>

Nobuo Danjou <nobuo.danjou@gmail.com>

SEE ALSO

Top

App::Hachero

DBIx::Class::Schema::Loader


App-Hachero documentation Contained in the App-Hachero distribution.

package App::Hachero::Plugin::Output::DBIC;
use strict;
use warnings;
use base qw(App::Hachero::Plugin::Base);

sub output : Hook {
    my ($self, $context, $args) = @_;
    my $schema = App::Hachero::Plugin::Output::DBIC::Schema
        ->connect(@{$self->config->{config}->{connect_info}});
    my $update_mode = $self->config->{config}->{update_mode} || 'update';
    unless ($schema) {
        $context->log(error => "connection error");
        return;
    }
    for my $key (keys %{$context->result}) {
        (my $table = $key) =~ s/\:\://g;
        my $rs = eval {$schema->resultset(ucfirst $table)};
        if ($@) {
            $context->log(error => $!);
            next;
        }
        if ($rs) {
            my $result = $context->result->{$key};
            for my $data ($result->values) {
                eval {
                    my $hashref = $data->hashref;
                    if ( $update_mode eq 'count_up' ) {
                        my $count = delete $hashref->{count};
                        my $rec = $rs->find_or_create($hashref);
                        $rec->update({count => ($rec->count || 0) + $count});
                    } else {
                        $rs->update_or_create($hashref)
                    }
                };
                if ($@) {
                    $context->log(error => $!);
                }
            }
        } else {
            $context->log(error => "$table not found");
        }
    }
}

package # hide from PAUSE
    App::Hachero::Plugin::Output::DBIC::Schema;
use base qw(DBIx::Class::Schema::Loader);

__PACKAGE__->load_classes;

1;
__END__