File Coverage

blib/lib/Workflow/Persister/DBI/AutoGeneratedId.pm
Criterion Covered Total %
statement 34 37 91.8
branch 12 16 75.0
condition 5 6 83.3
subroutine 9 9 100.0
pod 3 3 100.0
total 63 71 88.7


line stmt bran cond sub pod time code
1             package Workflow::Persister::DBI::AutoGeneratedId;
2              
3 19     19   229794 use warnings;
  19         70  
  19         1403  
4 19     19   126 use strict;
  19         46  
  19         517  
5 19     19   262 use v5.14.0;
  19         75  
6 19     19   116 use parent qw( Class::Accessor );
  19         39  
  19         156  
7 19     19   8559 use Log::Any;
  19         7258  
  19         245  
8 19     19   1577 use Workflow::Exception qw( configuration_error );
  19         63  
  19         14909  
9              
10             $Workflow::Persister::DBI::AutoGeneratedId::VERSION = '2.09';
11              
12             my @FIELDS = qw( log from_handle handle_property func_property );
13             __PACKAGE__->mk_accessors(@FIELDS);
14              
15             sub new {
16 3     3 1 4402 my ( $class, $params ) = @_;
17              
18 3         18 my $self =
19             bless { log => Log::Any->get_logger( category => $class ) }, $class;
20 3         428 for (@FIELDS) {
21 12 100       121 $self->$_( $params->{$_} ) if ( $params->{$_} );
22             }
23 3 100       24 if ( my $handle_type = $self->from_handle ) {
    50          
24 2 50       44 unless ( $handle_type =~ /^(database|statement)$/ ) {
25 0         0 configuration_error "Parameter 'from_handle' must be 'database' ",
26             "or 'statement' (Given: '$handle_type')";
27             }
28 2 50       6 unless ( $self->handle_property ) {
29 0         0 configuration_error "If you specify 'from_handle' you must ",
30             "specify a value for 'handle_property'";
31             }
32 2         30 $self->log->debug( "Using '", $self->handle_property, "' from ", "'",
33             $self->from_handle, "' for ID generator" );
34             } elsif ( !$self->func_property ) {
35 0         0 configuration_error "If you do not specify a value in 'from_handle' ",
36             "you must specify a value for 'func_property'";
37             } else {
38 1         30 $self->log->debug( "Using database func() property '",
39             $self->func_property, "' for ID generator" );
40             }
41 3         115 return $self;
42             }
43              
44 3     3 1 2964 sub pre_fetch_id {return}
45              
46             sub post_fetch_id {
47 3     3 1 115 my ( $self, $dbh, $sth ) = @_;
48              
49 3         13 my $from_handle = $self->from_handle;
50              
51 3 100 100     72 if ( defined $from_handle and $from_handle eq 'database' ) {
    100 66        
    50          
52 1         6 return $dbh->{ $self->handle_property };
53             } elsif ( defined $from_handle and $from_handle eq 'statement' ) {
54 1         6 return $sth->{ $self->handle_property };
55             } elsif ( my $func_property = $self->func_property ) {
56 1         35 return $dbh->func($func_property);
57             }
58             }
59              
60             1;
61              
62             __END__
63              
64             =pod
65              
66             =head1 NAME
67              
68             Workflow::Persister::DBI::AutoGeneratedId - Pull IDs from databases that autogenerate them
69              
70             =head1 VERSION
71              
72             This documentation describes version 2.09 of this package
73              
74             =head1 SYNOPSIS
75              
76             persister:
77             - name: MyPersister
78             dsn: DBI:mysql:database=foo
79             ...
80              
81             =head1 DESCRIPTION
82              
83             Be able to pull an ID from a database or statement handle, or call a
84             DBI function to get the value.
85              
86             =head2 Properties
87              
88             B<from_handle>
89              
90             If you want to pull the value from a handle specify either 'database'
91             or 'statement' to specify what handle to pull it from. You must also
92             specify a value for 'handle_property'. For example, if you are using
93             MySQL this would be 'database'.
94              
95             B<handle_property>
96              
97             Property to pull from handle specified in 'from_handle'. For example,
98             if you are using MySQL this would be 'mysql_insertid'.
99              
100             B<func_property>
101              
102             Property to pass to the DBI 'func()' call to return the ID value. For
103             example, if you are using SQLite this would be 'last_insert_rowid'.
104              
105             =head2 ATTRIBUTES
106              
107             =head3 log
108              
109             Contains the logger object associated with this instance.
110              
111             =head2 METHODS
112              
113             =head3 new ( \%params )
114              
115             This method instantiates a class for retrieval of auto-generated ids from a
116             L<DBI> based persistance entity.
117              
118             It takes a hashref containing keys matching the properties outlines in the
119             section above or throws L<Workflow::Exception>s if these are not defined.
120              
121             Returns instantiated object upon success.
122              
123             =head3 pre_fetch_id
124              
125             This is a I<dummy> method, use L</post_fetch_id>
126              
127             =head3 post_fetch_id
128              
129             Returns a unique sequence id from a database.
130              
131             Takes a two parameters, a L<DBI> database handle and a statement handle
132              
133             Returns a single value, a integer representing a sequence id from the provided
134             database handle, based on the statement handle.
135              
136             =head1 COPYRIGHT
137              
138             Copyright (c) 2003-2021 Chris Winters. All rights reserved.
139              
140             This library is free software; you can redistribute it and/or modify
141             it under the same terms as Perl itself.
142              
143             Please see the F<LICENSE>
144              
145             =head1 AUTHORS
146              
147             Please see L<Workflow>
148              
149             =cut