File Coverage

lib/Workflow/Persister/DBI/AutoGeneratedId.pm
Criterion Covered Total %
statement 27 35 77.1
branch 7 16 43.7
condition 2 6 33.3
subroutine 8 8 100.0
pod 3 3 100.0
total 47 68 69.1


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