CONTENTS

NAME

DBIx::Class::Ordered - Modify the position of objects in an ordered list.

SYNOPSIS

Create a table for your ordered data.

CREATE TABLE items (
  item_id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  position INTEGER NOT NULL
);

Optionally, add one or more columns to specify groupings, allowing you to maintain independent ordered lists within one table:

CREATE TABLE items (
  item_id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  position INTEGER NOT NULL,
  group_id INTEGER NOT NULL
);

Or even

CREATE TABLE items (
  item_id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  position INTEGER NOT NULL,
  group_id INTEGER NOT NULL,
  other_group_id INTEGER NOT NULL
);

In your Schema or DB class add "Ordered" to the top of the component list.

__PACKAGE__->load_components(qw( Ordered ... ));

Specify the column that stores the position number for each row.

package My::Item;
__PACKAGE__->position_column('position');

If you are using one grouping column, specify it as follows:

__PACKAGE__->grouping_column('group_id');

Or if you have multiple grouping columns:

__PACKAGE__->grouping_column(['group_id', 'other_group_id']);

That's it, now you can change the position of your objects.

#!/use/bin/perl
use My::Item;

my $item = My::Item->create({ name=>'Matt S. Trout' });
# If using grouping_column:
my $item = My::Item->create({ name=>'Matt S. Trout', group_id=>1 });

my $rs = $item->siblings();
my @siblings = $item->siblings();

my $sibling;
$sibling = $item->first_sibling();
$sibling = $item->last_sibling();
$sibling = $item->previous_sibling();
$sibling = $item->next_sibling();

