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