SQLEngine/Driver/Trait/NoSequences.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 118 CC 7 SCAR 19.8
CC
7
Cov
0%
seq_fetch_current
20.0
seq_increment
12.0
seq_bootstrap_init
6.0
CRAP 7.2
1 =head1 NAME
2  
3 DBIx::SQLEngine::Driver::Trait::NoSequences - For databases without native sequences
4  
5 =head1 SYNOPSIS
6  
7 # Classes can import this behavior if they don't have native sequences
8 use DBIx::SQLEngine::Driver::Trait::NoSequences ':all';
9
10 # Public interface for NoSequences functionality
11 $nextid = $sqldb->seq_increment( $table, $field );
12  
13 # Housekeeping functions for setup and removal
14 $sqldb->seq_create_table();
15 $sqldb->seq_insert_record( $table, $field );
16 $sqldb->seq_delete_record( $table, $field );
17 $sqldb->seq_drop_table();
18  
19 =head1 DESCRIPTION
20  
21 This package supports SQL database servers which do natively support
22 an auto-incrementing or unique sequence trigger. Instead, a special
23 table is allocated to store sequence values, and queries are used
24 to atomically retrieve and increment the sequence value to ensure
25 uniqueness.
26  
27 =head2 About Driver Traits
28  
29 You do not need to use this package directly; it is used internally by those driver subclasses which need it.
30  
31 For more information about Driver Traits, see L<DBIx::SQLEngine::Driver/"About Driver Traits">.
32  
33 =cut
34  
35 ########################################################################
36  
37 package DBIx::SQLEngine::Driver::Trait::NoSequences;
38  
39 use Exporter;
40 sub import { goto &Exporter::import }
41 @EXPORT_OK = qw(
42 do_insert_with_sequence
43 seq_table_name seq_create_table seq_drop_table
44 seq_insert_record seq_delete_record
45 seq_fetch_current sql_seq_fetch_current
46 seq_increment sql_seq_increment
47 seq_bootstrap_init
48 );
49 %EXPORT_TAGS = ( all => \@EXPORT_OK );
50  
51 use strict;
52 use Carp;
53  
54 ########################################################################
55  
56 =head1 REFERENCE
57  
58 The following methods are provided:
59  
60 =cut
61  
62 ########################################################################
63  
64 =head2 Insert to Add Data
65  
66 =over 4
67  
68 =item do_insert_with_sequence()
69  
70 $sqldb->do_insert_with_sequence( $seq_name, %sql_clauses ) : $row_count
71  
72 Insert a single row into a table in the datasource, using a sequence to fill in the values of the column named in the first argument. Should return 1, unless there's an exception.
73  
74 Implemented with _seq_do_insert_preinc() and seq_increment().
75  
76 =back
77  
78 =cut
79  
80 # $rows = $self->do_insert_with_sequence( $sequence, %clauses );
81 sub do_insert_with_sequence {
82 (shift)->_seq_do_insert_preinc( @_ )
83 }
84  
85 ########################################################################
86  
87 =head2 Sequence Table
88  
89 =over 4
90  
91 =item seq_fetch_current()
92  
93 $sqldb->seq_fetch_current( $table, $field ) : $current_value
94  
95 Fetches the current sequence value.
96  
97 Implemented as an exception-handling wrapper around the query defined in
98 sql_seq_fetch_current(), which attempts to create the sequence table if it
99 doesn't exist and insert a row for this sequence if needed.
100  
101 =item sql_seq_fetch_current()
102  
103 $sqldb->sql_seq_fetch_current( $table, $field ) : $sql, @params
104  
105 Returns a SQL statement to fetch the current value from the sequence table.
106  
107 =cut
108  
109 # $current_id = $sqldb->seq_fetch_current( $table, $field );
110 sub seq_fetch_current {
111 my ($self, $table, $field) = @_;
112  
113 my $seq_table = $self->seq_table_name;
114  
115 my $current;
116 eval {
117 local $SIG{__DIE__};
118 $current = $self->fetch_one_value(
119 sql => [ $self->sql_seq_fetch_current($table, $field) ]
120 );
121 unless ( defined $current and length $current ) {
122 $self->seq_insert_record( $table, $field );
123 $current = 0; # $self->seq_bootstrap_init( $table, $field ) || 0;
124 }
125 };
126
127 if ( my $err = $@ ) {
128 eval {
129 local $SIG{__DIE__};
130 $self->seq_create_table();
131 $self->seq_insert_record( $table, $field );
132 $current = 0;
133 };
134 if ( $@ ) {
135 confess "Unable to select from sequence table $seq_table: $err\n" .
136 "Also unable to automatically create sequence table: $@";
137 }
138 }
139 return $current;
140 }
141  
142 # $sql, @params = $sqldb->sql_seq_fetch_current( $table, $field );
143 sub sql_seq_fetch_current {
144 my ($self, $table, $field) = @_;
145 my $seq_table = $self->seq_table_name;
146 $self->sql_select(
147 table => $seq_table,
148 columns => 'seq_value',
149 criteria => [ 'seq_name = ?', "$table.$field" ],
150 );
151 }
152  
153 ########################################################################
154  
155 =pod
156  
157 =item seq_increment()
158  
159 $sqldb->seq_increment( $table, $field ) : $new_value
160  
161 Increments the sequence, and returns the newly allocated value.
162  
163 This is the primary "public" interface of this package.
164  
165 If someone else has completed the same increment before we have, our update will have no effect and we'll immediately try again and again until successful.
166  
167 If the table does not yet exist, attempts to create it automatically.
168  
169 If the sequence record does not yet exist, attempts to create it automatically.
170  
171 =item sql_seq_increment()
172  
173 $sqldb->sql_seq_increment( $table, $field, $current, $next ) : $sql, @params
174  
175 Generates a SQL statement for use by seq_increment().
176  
177 =cut
178  
179 # $nextid = $sqldb->seq_increment( $table, $field );
180 # $nextid = $sqldb->seq_increment( $table, $field, $value);
181 sub seq_increment {
182 my ($self, $table, $field, $next) = @_;
183
184 ATTEMPT: {
185 my $current = $self->seq_fetch_current( $table, $field );
186  
187 $next = $current + 1 unless ( $next and $next > $current );
188
189 return $next if $self->do_update(
190 sql => [ $self->sql_seq_increment( $table, $field, $current, $next ) ]
191 );
192
193 redo ATTEMPT;
194 }
195 }
196  
197 # $sql, @params = $sqldb->sql_seq_increment( $table, $field, $current, $next );
198 sub sql_seq_increment {
199 my ($self, $table, $field, $current, $next) = @_;
200 my $seq_table = $self->seq_table_name;
201 $self->sql_update(
202 table => $seq_table,
203 values => { seq_value => $next },
204 criteria => ['seq_name = ? and seq_value = ?', "$table.$field", $current]
205 );
206 }
207  
208 ########################################################################
209  
210 ########################################################################
211  
212 =pod
213  
214 =item seq_table_name()
215  
216 Constant 'dbix_sqlengine_seq'.
217  
218 =cut
219  
220 use constant seq_table_name => 'dbix_sqlengine_seq';
221  
222 ########################################################################
223  
224 =pod
225  
226 =item seq_create_table()
227  
228 $sqldb->seq_create_table()
229  
230 Issues a SQL create table statement to create the sequence table.
231  
232 =item seq_drop_table()
233  
234 $sqldb->seq_drop_table()
235  
236 Issues a SQL drop table statement to remove the sequence table.
237  
238 =cut
239  
240 sub seq_create_table {
241 my $self = shift;
242 my $seq_table = $self->seq_table_name;
243 $self->do_create_table( $seq_table, [
244 { type => 'text', name => 'seq_name', length => 48, required => 1 },
245 { type => 'int', name => 'seq_value', required => 1 },
246 { type => 'primary', name => 'seq_name' },
247 ] );
248 # warn "Created sequence table '$seq_table'";
249 }
250  
251 # $sqldb->seq_drop_table();
252 sub seq_drop_table {
253 my $self = shift;
254 my $seq_table = $self->seq_table_name;
255 $self->do_drop_table( $seq_table );
256 # warn "Dropped sequence table '$seq_table'";
257 }
258  
259 ########################################################################
260  
261 =pod
262  
263 =item seq_insert_record()
264  
265 $sqldb->seq_insert_record( $table, $field )
266  
267 Creates a record in the sequence table for a given field in a particular table.
268  
269 =item seq_delete_record()
270  
271 $sqldb->seq_delete_record( $table, $field )
272  
273 Removes the corresponding record in the sequence table.
274  
275 =cut
276  
277 # $sqldb->seq_insert_record( $table, $field );
278 sub seq_insert_record {
279 my $self = shift;
280 my ($table, $field) = @_;
281 $self->do_insert(
282 table => $self->seq_table_name,
283 values => { seq_name => "$table.$field", seq_value => 0, },
284 );
285 }
286  
287 # $sqldb->seq_delete_record( $table, $field );
288 sub seq_delete_record {
289 my $self = shift;
290 my ($table, $field) = @_;
291 $self->do_delete(
292 table => $self->seq_table_name,
293 criteria => [ 'seq_name = ?', "$table.$field" ],
294 );
295 }
296  
297 ########################################################################
298  
299 =pod
300  
301 =item seq_bootstrap_init()
302  
303 $sqldb->seq_bootstrap_init( $table, $field ) : $current_value
304  
305 Scans the designated field in a given table to determine its maximum value, and then stores that in sequence table.
306  
307 =cut
308  
309 # $sqldb->seq_bootstrap_init( $table, $field );
310 sub seq_bootstrap_init {
311 my $self = shift;
312 my ($table, $field) = @_;
313
314 my $max = $self->fetch_one_value(
315 table => $table,
316 columns => "max($field)",
317 );
318
319 return unless $max;
320
321 $self->seq_increment( $table, $field, $max );
322 }
323  
324 ########################################################################
325  
326 =head1 SEE ALSO
327  
328 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
329  
330 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
331 this distribution, including installation and license information.
332  
333 See L<DBIx::Sequence> for another version of the sequence-table functionality, which greatly inspired this module.
334  
335 =cut
336  
337 ########################################################################
338  
339 1;
340