Workflow

 view release on metacpan or  search on metacpan

lib/Workflow/Persister/DBI/AutoGeneratedId.pm  view on Meta::CPAN

package Workflow::Persister::DBI::AutoGeneratedId;

use warnings;
use strict;
use v5.14.0;
use parent qw( Class::Accessor );
use Log::Any;
use Workflow::Exception qw( configuration_error );

$Workflow::Persister::DBI::AutoGeneratedId::VERSION = '2.09';

my @FIELDS = qw( log from_handle handle_property func_property );
__PACKAGE__->mk_accessors(@FIELDS);

sub new {
    my ( $class, $params ) = @_;

    my $self =
        bless { log => Log::Any->get_logger( category => $class ) }, $class;
    for (@FIELDS) {
        $self->$_( $params->{$_} ) if ( $params->{$_} );
    }
    if ( my $handle_type = $self->from_handle ) {
        unless ( $handle_type =~ /^(database|statement)$/ ) {
            configuration_error "Parameter 'from_handle' must be 'database' ",
                "or 'statement' (Given: '$handle_type')";
        }
        unless ( $self->handle_property ) {
            configuration_error "If you specify 'from_handle' you must ",
                "specify a value for 'handle_property'";
        }
        $self->log->debug( "Using '", $self->handle_property, "' from ", "'",
                           $self->from_handle, "' for ID generator" );
    } elsif ( !$self->func_property ) {
        configuration_error "If you do not specify a value in 'from_handle' ",
            "you must specify a value for 'func_property'";
    } else {
        $self->log->debug( "Using database func() property '",
                           $self->func_property, "' for ID generator" );
    }
    return $self;
}

sub pre_fetch_id {return}

sub post_fetch_id {
    my ( $self, $dbh, $sth ) = @_;

    my $from_handle = $self->from_handle;

    if ( defined $from_handle and $from_handle eq 'database' ) {
        return $dbh->{ $self->handle_property };
    } elsif ( defined $from_handle and $from_handle eq 'statement' ) {
        return $sth->{ $self->handle_property };
    } elsif ( my $func_property = $self->func_property ) {
        return $dbh->func($func_property);
    }
}

1;

__END__

=pod

=head1 NAME

Workflow::Persister::DBI::AutoGeneratedId - Pull IDs from databases that autogenerate them

=head1 VERSION

This documentation describes version 2.09 of this package

=head1 SYNOPSIS

 persister:
 - name: MyPersister
   dsn: DBI:mysql:database=foo
   ...



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