$item->move_previous();
$item->move_next();
$item->move_first();
$item->move_last();
$item->move_to( $position );
$item->move_to_group( 'groupname' );
$item->move_to_group( 'groupname', $position );
$item->move_to_group( {group_id=>'groupname', 'other_group_id=>'othergroupname'} );
$item->move_to_group( {group_id=>'groupname', 'other_group_id=>'othergroupname'}, $position );

DESCRIPTION

This module provides a simple interface for modifying the ordered position of DBIx::Class objects.

AUTO UPDATE

All of the move_* methods automatically update the rows involved in the query. This is not configurable and is due to the fact that if you move a record it always causes other records in the list to be updated.

METHODS

position_column

__PACKAGE__->position_column('position');

Sets and retrieves the name of the column that stores the positional value of each record. Defaults to "position".

grouping_column

__PACKAGE__->grouping_column('group_id');

This method specifies a column to limit all queries in this module by. This effectively allows you to have multiple ordered lists within the same table.

null_position_value

__PACKAGE__->null_position_value(undef);

This method specifies a value of "position_column" which would never be assigned to a row during normal operation. When a row is moved, its position is set to this value temporarily, so that any unique constraints can not be violated. This value defaults to 0, which should work for all cases except when your positions do indeed start from 0.

siblings

my $rs = $item->siblings();
my @siblings = $item->siblings();

Returns an ordered resultset of all other objects in the same group excluding the one you called it on.

The ordering is a backwards-compatibility artifact - if you need a resultset with no ordering applied use _siblings

previous_siblings

my $prev_rs = $item->previous_siblings();
my @prev_siblings = $item->previous_siblings();

Returns a resultset of all objects in the same group positioned before the object on which this method was called.

next_siblings

my $next_rs = $item->next_siblings();
my @next_siblings = $item->next_siblings();

Returns a resultset of all objects in the same group positioned after the object on which this method was called.

previous_sibling

my $sibling = $item->previous_sibling();

Returns the sibling that resides one position back. Returns 0 if the current object is the first one.

first_sibling

my $sibling = $item->first_sibling();

Returns the first sibling object, or 0 if the first sibling is this sibling.

next_sibling

my $sibling = $item->next_sibling();

Returns the sibling that resides one position forward. Returns 0 if the current object is the last one.

last_sibling

my $sibling = $item->last_sibling();

Returns the last sibling, or 0 if the last sibling is this sibling.

move_previous

$item->move_previous();

Swaps position with the sibling in the position previous in the list. Returns 1 on success, and 0 if the object is already the first one.

move_next

$item->move_next();

Swaps position with the sibling in the next position in the list. Returns 1 on success, and 0 if the object is already the last in the list.

move_first

$item->move_first();

Moves the object to the first position in the list. Returns 1 on success, and 0 if the object is already the first.

move_last

$item->move_last();

Moves the object to the last position in the list. Returns 1 on success, and 0 if the object is already the last one.

move_to

$item->move_to( $position );

Moves the object to the specified position. Returns 1 on success, and 0 if the object is already at the specified position.

move_to_group

$item->move_to_group( $group, $position );

Moves the object to the specified position of the specified group, or to the end of the group if $position is undef. 1 is returned on success, and 0 is returned if the object is already at the specified position of the specified group.

$group may be specified as a single scalar if only one grouping column is in use, or as a hashref of column => value pairs if multiple grouping columns are in use.

insert

Overrides the DBIC insert() method by providing a default position number. The default will be the number of rows in the table +1, thus positioning the new record at the last position.

update

Overrides the DBIC update() method by checking for a change to the position and/or group columns. Movement within a group or to another group is handled by repositioning the appropriate siblings. Position defaults to the end of a new group if it has been changed to undef.

delete

Overrides the DBIC delete() method by first moving the object to the last position, then deleting it, thus ensuring the integrity of the positions.

METHODS FOR EXTENDING ORDERED

You would want to override the methods below if you use sparse (non-linear) or non-numeric position values. This can be useful if you are working with preexisting non-normalised position data, or if you need to work with materialized path columns.

_position_from_value

my $num_pos = $item->_position_from_value ( $pos_value )

Returns the absolute numeric position of an object with a position value set to $pos_value. By default simply returns $pos_value.

_position_value

my $pos_value = $item->_position_value ( $pos )

Returns the value of "position_column" of the object at numeric position $pos. By default simply returns $pos.

_initial_position_value

__PACKAGE__->_initial_position_value(0);

This method specifies a value of "position_column" which is assigned to the first inserted element of a group, if no value was supplied at insertion time. All subsequent values are derived from this one by "_next_position_value" below. Defaults to 1.

_next_position_value

my $new_value = $item->_next_position_value ( $position_value )

Returns a position value that would be considered next with regards to $position_value. Can be pretty much anything, given that $position_value < $new_value where < is the SQL comparison operator (usually works fine on strings). The default method expects $position_value to be numeric, and returns $position_value + 1

_shift_siblings

$item->_shift_siblings ($direction, @between)

Shifts all siblings with positions values in the range @between (inclusive) by one position as specified by $direction (left if < 0, right if > 0). By default simply increments/decrements each "position_column" value by 1, doing so in a way as to not violate any existing constraints.

Note that if you override this method and have unique constraints including the "position_column" the shift is not a trivial task. Refer to the implementation source of the default method for more information.

CAVEATS

Resultset Methods

Note that all Insert/Create/Delete overrides are happening on DBIx::Class::Row methods only. If you use the DBIx::Class::ResultSet versions of update or delete, all logic present in this module will be bypassed entirely (possibly resulting in a broken order-tree). Instead always use the update_all and delete_all methods, which will invoke the corresponding row method on every member of the given resultset.

Race Condition on Insert

If a position is not specified for an insert, a position will be chosen based either on "_initial_position_value" or "_next_position_value", depending if there are already some items in the current group. The space of time between the necessary selects and insert introduces a race condition. Having unique constraints on your position/group columns, and using transactions (see "txn_do" in DBIx::Class::Storage) will prevent such race conditions going undetected.

Multiple Moves

If you have multiple same-group result objects already loaded from storage, you need to be careful when executing move_* operations on them: without a "position_column" reload the "_position_value" of the "siblings" will be out of sync with the underlying storage.

Starting from version 0.082800 DBIC will implicitly perform such reloads when the move_* happens as a part of a transaction (a good example of such situation is $ordered_resultset->delete_all).

If it is not possible for you to wrap the entire call-chain in a transaction, you will need to call "discard_changes" in DBIx::Class::Row to get an object up-to-date before proceeding, otherwise undefined behavior will result.

Default Values

Using a database defined default_value on one of your group columns could result in the position not being assigned correctly.

FURTHER QUESTIONS?

Check the list of additional DBIC resources.

COPYRIGHT AND LICENSE

This module is free software copyright by the DBIx::Class (DBIC) authors. You can redistribute it and/or modify it under the same terms as the DBIx::Class library.