File Coverage

blib/lib/Workflow/Persister/DBI/SequenceId.pm
Criterion Covered Total %
statement 35 35 100.0
branch n/a
condition 1 2 50.0
subroutine 11 11 100.0
pod 3 3 100.0
total 50 51 98.0


line stmt bran cond sub pod time code
1             package Workflow::Persister::DBI::SequenceId;
2              
3 19     19   269319 use warnings;
  19         49  
  19         1362  
4 19     19   116 use strict;
  19         40  
  19         541  
5 19     19   218 use v5.14.0;
  19         75  
6 19     19   113 use parent qw( Class::Accessor );
  19         45  
  19         140  
7 19     19   4707 use DBI;
  19         15800  
  19         2920  
8 19     19   564 use Log::Any;
  19         6943  
  19         218  
9 19     19   1485 use Workflow::Exception qw( persist_error );
  19         78  
  19         1091  
10 19     19   491 use Syntax::Keyword::Try;
  19         1965  
  19         195  
11              
12             $Workflow::Persister::DBI::SequenceId::VERSION = '2.09';
13              
14             my @FIELDS = qw( log sequence_name sequence_select );
15             __PACKAGE__->mk_accessors(@FIELDS);
16              
17              
18             sub new {
19 3     3 1 2209 my ( $class, $params ) = @_;
20 3   50     11 $params ||= {};
21 3         14 $params->{log} = Log::Any->get_logger( category => $class );
22              
23 3         410 return bless { %{$params} }, $class;
  3         26  
24             }
25              
26             sub pre_fetch_id {
27 1     1 1 84 my ( $self, $dbh ) = @_;
28 1         5 my $full_select = sprintf $self->sequence_select, $self->sequence_name;
29 1         35 $self->log->debug("SQL to fetch sequence: ", $full_select);
30 1         12 my ($row);
31             try {
32             my $sth = $dbh->prepare($full_select);
33             $sth->execute;
34             $row = $sth->fetchrow_arrayref;
35             $sth->finish;
36             }
37 1         2 catch ($error) {
38             persist_error "Failed to retrieve sequence: $error";
39             }
40 1         41 return $row->[0];
41             }
42              
43 1     1 1 895 sub post_fetch_id {return}
44              
45             1;
46              
47             __END__
48              
49             =pod
50              
51             =head1 NAME
52              
53             Workflow::Persister::DBI::SequenceId - Persister to fetch ID from a sequence
54              
55             =head1 VERSION
56              
57             This documentation describes version 2.09 of this package
58              
59             =head1 SYNOPSIS
60              
61             persister:
62             - name: MyPersister
63             workflow_sequence: wf_seq
64             history_sequence: wf_history_seq
65             ...
66              
67             =head1 DESCRIPTION
68              
69             Implementation for DBI persister to fetch an ID value from a sequence.
70              
71             =head2 Properties
72              
73             B<sequence_name>
74              
75             Name of the sequence to select the next id value from.
76              
77             B<sequence_select>
78              
79             C<sprintf> template string with a single placeholder (C<%s>) used to
80             interpolate the sequence name. The resulting string is used as the SQL
81             statement to retrieve the next sequence value.
82              
83             =head2 ATTRIBUTES
84              
85             =head3 log
86              
87             Contains the logger object associated with this instance.
88              
89             =head2 METHODS
90              
91             =head3 new ( \%params )
92              
93             This method instantiates a class for retrieval of sequence ids from a
94             L<DBI> based persistance entity.
95              
96             It takes a hashref containing keys matching the properties outlines in the
97             section above or throws L<Workflow::Exception>s if these are not defined.
98              
99             Returns instantiated object upon success.
100              
101             =head3 pre_fetch_id
102              
103             Returns a unique sequence id from a database.
104              
105             Takes a single parameter, a L<DBI> database handle.
106              
107             Returns a single value, a integer representing a sequence id from the provided
108             database handle.
109              
110             =head3 post_fetch_id
111              
112             This is a I<dummy> method, use L</pre_fetch_id>
113              
114             =head1 COPYRIGHT
115              
116             Copyright (c) 2003-2021 Chris Winters. All rights reserved.
117              
118             This library is free software; you can redistribute it and/or modify
119             it under the same terms as Perl itself.
120              
121             Please see the F<LICENSE>
122              
123             =head1 AUTHORS
124              
125             Please see L<Workflow>
126              
127             =cut