DBIx/Class/Ordered.pm

filter: Statementstmt 96.5% 197 / 204 Branchbran 85.3% 70 / 82 Conditioncond 64.2% 9 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 96.9% 32 / 33 Podpod 100% 16 / 16 totaltotal 91.6% 328 / 358 CC 50 SCAR 17.4
CC
50
Cov
92%
move_to_group
15.2
move_to
10.0
_shift_siblings
6.0
CRAP 5.7
« DBIx/Class/SQLMaker/MySQL.pm ↑ Summary DBIx/Class/InflateColumn.pm »
1 package DBIx::Class::Ordered;
2 329 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 329 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 329 ▶use base qw( DBIx::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 DBIx::Class::Ordered - Modify the position of objects in an ordered list.
9  
10 =head1 SYNOPSIS
11  
12 Create a table for your ordered data.
13  
14 CREATE TABLE items (
15 item_id INTEGER PRIMARY KEY AUTOINCREMENT,
16 name TEXT NOT NULL,
17 position INTEGER NOT NULL
18 );
19  
20 Optionally, add one or more columns to specify groupings, allowing you
21 to maintain independent ordered lists within one table:
22  
23 CREATE TABLE items (
24 item_id INTEGER PRIMARY KEY AUTOINCREMENT,
25 name TEXT NOT NULL,
26 position INTEGER NOT NULL,
27 group_id INTEGER NOT NULL
28 );
29  
30 Or even
31  
32 CREATE TABLE items (
33 item_id INTEGER PRIMARY KEY AUTOINCREMENT,
34 name TEXT NOT NULL,
35 position INTEGER NOT NULL,
36 group_id INTEGER NOT NULL,
37 other_group_id INTEGER NOT NULL
38 );
39  
40 In your Schema or DB class add "Ordered" to the top
41 of the component list.
42  
43 __PACKAGE__->load_components(qw( Ordered ... ));
44  
45 Specify the column that stores the position number for
46 each row.
47  
48 package My::Item;
49 __PACKAGE__->position_column('position');
50  
51 If you are using one grouping column, specify it as follows:
52  
53 __PACKAGE__->grouping_column('group_id');
54  
55 Or if you have multiple grouping columns:
56  
57 __PACKAGE__->grouping_column(['group_id', 'other_group_id']);
58  
59 That's it, now you can change the position of your objects.
60  
61 #!/use/bin/perl
62 use My::Item;
63  
64 my $item = My::Item->create({ name=>'Matt S. Trout' });
65 # If using grouping_column:
66 my $item = My::Item->create({ name=>'Matt S. Trout', group_id=>1 });
67  
68 my $rs = $item->siblings();
69 my @siblings = $item->siblings();
70  
71 my $sibling;
72 $sibling = $item->first_sibling();
73 $sibling = $item->last_sibling();
74 $sibling = $item->previous_sibling();
75 $sibling = $item->next_sibling();
76  
77 $item->move_previous();
78 $item->move_next();
79 $item->move_first();
80 $item->move_last();
81 $item->move_to( $position );
82 $item->move_to_group( 'groupname' );
83 $item->move_to_group( 'groupname', $position );
84 $item->move_to_group( {group_id=>'groupname', 'other_group_id=>'othergroupname'} );
85 $item->move_to_group( {group_id=>'groupname', 'other_group_id=>'othergroupname'}, $position );
86  
87 =head1 DESCRIPTION
88  
89 This module provides a simple interface for modifying the ordered
90 position of DBIx::Class objects.
91  
92 =head1 AUTO UPDATE
93  
94 All of the move_* methods automatically update the rows involved in
95 the query. This is not configurable and is due to the fact that if you
96 move a record it always causes other records in the list to be updated.
97  
98 =head1 METHODS
99  
100 =head2 position_column
101  
102 __PACKAGE__->position_column('position');
103  
104 Sets and retrieves the name of the column that stores the
105 positional value of each record. Defaults to "position".
106  
107 =cut
108  
109 329 __PACKAGE__->mk_classdata( 'position_column' => 'position' );
110  
111 =head2 grouping_column
112  
113 __PACKAGE__->grouping_column('group_id');
114  
115 This method specifies a column to limit all queries in
116 this module by. This effectively allows you to have multiple
117 ordered lists within the same table.
118  
119 =cut
120  
121 329 __PACKAGE__->mk_classdata( 'grouping_column' );
122  
123 =head2 null_position_value
124  
125 __PACKAGE__->null_position_value(undef);
126  
127 This method specifies a value of L</position_column> which B<would
128 never be assigned to a row> during normal operation. When
129 a row is moved, its position is set to this value temporarily, so
130 that any unique constraints can not be violated. This value defaults
131 to 0, which should work for all cases except when your positions do
132 indeed start from 0.
133  
134 =cut
135  
136 329 __PACKAGE__->mk_classdata( 'null_position_value' => 0 );
137  
138 =head2 siblings
139  
140 my $rs = $item->siblings();
141 my @siblings = $item->siblings();
142  
143 Returns an B<ordered> resultset of all other objects in the same
144 group excluding the one you called it on.
145  
146 The ordering is a backwards-compatibility artifact - if you need
147 a resultset with no ordering applied use C<_siblings>
148  
149 =cut
150 0 ▶sub siblings {
Subroutine siblings: not called CC=1 SCAR=6.9
Pod documented
151 0 my $self = shift;
152 0 return $self->_siblings->search ({}, { order_by => $self->position_column } );
153 }
154  
155 =head2 previous_siblings
156  
157 my $prev_rs = $item->previous_siblings();
158 my @prev_siblings = $item->previous_siblings();
159  
160 Returns a resultset of all objects in the same group
161 positioned before the object on which this method was called.
162  
163 =cut
164 390 ▶sub previous_siblings {
Subroutine previous_siblings: called CC=2 SCAR=7.0
Pod documented
165 390 my $self = shift;
166 390 my $position_column = $self->position_column;
167 390 my $position = $self->get_column ($position_column);
168 390 ▶ return ( defined $position
Branch: 1 branch
BranchTrueFalseTotal
defined $position ? : 390 0 390
169 ? $self->_siblings->search ({ $position_column => { '<', $position } })
170 : $self->_siblings
171 );
172 }
173  
174 =head2 next_siblings
175  
176 my $next_rs = $item->next_siblings();
177 my @next_siblings = $item->next_siblings();
178  
179 Returns a resultset of all objects in the same group
180 positioned after the object on which this method was called.
181  
182 =cut
183 841 ▶sub next_siblings {
Subroutine next_siblings: called CC=2 SCAR=6.9
Pod documented
184 841 my $self = shift;
185 841 my $position_column = $self->position_column;
186 841 my $position = $self->get_column ($position_column);
187 841 ▶ return ( defined $position
Branch: 1 branch
BranchTrueFalseTotal
defined $position ? : 582 259 841
188 ? $self->_siblings->search ({ $position_column => { '>', $position } })
189 : $self->_siblings
190 );
191 }
192  
193 =head2 previous_sibling
194  
195 my $sibling = $item->previous_sibling();
196  
197 Returns the sibling that resides one position back. Returns 0
198 if the current object is the first one.
199  
200 =cut
201  
202 260 ▶sub previous_sibling {
Subroutine previous_sibling: called CC=2 SCAR=6.9
Pod documented
203 260 my $self = shift;
204 260 my $position_column = $self->position_column;
205  
206 260 my $psib = $self->previous_siblings->search(
207 {},
208 { rows => 1, order_by => { '-desc' => $position_column } },
209 )->single;
210  
211 260 ▶ return defined $psib ? $psib : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $psib ? : 246 14 260
212 }
213  
214 =head2 first_sibling
215  
216 my $sibling = $item->first_sibling();
217  
218 Returns the first sibling object, or 0 if the first sibling
219 is this sibling.
220  
221 =cut
222  
223 130 ▶sub first_sibling {
Subroutine first_sibling: called CC=2 SCAR=6.9
Pod documented
224 130 my $self = shift;
225 130 my $position_column = $self->position_column;
226  
227 130 my $fsib = $self->previous_siblings->search(
228 {},
229 { rows => 1, order_by => { '-asc' => $position_column } },
230 )->single;
231  
232 130 ▶ return defined $fsib ? $fsib : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $fsib ? : 116 14 130
233 }
234  
235 =head2 next_sibling
236  
237 my $sibling = $item->next_sibling();
238  
239 Returns the sibling that resides one position forward. Returns 0
240 if the current object is the last one.
241  
242 =cut
243  
244 260 ▶sub next_sibling {
Subroutine next_sibling: called CC=2 SCAR=6.9
Pod documented
245 260 my $self = shift;
246 260 my $position_column = $self->position_column;
247 260 my $nsib = $self->next_siblings->search(
248 {},
249 { rows => 1, order_by => { '-asc' => $position_column } },
250 )->single;
251  
252 260 ▶ return defined $nsib ? $nsib : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $nsib ? : 246 14 260
253 }
254  
255 =head2 last_sibling
256  
257 my $sibling = $item->last_sibling();
258  
259 Returns the last sibling, or 0 if the last sibling is this
260 sibling.
261  
262 =cut
263  
264 130 ▶sub last_sibling {
Subroutine last_sibling: called CC=2 SCAR=6.9
Pod documented
265 130 my $self = shift;
266 130 my $position_column = $self->position_column;
267 130 my $lsib = $self->next_siblings->search(
268 {},
269 { rows => 1, order_by => { '-desc' => $position_column } },
270 )->single;
271  
272 130 ▶ return defined $lsib ? $lsib : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $lsib ? : 116 14 130
273 }
274  
275 # an optimized method to get the last sibling position value without inflating a result object
276 451 ▶sub _last_sibling_posval {
Subroutine _last_sibling_posval: called CC=1 SCAR=0.0
277 451 my $self = shift;
278 451 my $position_column = $self->position_column;
279  
280 451 my $cursor = $self->next_siblings->search(
281 {},
282 { rows => 1, order_by => { '-desc' => $position_column }, select => $position_column },
283 )->cursor;
284  
285 451 my ($pos) = $cursor->next;
286 451 return $pos;
287 }
288  
289 =head2 move_previous
290  
291 $item->move_previous();
292  
293 Swaps position with the sibling in the position previous in
294 the list. Returns 1 on success, and 0 if the object is
295 already the first one.
296  
297 =cut
298  
299 65 ▶sub move_previous {
Subroutine move_previous: called CC=1 SCAR=0.0
Pod documented
300 65 my $self = shift;
301 65 return $self->move_to ($self->_position - 1);
302 }
303  
304 =head2 move_next
305  
306 $item->move_next();
307  
308 Swaps position with the sibling in the next position in the
309 list. Returns 1 on success, and 0 if the object is already
310 the last in the list.
311  
312 =cut
313  
314 65 ▶sub move_next {
Subroutine move_next: called CC=2 SCAR=6.9
Pod documented
315 65 my $self = shift;
316 65 ▶ return 0 unless defined $self->_last_sibling_posval; # quick way to check for no more siblings
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_last_sibling_posval 14 51 65
317 51 return $self->move_to ($self->_position + 1);
318 }
319  
320 =head2 move_first
321  
322 $item->move_first();
323  
324 Moves the object to the first position in the list. Returns 1
325 on success, and 0 if the object is already the first.
326  
327 =cut
328  
329 65 ▶sub move_first {
Subroutine move_first: called CC=1 SCAR=0.0
Pod documented
330 65 return shift->move_to( 1 );
331 }
332  
333 =head2 move_last
334  
335 $item->move_last();
336  
337 Moves the object to the last position in the list. Returns 1
338 on success, and 0 if the object is already the last one.
339  
340 =cut
341  
342 129 ▶sub move_last {
Subroutine move_last: called CC=2 SCAR=6.9
Pod documented
343 129 my $self = shift;
344 129 my $last_posval = $self->_last_sibling_posval;
345  
346 129 ▶ return 0 unless defined $last_posval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_posval 48 81 129
347  
348 81 return $self->move_to( $self->_position_from_value ($last_posval) );
349 }
350  
351 =head2 move_to
352  
353 $item->move_to( $position );
354  
355 Moves the object to the specified position. Returns 1 on
356 success, and 0 if the object is already at the specified
357 position.
358  
359 =cut
360  
361 577 ▶sub move_to {
Subroutine move_to: called CC=10 SCAR=23.0
Pod documented
362 577 my( $self, $to_position ) = @_;
363 577 ▶ return 0 if ( $to_position < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $to_position < 1 14 563 577
364  
365 563 my $position_column = $self->position_column;
366  
367 563 my $is_txn;
368 563 ▶ if ($is_txn = $self->result_source->schema->storage->transaction_depth) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_txn = $self->result_source->schema->storage->transaction_depth) { } 30 533 563
elsif ($self->is_column_changed($position_column)) { } 2 531 533
369 # Reload position state from storage
370 # The thinking here is that if we are in a transaction, it is
371 # *more likely* the object went out of sync due to resultset
372 # level shenanigans. Instead of always reloading (slow) - go
373 # ahead and hand-hold only in the case of higher layers
374 # requesting the safety of a txn
375  
376 30 ▶ $self->store_column(
Condition33%1/3
($self->result_source->resultset->search($self->_storage_ident_condition, {'rows', 1, 'columns', $position_column})->cursor->next)[0] || $self->throw_exception(sprintf(q[Unable to locate object '%s' in storage - object went ouf of sync...?], $self->ID))
l!l&&r!l&&!r
3000
MC/DC0%0/2
($self->result_source->resultset->search($self->_storage_ident_condition, {'rows', 1, 'columns', $position_column})->cursor->next)[0] || $self->throw_exception(sprintf(q[Unable to locate object '%s' in storage - object went ouf of sync...?], $self->ID))
($self->result_source->resultset->search($self->_storage_ident_condition, {'rows', 1, 'columns', $position_column})->cursor->next)[0] $self->throw_exception(sprintf(q[Unable to locate object '%s' in storage - object went ouf of sync...?], $self->ID))
Truth table33%1/3
A || B
A= ($self->result_source->resultset->search($self->_storage_ident_condition, {'rows', 1, 'columns', $position_column})->cursor->next)[0]B= $self->throw_exception(sprintf(q[Unable to locate object '%s' in storage - object went ouf of sync...?], $self->ID))
ABresult
1X1
011
000
377 $position_column,
378 ( $self->result_source
379 ->resultset
380 ->search($self->_storage_ident_condition, { rows => 1, columns => $position_column })
381 ->cursor
382 ->next
383 )[0] || $self->throw_exception(
384 sprintf "Unable to locate object '%s' in storage - object went ouf of sync...?",
385 $self->ID
386 ),
387 );
388 30 delete $self->{_dirty_columns}{$position_column};
389 }
390 elsif ($self->is_column_changed ($position_column) ) {
391 # something changed our position, we need to know where we
392 # used to be - use the stashed value
393 2 $self->store_column($position_column, delete $self->{_column_data_in_storage}{$position_column});
394 2 delete $self->{_dirty_columns}{$position_column};
395 }
396  
397 563 my $from_position = $self->_position;
398  
399 563 ▶ if ( $from_position == $to_position ) { # FIXME this will not work for non-numeric order
Branch: 1 branch
BranchTrueFalseTotal
if ($from_position == $to_position) 80 483 563
400 80 return 0;
401 }
402  
403 483 ▶ my $guard = $is_txn ? undef : $self->result_source->schema->txn_scope_guard;
Branch: 1 branch
BranchTrueFalseTotal
$is_txn ? : 29 454 483
404  
405 483 my ($direction, @between);
406 483 ▶ if ( $from_position < $to_position ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_position < $to_position) { } 257 226 483
407 257 $direction = -1;
408 257 @between = map { $self->_position_value ($_) } ( $from_position + 1, $to_position );
409 }
410 else {
411 226 $direction = 1;
412 226 @between = map { $self->_position_value ($_) } ( $to_position, $from_position - 1 );
413 }
414  
415 483 my $new_pos_val = $self->_position_value ($to_position); # record this before the shift
416  
417 # we need to null-position the moved row if the position column is part of a constraint
418 483 ▶ if (grep { $_ eq $position_column } ( map { @$_ } (values %{{ $self->result_source->unique_constraints }} ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $position_column;} map({@$_;} values %{+{$self->result_source->unique_constraints};})) 16 467 483
419 16 $self->_ordered_internal_update({ $position_column => $self->null_position_value });
420 }
421  
422 483 $self->_shift_siblings ($direction, @between);
423 483 $self->_ordered_internal_update({ $position_column => $new_pos_val });
424  
425 483 ▶ $guard->commit if $guard;
Branch: 1 branch
BranchTrueFalseTotal
if $guard 454 29 483
426 483 return 1;
427 }
428  
429 =head2 move_to_group
430  
431 $item->move_to_group( $group, $position );
432  
433 Moves the object to the specified position of the specified
434 group, or to the end of the group if $position is undef.
435 1 is returned on success, and 0 is returned if the object is
436 already at the specified position of the specified group.
437  
438 $group may be specified as a single scalar if only one
439 grouping column is in use, or as a hashref of column => value pairs
440 if multiple grouping columns are in use.
441  
442 =cut
443  
444 19 ▶sub move_to_group {
Subroutine move_to_group: called CC=13 SCAR=27.2
Pod documented
445 19 my( $self, $to_group, $to_position ) = @_;
446  
447 # if we're given a single value, turn it into a hashref
448 19 ▶ unless (ref $to_group eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $to_group eq "HASH") 6 13 19
449 6 my @gcols = $self->_grouping_columns;
450  
451 6 ▶ $self->throw_exception ('Single group supplied for a multi-column group identifier') if @gcols > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @gcols > 1 0 6 6
452 6 $to_group = {$gcols[0] => $to_group};
453 }
454  
455 19 my $position_column = $self->position_column;
456  
457 19 ▶ return 0 if ( defined($to_position) and $to_position < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $to_position and $to_position < 1 0 19 19
Condition66%2/3
defined $to_position and $to_position < 1
!ll&&!rl&&r
3160
MC/DC0%0/2
defined $to_position and $to_position < 1
defined $to_position $to_position < 1
Truth table66%2/3
A and B
A= defined $to_positionB= $to_position < 1
ABresult
0X0
100
111
458  
459 # check if someone changed the _grouping_columns - this will
460 # prevent _is_in_group working, so we need to restore the
461 # original stashed values
462 19 for ($self->_grouping_columns) {
463 24 ▶ if ($self->is_column_changed ($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_column_changed($_)) 16 8 24
464 16 $self->store_column($_, delete $self->{_column_data_in_storage}{$_});
465 16 delete $self->{_dirty_columns}{$_};
466 }
467 }
468  
469 19 ▶ if ($self->_is_in_group ($to_group) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_in_group($to_group)) 0 19 19
470 0 my $ret;
471 0 ▶ if (defined $to_position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $to_position) 0 0 0
472 0 $ret = $self->move_to ($to_position);
473 }
474  
475 0 ▶ return $ret||0;
Condition0%0/2
$ret || 0
l!l
00
MC/DC0%0/1
$ret || 0
$ret
Truth table0%0/2
A
A= $ret
Aresult
11
00
476 }
477  
478 19 my $guard = $self->result_source->schema->txn_scope_guard;
479  
480 # Move to end of current group to adjust siblings
481 19 $self->move_last;
482  
483 19 $self->set_inflated_columns({ %$to_group, $position_column => undef });
484 19 my $new_group_last_posval = $self->_last_sibling_posval;
485 19 my $new_group_last_position = $self->_position_from_value (
486 $new_group_last_posval
487 );
488  
489 19 ▶ if ( not defined($to_position) or $to_position > $new_group_last_position) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_position or $to_position > $new_group_last_position) { } 5 14 19
Condition100%3/3
not defined $to_position or $to_position > $new_group_last_position
l!l&&r!l&&!r
3214
MC/DC100%2/2
not defined $to_position or $to_position > $new_group_last_position
not defined $to_position $to_position > $new_group_last_position
Truth table100%3/3
not(A) or B
A= not defined $to_positionB= $to_position > $new_group_last_position
ABresult
1X1
011
000
490 5 ▶ $self->set_column(
Branch: 1 branch
BranchTrueFalseTotal
$new_group_last_position ? : 5 0 5
491 $position_column => $new_group_last_position
492 ? $self->_next_position_value ( $new_group_last_posval )
493 : $self->_initial_position_value
494 );
495 }
496 else {
497 14 my $bumped_pos_val = $self->_position_value ($to_position);
498 14 my @between = map { $self->_position_value ($_) } ($to_position, $new_group_last_position);
499 14 $self->_shift_siblings (1, @between); #shift right
500 14 $self->set_column( $position_column => $bumped_pos_val );
501 }
502  
503 19 $self->_ordered_internal_update;
504  
505 19 $guard->commit;
506  
507 19 return 1;
508 }
509  
510 =head2 insert
511  
512 Overrides the DBIC insert() method by providing a default
513 position number. The default will be the number of rows in
514 the table +1, thus positioning the new record at the last position.
515  
516 =cut
517  
518 263 ▶sub insert {
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
519 263 my $self = shift;
520 263 my $position_column = $self->position_column;
521  
522 263 ▶ unless ($self->get_column($position_column)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_column($position_column)) 238 25 263
523 238 my $lsib_posval = $self->_last_sibling_posval;
524 238 ▶ $self->set_column(
Branch: 1 branch
BranchTrueFalseTotal
defined $lsib_posval ? : 169 69 238
525 $position_column => (defined $lsib_posval
526 ? $self->_next_position_value ( $lsib_posval )
527 : $self->_initial_position_value
528 )
529 );
530 }
531  
532 263 return $self->next::method( @_ );
533 }
534  
535 =head2 update
536  
537 Overrides the DBIC update() method by checking for a change
538 to the position and/or group columns. Movement within a
539 group or to another group is handled by repositioning
540 the appropriate siblings. Position defaults to the end
541 of a new group if it has been changed to undef.
542  
543 =cut
544  
545 570 ▶sub update {
Subroutine update: called CC=6 SCAR=17.9
Pod documented
546 570 my $self = shift;
547  
548 # this is set by _ordered_internal_update()
549 570 ▶ return $self->next::method(@_) if $self->result_source->schema->{_ORDERED_INTERNAL_UPDATE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->result_source->schema->{"_ORDERED_INTERNAL_UPDATE"} 554 16 570
550  
551 16 my $upd = shift;
552 16 ▶ $self->set_inflated_columns($upd) if $upd;
Branch: 1 branch
BranchTrueFalseTotal
if $upd 8 8 16
553  
554 16 my $position_column = $self->position_column;
555 16 my @group_columns = $self->_grouping_columns;
556  
557 # see if the order is already changed
558 16 my $changed_ordering_cols = { map { $_ => $self->get_column($_) } grep { $self->is_column_changed($_) } ($position_column, @group_columns) };
559  
560 # nothing changed - short circuit
561 16 ▶ if (! keys %$changed_ordering_cols) {
Branch: 2 branches
BranchTrueFalseTotal
if (not keys %$changed_ordering_cols) { } 1 15 16
elsif (grep {exists $changed_ordering_cols->{$_};} @group_columns) { } 13 2 15
562 1 return $self->next::method( undef, @_ );
563 }
564 20 elsif (grep { exists $changed_ordering_cols->{$_} } @group_columns ) {
565 $self->move_to_group(
566 # since the columns are already re-set the _grouping_clause is correct
567 # move_to_group() knows how to get the original storage values
568 { $self->_grouping_clause },
569  
570 # The FIXME bit contradicts the documentation: POD states that
571 # when changing groups without supplying explicit positions in
572 # move_to_group(), we push the item to the end of the group.
573 # However when I was rewriting this, the position from the old
574 # group was clearly passed to the new one
575 # Probably needs to go away (by ribasushi)
576 (exists $changed_ordering_cols->{$position_column}
577 13 ▶ ? $changed_ordering_cols->{$position_column} # means there was a position change supplied with the update too
Branch: 1 branch
BranchTrueFalseTotal
exists $changed_ordering_cols->{$position_column} ? : 5 8 13
578 : $self->_position # FIXME! (replace with undef)
579 ),
580 );
581 }
582 else {
583 2 $self->move_to($changed_ordering_cols->{$position_column});
584 }
585  
586 15 return $self;
587 }
588  
589 =head2 delete
590  
591 Overrides the DBIC delete() method by first moving the object
592 to the last position, then deleting it, thus ensuring the
593 integrity of the positions.
594  
595 =cut
596  
597 45 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
598 45 my $self = shift;
599  
600 45 my $guard = $self->result_source->schema->txn_scope_guard;
601  
602 45 $self->move_last;
603  
604 45 $self->next::method( @_ );
605  
606 45 $guard->commit;
607  
608 45 return $self;
609 }
610  
611 # add the current position/group to the things we track old values for
612 600 ▶sub _track_storage_value {
Subroutine _track_storage_value: called CC=2 SCAR=6.9
613 600 my ($self, $col) = @_;
614 return (
615 $self->next::method($col)
616 ||
617 600 ▶ grep { $_ eq $col } ($self->position_column, $self->_grouping_columns)
Condition100%3/3
$self->next::method($col) || grep({$_ eq $col;} $self->position_column, $self->_grouping_columns)
l!l&&r!l&&!r
15972
MC/DC100%2/2
$self->next::method($col) || grep({$_ eq $col;} $self->position_column, $self->_grouping_columns)
$self->next::method($col) grep({$_ eq $col;} $self->position_column, $self->_grouping_columns)
Truth table100%3/3
A || B
A= $self->next::method($col)B= grep({$_ eq $col;} $self->position_column, $self->_grouping_columns)
ABresult
1X1
011
000
618 );
619 }
620  
621 =head1 METHODS FOR EXTENDING ORDERED
622  
623 You would want to override the methods below if you use sparse
624 (non-linear) or non-numeric position values. This can be useful
625 if you are working with preexisting non-normalised position data,
626 or if you need to work with materialized path columns.
627  
628 =head2 _position_from_value
629  
630 my $num_pos = $item->_position_from_value ( $pos_value )
631  
632 Returns the B<absolute numeric position> of an object with a B<position
633 value> set to C<$pos_value>. By default simply returns C<$pos_value>.
634  
635 =cut
636 787 ▶sub _position_from_value {
Subroutine _position_from_value: called CC=2 SCAR=7.1
637 787 my ($self, $val) = @_;
638  
639 787 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 787 787
640  
641 # #the right way to do this
642 # return $self -> _group_rs
643 # -> search({ $self->position_column => { '<=', $val } })
644 # -> count
645  
646 787 return $val;
647 }
648  
649 =head2 _position_value
650  
651 my $pos_value = $item->_position_value ( $pos )
652  
653 Returns the B<value> of L</position_column> of the object at numeric
654 position C<$pos>. By default simply returns C<$pos>.
655  
656 =cut
657 1491 ▶sub _position_value {
Subroutine _position_value: called CC=1 SCAR=0.0
658 1491 my ($self, $pos) = @_;
659  
660 # #the right way to do this (not optimized)
661 # my $position_column = $self->position_column;
662 # return $self -> _group_rs
663 # -> search({}, { order_by => $position_column })
664 # -> slice ( $pos - 1)
665 # -> single
666 # -> get_column ($position_column);
667  
668 1491 return $pos;
669 }
670  
671 =head2 _initial_position_value
672  
673 __PACKAGE__->_initial_position_value(0);
674  
675 This method specifies a B<value> of L</position_column> which is assigned
676 to the first inserted element of a group, if no value was supplied at
677 insertion time. All subsequent values are derived from this one by
678 L</_next_position_value> below. Defaults to 1.
679  
680 =cut
681  
682 329 __PACKAGE__->mk_classdata( '_initial_position_value' => 1 );
683  
684 =head2 _next_position_value
685  
686 my $new_value = $item->_next_position_value ( $position_value )
687  
688 Returns a position B<value> that would be considered C<next> with
689 regards to C<$position_value>. Can be pretty much anything, given
690 that C<< $position_value < $new_value >> where C<< < >> is the
691 SQL comparison operator (usually works fine on strings). The
692 default method expects C<$position_value> to be numeric, and
693 returns C<$position_value + 1>
694  
695 =cut
696 174 ▶sub _next_position_value {
Subroutine _next_position_value: called CC=1 SCAR=0.0
697 174 return $_[1] + 1;
698 }
699  
700 =head2 _shift_siblings
701  
702 $item->_shift_siblings ($direction, @between)
703  
704 Shifts all siblings with B<positions values> in the range @between
705 (inclusive) by one position as specified by $direction (left if < 0,
706 right if > 0). By default simply increments/decrements each
707 L</position_column> value by 1, doing so in a way as to not violate
708 any existing constraints.
709  
710 Note that if you override this method and have unique constraints
711 including the L</position_column> the shift is not a trivial task.
712 Refer to the implementation source of the default method for more
713 information.
714  
715 =cut
716 497 ▶sub _shift_siblings {
Subroutine _shift_siblings: called CC=6 SCAR=17.9
717 497 my ($self, $direction, @between) = @_;
718 497 ▶ return 0 unless $direction;
Branch: 1 branch
BranchTrueFalseTotal
unless $direction 0 497 497
719  
720 497 my $position_column = $self->position_column;
721  
722 497 my ($op, $ord);
723 497 ▶ if ($direction < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction < 0) { } 257 240 497
724 257 $op = '-';
725 257 $ord = 'asc';
726 }
727 else {
728 240 $op = '+';
729 240 $ord = 'desc';
730 }
731  
732 497 my $shift_rs = $self->_group_rs-> search ({ $position_column => { -between => \@between } });
733  
734 # some databases (sqlite, pg, perhaps others) are dumb and can not do a
735 # blanket increment/decrement without violating a unique constraint.
736 # So what we do here is check if the position column is part of a unique
737 # constraint, and do a one-by-one update if this is the case.
738 497 my $rsrc = $self->result_source;
739  
740 # set in case there are more cascades combined with $rs->update => $rs_update_all overrides
741 497 local $rsrc->schema->{_ORDERED_INTERNAL_UPDATE} = 1;
742 497 my @pcols = $rsrc->primary_columns;
743 497 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $position_column;} map({@$_;} values %{+{$rsrc->unique_constraints};})) { } 16 481 497
744 561 grep { $_ eq $position_column } ( map { @$_ } (values %{{ $rsrc->unique_constraints }} ) )
745 ) {
746 16 my $clean_rs = $rsrc->resultset;
747  
748 16 for ( $shift_rs->search (
749 {}, { order_by => { "-$ord", $position_column }, select => [$position_column, @pcols] }
750 )->cursor->all ) {
751 36 my $pos = shift @$_;
752 36 ▶ $clean_rs->find(@$_)->update ({ $position_column => $pos + ( ($op eq '+') ? 1 : -1 ) });
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 0 36 36
753 }
754 }
755 else {
756 481 $shift_rs->update ({ $position_column => \ "$position_column $op 1" } );
757 }
758 }
759  
760  
761 # This method returns a resultset containing all members of the row
762 # group (including the row itself).
763 1728 ▶sub _group_rs {
Subroutine _group_rs: called CC=1 SCAR=0.0
764 1728 my $self = shift;
765 1728 return $self->result_source->resultset->search({$self->_grouping_clause()});
766 }
767  
768 # Returns an unordered resultset of all objects in the same group
769 # excluding the object you called this method on.
770 1231 ▶sub _siblings {
Subroutine _siblings: called CC=2 SCAR=6.9
771 1231 my $self = shift;
772 1231 my $position_column = $self->position_column;
773 1231 my $pos;
774 1231 ▶ return defined ($pos = $self->get_column($position_column))
Branch: 1 branch
BranchTrueFalseTotal
defined($pos = $self->get_column($position_column)) ? : 972 259 1231
775 ? $self->_group_rs->search(
776 { $position_column => { '!=' => $pos } },
777 )
778 : $self->_group_rs
779 ;
780 }
781  
782 # Returns the B<absolute numeric position> of the current object, with the
783 # first object being at position 1, its sibling at position 2 and so on.
784 687 ▶sub _position {
Subroutine _position: called CC=1 SCAR=0.0
785 687 my $self = shift;
786 687 return $self->_position_from_value ($self->get_column ($self->position_column) );
787 }
788  
789 # This method returns one or more name=>value pairs for limiting a search
790 # by the grouping column(s). If the grouping column is not defined then
791 # this will return an empty list.
792 1760 ▶sub _grouping_clause {
Subroutine _grouping_clause: called CC=1 SCAR=0.0
793 1760 my( $self ) = @_;
794 1760 return map { $_ => $self->get_column($_) } $self->_grouping_columns();
795 }
796  
797 # Returns a list of the column names used for grouping, regardless of whether
798 # they were specified as an arrayref or a single string, and returns ()
799 # if there is no grouping.
800 2400 ▶sub _grouping_columns {
Subroutine _grouping_columns: called CC=3 SCAR=11.0
801 2400 my( $self ) = @_;
802 2400 my $col = $self->grouping_column();
803 2400 ▶ if (ref $col eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $col eq 'ARRAY') { } 1130 1270 2400
elsif ($col) { } 1118 152 1270
804 1130 return @$col;
805 } elsif ($col) {
806 1118 return ( $col );
807 } else {
808 152 return ();
809 }
810 }
811  
812 # Returns true if the object is in the group represented by hashref $other
813 19 ▶sub _is_in_group {
Subroutine _is_in_group: called CC=4 SCAR=14.3
814 19 my ($self, $other) = @_;
815 19 my $current = {$self->_grouping_clause};
816  
817 329 ▶ no warnings qw/uninitialized/;
Subroutine BEGIN: called CC=1 SCAR=0.0
818  
819 19 ▶ return 0 if (
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(keys %$current)) ne join("\0", sort(keys %$other)) 0 19 19
820 join ("\x00", sort keys %$current)
821 ne
822 join ("\x00", sort keys %$other)
823 );
824 19 for my $key (keys %$current) {
825 19 ▶ return 0 if $current->{$key} ne $other->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $current->{$key} ne $other->{$key} 19 0 19
826 }
827 0 return 1;
828 }
829  
830 # This is a short-circuited method, that is used internally by this
831 # module to update positioning values in isolation (i.e. without
832 # triggering any of the positioning integrity code).
833 #
834 # Some day you might get confronted by datasets that have ambiguous
835 # positioning data (e.g. duplicate position values within the same group,
836 # in a table without unique constraints). When manually fixing such data
837 # keep in mind that you can not invoke L<DBIx::Class::Row/update> like
838 # you normally would, as it will get confused by the wrong data before
839 # having a chance to update the ill-defined row. If you really know what
840 # you are doing use this method which bypasses any hooks introduced by
841 # this module.
842 518 ▶sub _ordered_internal_update {
Subroutine _ordered_internal_update: called CC=1 SCAR=0.0
843 518 my $self = shift;
844 518 local $self->result_source->schema->{_ORDERED_INTERNAL_UPDATE} = 1;
845 518 return $self->update (@_);
846 }
847  
848 329 1;
849  
850 __END__
« DBIx/Class/SQLMaker/MySQL.pm ↑ Summary DBIx/Class/InflateColumn.pm »