App-OverWatch

 view release on metacpan or  search on metacpan

lib/App/OverWatch/DB.pm  view on Meta::CPAN

package App::OverWatch::DB;
# ABSTRACT: Database connector base class

use strict;
use warnings;
use utf8;

use App::OverWatch::Config;

use DBIx::Connector;
use Try::Tiny;
use Module::Load qw( load );

sub new {
    my $class = shift;
    my $Config = shift || die "Error: require 'Config' arg";

    my $type = $Config->db_type();

    my $subclass = "$class::$type";
    load($subclass);

    my $self = bless( {}, $subclass );

    $self->{Config} = $Config;

    return $self;
}

sub type {
    my $self = shift;
    return $self->{Config}->db_type();
}

sub dbix_run {
    my ($self, $sql, @bind_values) = @_;

    my $conn = $self->_dbix_conn();

    return $conn->run(
        ping => sub {
            my $ret = 0;
            try {
                $ret = $_->do( $sql, {}, @bind_values);
            } catch {
                warn "Caught exception: $_\n";
                $ret = 0;
            };
            $ret;
        });
}

sub dbix_select {
    my ($self, $sql, @bind_values) = @_;

    my $conn = $self->_dbix_conn();

    return $conn->run( ping => sub {
                                   my $sth = undef;
                                   try {
                                       $sth = $_->prepare( $sql );
                                       $sth->execute( @bind_values );
                                   } catch {
                                       warn "Caught exception: $_\n";
                                       $sth = undef;
                                   };
                                   $sth;
                               });
}

## Private



( run in 0.607 second using v1.01-cache-2.11-cpan-39bf76dae61 )