blib/lib/App/Netdisco/DB/ExplicitLocking.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 67 CC 5 SCAR 27.7
CC
5
Cov
0%
txn_do_locked
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 16.0
1 package App::Netdisco::DB::ExplicitLocking;
2  
3 use strict;
4 use warnings;
5  
6 our %lock_modes;
7  
8 BEGIN {
9 %lock_modes = (
10 ACCESS_SHARE => 'ACCESS SHARE',
11 ROW_SHARE => 'ROW SHARE',
12 ROW_EXCLUSIVE => 'ROW EXCLUSIVE',
13 SHARE_UPDATE_EXCLUSIVE => 'SHARE UPDATE EXCLUSIVE',
14 SHARE => 'SHARE',
15 SHARE_ROW_EXCLUSIVE => 'SHARE ROW EXCLUSIVE',
16 EXCLUSIVE => 'EXCLUSIVE',
17 ACCESS_EXCLUSIVE => 'ACCESS EXCLUSIVE',
18 );
19 }
20  
21 use constant \%lock_modes;
22  
23 use base 'Exporter';
24 our @EXPORT = ();
25 our @EXPORT_OK = (keys %lock_modes);
26 our %EXPORT_TAGS = (modes => \@EXPORT_OK);
27  
28 sub txn_do_locked {
29 my ($self, $table, $mode, $sub) = @_;
30 my $sql_fmt = q{LOCK TABLE %s IN %%s MODE};
31 my $schema = $self;
32  
33 if ($self->can('result_source')) {
34 # ResultSet component
35 $sub = $mode;
36 $mode = $table;
37 $table = $self->result_source->from;
38 $schema = $self->result_source->schema;
39 }
40  
41 $schema->throw_exception('missing Table name to txn_do_locked()')
42 unless $table;
43  
44 $table = [$table] if ref '' eq ref $table;
45 my $table_fmt = join ', ', ('%s' x scalar @$table);
46 my $sql = sprintf $sql_fmt, $table_fmt;
47  
48 if (ref '' eq ref $mode and $mode) {
49 scalar grep {$_ eq $mode} values %lock_modes
50 or $schema->throw_exception('bad LOCK_MODE to txn_do_locked()');
51 }
52 else {
53 $sub = $mode;
54 $mode = 'ACCESS EXCLUSIVE';
55 }
56  
57 $schema->txn_do(sub {
58 my @params = map {$schema->storage->dbh->quote_identifier($_)} @$table;
59 $schema->storage->dbh->do(sprintf $sql, @params, $mode);
60 $sub->();
61 });
62 }
63  
64 =head1 NAME
65  
66 App::Netdisco::DB::ExplicitLocking - Support for PostgreSQL Lock Modes
67  
68 =head1 SYNOPSIS
69  
70 In your L<DBIx::Class> schema:
71  
72 package My::Schema;
73 __PACKAGE__->load_components('+App::Netdisco::DB::ExplicitLocking');
74  
75 Then, in your application code:
76  
77 use App::Netdisco::DB::ExplicitLocking ':modes';
78 $schema->txn_do_locked($table, MODE_NAME, sub { ... });
79  
80 This also works for the ResultSet:
81  
82 package My::Schema::ResultSet::TableName;
83 __PACKAGE__->load_components('+App::Netdisco::DB::ExplicitLocking');
84  
85 Then, in your application code:
86  
87 use App::Netdisco::DB::ExplicitLocking ':modes';
88 $schema->resultset('TableName')->txn_do_locked(MODE_NAME, sub { ... });
89  
90 =head1 DESCRIPTION
91  
92 This L<DBIx::Class> component provides an easy way to execute PostgreSQL table
93 locks before a transaction block.
94  
95 You can load the component in either the Schema class or ResultSet class (or
96 both) and then use an interface very similar to C<DBIx::Class>'s C<txn_do()>.
97  
98 The package also exports constants for each of the table lock modes supported
99 by PostgreSQL, which must be used if specifying the mode (default mode is
100 C<ACCESS EXCLUSIVE>).
101  
102 =head1 EXPORTS
103  
104 With the C<:modes> tag (as in SYNOPSIS above) the following constants are
105 exported and must be used if specifying the lock mode:
106  
107 =over 4
108  
109 =item * C<ACCESS_SHARE>
110  
111 =item * C<ROW_SHARE>
112  
113 =item * C<ROW_EXCLUSIVE>
114  
115 =item * C<SHARE_UPDATE_EXCLUSIVE>
116  
117 =item * C<SHARE>
118  
119 =item * C<SHARE_ROW_EXCLUSIVE>
120  
121 =item * C<EXCLUSIVE>
122  
123 =item * C<ACCESS_EXCLUSIVE>
124  
125 =back
126  
127 =head1 METHODS
128  
129 =head2 C<< $schema->txn_do_locked($table|\@tables, MODE_NAME?, $subref) >>
130  
131 This is the method signature used when the component is loaded into your
132 Schema class. The reason you might want to use this over the ResultSet version
133 (below) is to specify multiple tables to be locked before the transaction.
134  
135 The first argument is one or more tables, and is required. Note that these are
136 the real table names in PostgreSQL, and not C<DBIx::Class> ResultSet aliases
137 or anything like that.
138  
139 The mode name is optional, and defaults to C<ACCESS EXCLUSIVE>. You must use
140 one of the exported constants in this parameter.
141  
142 Finally pass a subroutine reference, just as you would to the normal
143 C<DBIx::Class> C<txn_do()> method. Note that additional arguments are not
144 supported.
145  
146 =head2 C<< $resultset->txn_do_locked(MODE_NAME?, $subref) >>
147  
148 This is the method signature used when the component is loaded into your
149 ResultSet class. If you don't yet have a ResultSet class (which is the default
150 - normally only Result classes are created) then you can create a stub which
151 simply loads this component (and inherits from C<DBIx::Class::ResultSet>).
152  
153 This is the simplest way to use this module if you only want to lock one table
154 before your transaction block.
155  
156 The first argument is the optional mode name, which defaults to C<ACCESS
157 EXCLUSIVE>. You must use one of the exported constants in this parameter.
158  
159 The second argument is a subroutine reference, just as you would pass to the
160 normal C<DBIx::Class> C<txn_do()> method. Note that additional arguments are
161 not supported.
162  
163 =cut
164  
165 1;