DBIx::Class::Ordered(3p)

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.

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

Returns either a resultset or an array of all other objects excluding the one you called it on.

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

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

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

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

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

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

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

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

  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.

PRIVATE METHODS
These methods are used internally. You should never have the need to use them.

  _grouping_clause
This method returns one or more name=>value pairs for limiting a search by the grouping column(s). If the grouping column is not defined then this will return an empty list.

  _get_grouping_columns
Returns a list of the column names used for grouping, regardless of whether they were specified as an arrayref or a single string, and returns () if there is no grouping.

  _is_in_group($other)
    $item->_is_in_group( {user => 'fred', list => 'work'} )

Returns true if the object is in the group represented by hashref $other

BUGS

  Unique Constraints
Unique indexes and constraints on the position column are not supported at this time. It would be make sense to support them, but there are some unexpected database issues that make this hard to do. The main problem from the author's view is that SQLite (the DB engine that we use for testing) does not support ORDER BY on updates.

  Race Condition on Insert
If a position is not specified for an insert than a position will be chosen based on COUNT(*)+1. But, it first selects the count, and then inserts the record. The space of time between select and insert introduces a race condition. To fix this we need the ability to lock tables in DBIC. I've added an entry in the TODO about this.

  Multiple Moves
Be careful when issueing move_* methods to multiple objects. If you've pre-loaded the objects then when you move one of the objects the position of the other object will not reflect their new value until you reload them from the database.

There are times when you will want to move objects as groups, such as changeing the parent of several objects at once - this directly conflicts with this problem. One solution is for us to write a ResultSet class that supports a parent() method, for example. Another solution is to somehow automagically modify the objects that exist in the current object's result set to have the new position value.

AUTHOR
Aran Deltac <bluefeet@cpan.org>

LICENSE
You may distribute this code under the same terms as Perl itself.