my $parent_column = $self->parent_column();
if ($new_parent) {
if (ref($new_parent)) {
- $new_parent = $new_parent->id();
+ $new_parent = $new_parent->id() || 0;
}
- return 0 if ($new_parent == $self->get_column($parent_column));
+ return 0 if ($new_parent == ($self->get_column($parent_column)||0));
my $positioned = $self->can('position_column');
$self->move_last if ($positioned);
$self->set_column( $parent_column => $new_parent );
my( $self ) = @_;
return (
$self->parent_column() =>
- $self->get_column($self->parent_column()) || 0
+ $self->get_column($self->parent_column())
);
}
__PACKAGE__->load_classes(qw/
Artist
+ Employee::Positioned
+ Employee::AdjacencyList
+ Employee::PositionedAdjacencyList
CD
#dummy
Track
use base 'DBIx::Class::Core';
-__PACKAGE__->load_components('Positioned','PK::Auto');
+__PACKAGE__->load_components('PK::Auto');
DBICTest::Schema::Artist->table('artist');
DBICTest::Schema::Artist->add_columns(
size => 100,
is_nullable => 1,
},
- position => {
- data_type => 'integer',
- },
);
DBICTest::Schema::Artist->set_primary_key('artistid');
-__PACKAGE__->position_column('position');
__PACKAGE__->mk_classdata('field_name_for', {
artistid => 'primary key',
name => 'artist name',
- position => 'list position',
});
1;
--- /dev/null
+package # hide from PAUSE
+ DBICTest::Schema::Employee::AdjacencyList;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw(
+ Tree::AdjacencyList
+ PK::Auto
+ Core
+));
+
+__PACKAGE__->table('employees_adjacencylist');
+
+__PACKAGE__->add_columns(
+ employee_id => {
+ data_type => 'integer',
+ is_auto_increment => 1
+ },
+ parent_id => {
+ data_type => 'integer',
+ is_nullable => 1,
+ },
+ name => {
+ data_type => 'varchar',
+ size => 100,
+ is_nullable => 1,
+ },
+);
+
+__PACKAGE__->set_primary_key('employee_id');
+__PACKAGE__->parent_column('parent_id');
+
+__PACKAGE__->mk_classdata('field_name_for', {
+ employee_id => 'primary key',
+ parent_id => 'parent id',
+ name => 'employee name',
+});
+
+1;
--- /dev/null
+package # hide from PAUSE
+ DBICTest::Schema::Employee::Positioned;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw( Positioned PK::Auto Core ));
+
+__PACKAGE__->table('employees_positioned');
+
+__PACKAGE__->add_columns(
+ employee_id => {
+ data_type => 'integer',
+ is_auto_increment => 1
+ },
+ position => {
+ data_type => 'integer',
+ },
+ name => {
+ data_type => 'varchar',
+ size => 100,
+ is_nullable => 1,
+ },
+);
+
+__PACKAGE__->set_primary_key('employee_id');
+__PACKAGE__->position_column('position');
+
+__PACKAGE__->mk_classdata('field_name_for', {
+ employee_id => 'primary key',
+ position => 'list position',
+ name => 'employee name',
+});
+
+1;
--- /dev/null
+package # hide from PAUSE
+ DBICTest::Schema::Employee::PositionedAdjacencyList;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw(
+ Tree::AdjacencyList
+ Positioned
+ PK::Auto
+ Core
+));
+
+__PACKAGE__->table('employees_positioned_adjacencylist');
+
+__PACKAGE__->add_columns(
+ employee_id => {
+ data_type => 'integer',
+ is_auto_increment => 1
+ },
+ parent_id => {
+ data_type => 'integer',
+ is_nullable => 1,
+ },
+ position => {
+ data_type => 'integer',
+ },
+ name => {
+ data_type => 'varchar',
+ size => 100,
+ is_nullable => 1,
+ },
+);
+
+__PACKAGE__->set_primary_key('employee_id');
+__PACKAGE__->position_column('position');
+__PACKAGE__->parent_column('parent_id');
+
+__PACKAGE__->mk_classdata('field_name_for', {
+ employee_id => 'primary key',
+ parent_id => 'parent id',
+ position => 'list position',
+ name => 'employee name',
+});
+
+1;
--
-- Created by SQL::Translator::Producer::SQLite
--- Created on Sun Mar 19 19:16:50 2006
+-- Created on Tue Mar 21 12:11:03 2006
--
BEGIN TRANSACTION;
--
+-- Table: employees_positioned
+--
+CREATE TABLE employees_positioned (
+ employee_id INTEGER PRIMARY KEY NOT NULL,
+ position integer NOT NULL,
+ name varchar(100)
+);
+
+--
-- Table: serialized
--
CREATE TABLE serialized (
);
--
--- Table: twokeys
+-- Table: cd_to_producer
--
-CREATE TABLE twokeys (
- artist integer NOT NULL,
+CREATE TABLE cd_to_producer (
cd integer NOT NULL,
- PRIMARY KEY (artist, cd)
+ producer integer NOT NULL,
+ PRIMARY KEY (cd, producer)
);
--
);
--
--- Table: cd_to_producer
+-- Table: employees_adjacencylist
--
-CREATE TABLE cd_to_producer (
- cd integer NOT NULL,
- producer integer NOT NULL,
- PRIMARY KEY (cd, producer)
+CREATE TABLE employees_adjacencylist (
+ employee_id INTEGER PRIMARY KEY NOT NULL,
+ parent_id integer,
+ name varchar(100)
);
--
--
CREATE TABLE artist (
artistid INTEGER PRIMARY KEY NOT NULL,
- name varchar,
- position INTEGER
+ name varchar(100)
+);
+
+--
+-- Table: employees_positioned_adjacencylist
+--
+CREATE TABLE employees_positioned_adjacencylist (
+ employee_id INTEGER PRIMARY KEY NOT NULL,
+ parent_id integer,
+ position integer NOT NULL,
+ name varchar(100)
);
--
);
--
--- Table: fourkeys
---
-CREATE TABLE fourkeys (
- foo integer NOT NULL,
- bar integer NOT NULL,
- hello integer NOT NULL,
- goodbye integer NOT NULL,
- PRIMARY KEY (foo, bar, hello, goodbye)
-);
-
---
-- Table: cd
--
CREATE TABLE cd (
);
--
--- Table: artist_undirected_map
---
-CREATE TABLE artist_undirected_map (
- id1 integer NOT NULL,
- id2 integer NOT NULL,
- PRIMARY KEY (id1, id2)
-);
-
---
--- Table: onekey
---
-CREATE TABLE onekey (
- id INTEGER PRIMARY KEY NOT NULL,
- artist integer NOT NULL,
- cd integer NOT NULL
-);
-
---
-- Table: track
--
CREATE TABLE track (
);
--
--- Table: producer
+-- Table: treelike
--
-CREATE TABLE producer (
- producerid INTEGER PRIMARY KEY NOT NULL,
+CREATE TABLE treelike (
+ id INTEGER PRIMARY KEY NOT NULL,
+ parent integer NOT NULL,
name varchar(100) NOT NULL
);
);
--
--- Table: treelike
+-- Table: twokeys
--
-CREATE TABLE treelike (
+CREATE TABLE twokeys (
+ artist integer NOT NULL,
+ cd integer NOT NULL,
+ PRIMARY KEY (artist, cd)
+);
+
+--
+-- Table: fourkeys
+--
+CREATE TABLE fourkeys (
+ foo integer NOT NULL,
+ bar integer NOT NULL,
+ hello integer NOT NULL,
+ goodbye integer NOT NULL,
+ PRIMARY KEY (foo, bar, hello, goodbye)
+);
+
+--
+-- Table: artist_undirected_map
+--
+CREATE TABLE artist_undirected_map (
+ id1 integer NOT NULL,
+ id2 integer NOT NULL,
+ PRIMARY KEY (id1, id2)
+);
+
+--
+-- Table: onekey
+--
+CREATE TABLE onekey (
id INTEGER PRIMARY KEY NOT NULL,
- parent integer NOT NULL,
+ artist integer NOT NULL,
+ cd integer NOT NULL
+);
+
+--
+-- Table: producer
+--
+CREATE TABLE producer (
+ producerid INTEGER PRIMARY KEY NOT NULL,
name varchar(100) NOT NULL
);
'data_type' => 'varchar',
'is_nullable' => 0,
},
- 'position' => {
- 'data_type' => 'INTEGER',
- 'is_nullable' => 0,
- 'size' => undef,
- },
};
is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
sub run_tests {
+ plan tests => 96;
my $schema = shift;
- my $artists = $schema->resultset("Artist");
- $artists->delete();
- $artists->create({ artistid=>1, name=>"Joe" });
- $artists->create({ artistid=>2, name=>"Bob" });
- $artists->create({ artistid=>3, name=>"Ted" });
- $artists->create({ artistid=>4, name=>"Ned" });
- $artists->create({ artistid=>5, name=>"Don" });
+ foreach my $class ( 'Positioned', 'PositionedAdjacencyList', 'AdjacencyList' ) {
- $artists = $artists->search(undef,{order_by=>'position'});
+ my $employees = $schema->resultset('Employee::'.$class);
- plan tests => 230;
+ if ($employees->result_class->can('position_column')) {
- check_positions($schema);
+ $employees->delete();
+ foreach (1..5) {
+ $employees->create({ name=>'temp' });
+ }
- my $artist;
+ $employees = $employees->search(undef,{order_by=>'position'});
+ ok( check_positions($employees), "$class: intial positions" );
- foreach my $position (1..5) {
+ my $employee;
- $artist = $artists->find({ position=>$position });
- $artist->move_previous();
- check_positions($schema);
+ foreach my $position (1..$employees->count()) {
- $artist = $artists->find({ position=>$position });
- $artist->move_next();
- check_positions($schema);
+ $employee = $employees->find({ position=>$position });
+ $employee->move_previous();
+ ok( check_positions($employees), "$class: move_previous( $position )" );
- $artist = $artists->find({ position=>$position });
- $artist->move_first();
- check_positions($schema);
+ $employee = $employees->find({ position=>$position });
+ $employee->move_next();
+ ok( check_positions($employees), "$class: move_next( $position )" );
- $artist = $artists->find({ position=>$position });
- $artist->move_last();
- check_positions($schema);
+ $employee = $employees->find({ position=>$position });
+ $employee->move_first();
+ ok( check_positions($employees), "$class: move_first( $position )" );
- foreach my $to_position (1..5) {
+ $employee = $employees->find({ position=>$position });
+ $employee->move_last();
+ ok( check_positions($employees), "$class: move_last( $position )" );
- $artist = $artists->find({ position=>$position });
- $artist->move_to($to_position);
- check_positions($schema);
+ foreach my $to_position (1..$employees->count()) {
+ $employee = $employees->find({ position=>$position });
+ $employee->move_to($to_position);
+ ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
+ }
+ }
}
+ if ($employees->result_class->can('parent_column')) {
+ $employees->delete();
+ my $mom = $employees->create({ name=>'temp' });
+ foreach (1..14) {
+ $employees->create({ name=>'temp', parent_id=>$mom->id() });
+ }
+
+ my $children = $mom->children();
+ ok( ($children->count()==14), 'correct number of children' );
+
+ my $grandma = $mom;
+ my @children = $children->all();
+ $mom = pop(@children);
+ foreach my $child (@children) {
+ $child->parent( $mom );
+ }
+
+ ok( ($mom->children->count() == 13), 'correct number of grandchildren' );
+
+ if ($employees->result_class->can('position_column')) {
+ # TODO: Test positioning within a tree.
+ }
+ }
}
}
sub check_positions {
- my $schema = shift;
- my $artists = $schema->resultset("Artist")->search(undef,{order_by=>'position'});
+ my( $employees ) = @_;
+ $employees->reset();
my $expected_position = 0;
- while (my $artist = $artists->next()) {
+ my $is_ok = 1;
+ while (my $employee = $employees->next()) {
$expected_position ++;
- ok( ($artist->position()==$expected_position), 'default positions set as expected' );
+ if ($employee->position()!=$expected_position) {
+ $is_ok = 0;
+ last;
+ }
}
+ return $is_ok;
}
1;