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