Merge 'trunk' into 'DBIx-Class-current'
Daniel Westermann-Clark [Wed, 12 Apr 2006 15:03:00 +0000 (15:03 +0000)]
r8443@fortuna (orig r1442):  dwc | 2006-04-11 23:29:12 -0400
POD fix for search.cpan.org
r8446@fortuna (orig r1443):  dwc | 2006-04-12 11:01:57 -0400
Fix for -and conditions when updating or deleting on a ResultSet

27 files changed:
Changes
TODO
lib/DBIx/Class/Manual/Component.pod
lib/DBIx/Class/Ordered.pm [new file with mode: 0644]
lib/DBIx/Class/ResultSet.pm
lib/DBIx/Class/ResultSetColumn.pm [new file with mode: 0644]
lib/DBIx/Class/ResultSource.pm
lib/DBIx/Class/ResultSourceProxy.pm
lib/DBIx/Class/Row.pm
lib/DBIx/Class/Schema.pm
lib/DBIx/Class/Storage/DBI.pm
lib/SQL/Translator/Parser/DBIx/Class.pm
maint/inheritance_pod.pl [new file with mode: 0755]
t/basicrels/28result_set_column.t [new file with mode: 0644]
t/helperrels/26sqlt.t
t/helperrels/27ordered.t [new file with mode: 0644]
t/helperrels/28result_set_column.t [new file with mode: 0644]
t/lib/DBICTest/Schema.pm
t/lib/DBICTest/Schema/ArtistSourceName.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Employee.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/TwoKeyTreeLike.pm
t/lib/sqlite.sql
t/run/01core.tl
t/run/04db.tl
t/run/145db2.tl
t/run/27ordered.tl [new file with mode: 0644]
t/run/28result_set_column.tl [new file with mode: 0644]

diff --git a/Changes b/Changes
index d06e1bf..ea392d3 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,10 @@
 Revision history for DBIx::Class
 
+        - added remove_column(s) to ResultSource/ResultSourceProxy
+        - added add_column alias to ResultSourceProxy
+        - added source_name to ResultSource
+       - load_classes now uses source_name and sets it if necessary
+
 0.06002
         - fix for -and conditions when updating or deleting on a ResultSet
 
@@ -77,7 +82,7 @@ Revision history for DBIx::Class
         - remove build dependency on version.pm
 
 0.05004 2006-02-13 20:59:00
-        - allow specification of related columns via cols attr when primary 
+        - allow specification of related columns via cols attr when primary
           keys of the related table are not fetched
         - fix count for group_by as scalar
         - add horrific fix to make Oracle's retarded limit syntax work
diff --git a/TODO b/TODO
index d0726b3..055e5c4 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,3 +1,18 @@
+2006-04-11 by castaway
+ - using PK::Auto should set is_auto_increment for the PK columns, so that copy() "just works"
+ - docs of copy() should say that is_auto_increment is essential for auto_incrementing keys
+
+2006-03-25 by mst
+  - Refactor ResultSet::new to be less hairy
+    - we should move the setup of select, as, and from out of here
+      - these should be local rs attrs, not main attrs, and extra joins
+        provided on search should be merged
+  - find a way to un-wantarray search without breaking compat
+  - audit logging component
+  - delay relationship setup if done via ->load_classes
+  - double-sided relationships
+  - incremental deploy
+  - make short form of class specifier in relationships work
 
 2006-01-31 by bluefeet
  - Create a DBIx::Class::FilterColumn to replace inflate/deflate. This 
    We should still support the old inflate/deflate syntax, but this new 
    way should be recommended. 
 
-2006-02-07 by JR
+2006-02-07 by castaway
  - Extract DBIC::SQL::Abstract into a separate module for CPAN
  - Chop PK::Auto::Foo up to have PK::Auto refer to an appropriate
    DBIx::Storage::DBI::Foo, which will be loaded on connect from Driver info?
+(done -> 0.06001!)
  - Add deploy method to Schema, which will create DB tables from Schema, via
    SQLT
+(sorta done)
 
 2006-03-18 by bluefeet
  - Support table locking.
 
+2006-03-21 by bluefeet
+ - When subclassing a dbic class make it so you don't have to do 
+   __PACKAGE__->table(__PACKAGE__->table()); for the result set to 
+   return the correct object type.
+
+2006-03-27 by mst
+ Add the ability for deploy to be given a directory and grab <dbname>.sql 
+ out of there if available. Try SQL::Translator if not. If none of the above, 
+ cry (and die()).  Then you can have a script that pre-gens for all available 
+ SQLT modules so an app can do its own deploy without SQLT on the target 
+ system
+
index 2607e36..9bbe684 100644 (file)
@@ -90,6 +90,8 @@ L<DBIx::Class::FormTools> - Build forms with multiple interconnected objects.
 
 L<DBIx::Class::HTMLWidget> - Like FromForm but with DBIx::Class and HTML::Widget.
 
+L<DBIx::Class::Ordered> - Modify the position of objects in an ordered list.
+
 L<DBIx::Class::PK::Auto> - Retrieve automatically created primary keys upon insert.
 
 L<DBIx::Class::QueriesTime> - Display the amount of time it takes to run queries.
diff --git a/lib/DBIx/Class/Ordered.pm b/lib/DBIx/Class/Ordered.pm
new file mode 100644 (file)
index 0000000..ba5514d
--- /dev/null
@@ -0,0 +1,385 @@
+# vim: ts=8:sw=4:sts=4:et
+package DBIx::Class::Ordered;
+use strict;
+use warnings;
+use base qw( DBIx::Class );
+
+=head1 NAME
+
+DBIx::Class::Ordered - Modify the position of objects in an ordered list.
+
+=head1 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
+  );
+  # Optional: 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');
+  __PACKAGE__->grouping_column('group_id'); # optional
+
+Thats 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 );
+
+=head1 DESCRIPTION
+
+This module provides a simple interface for modifying the ordered 
+position of DBIx::Class objects.
+
+=head1 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.
+
+=head1 METHODS
+
+=head2 position_column
+
+  __PACKAGE__->position_column('position');
+
+Sets and retrieves the name of the column that stores the 
+positional value of each record.  Default to "position".
+
+=cut
+
+__PACKAGE__->mk_classdata( 'position_column' => 'position' );
+
+=head2 grouping_column
+
+  __PACKAGE__->grouping_column('group_id');
+
+This method specified a column to limit all queries in 
+this module by.  This effectively allows you to have multiple 
+ordered lists within the same table.
+
+=cut
+
+__PACKAGE__->mk_classdata( 'grouping_column' );
+
+=head2 siblings
+
+  my $rs = $item->siblings();
+  my @siblings = $item->siblings();
+
+Returns either a result set or an array of all other objects 
+excluding the one you called it on.
+
+=cut
+
+sub siblings {
+    my( $self ) = @_;
+    my $position_column = $self->position_column;
+    my $rs = $self->result_source->resultset->search(
+        {
+            $position_column => { '!=' => $self->get_column($position_column) },
+            $self->_grouping_clause(),
+        },
+        { order_by => $self->position_column },
+    );
+    return $rs->all() if (wantarray());
+    return $rs;
+}
+
+=head2 first_sibling
+
+  my $sibling = $item->first_sibling();
+
+Returns the first sibling object, or 0 if the first sibling 
+is this sibliing.
+
+=cut
+
+sub first_sibling {
+    my( $self ) = @_;
+    return 0 if ($self->get_column($self->position_column())==1);
+    return ($self->result_source->resultset->search(
+        {
+            $self->position_column => 1,
+            $self->_grouping_clause(),
+        },
+    )->all())[0];
+}
+
+=head2 last_sibling
+
+  my $sibling = $item->last_sibling();
+
+Return the last sibling, or 0 if the last sibling is this 
+sibling.
+
+=cut
+
+sub last_sibling {
+    my( $self ) = @_;
+    my $count = $self->result_source->resultset->search({$self->_grouping_clause()})->count();
+    return 0 if ($self->get_column($self->position_column())==$count);
+    return ($self->result_source->resultset->search(
+        {
+            $self->position_column => $count,
+            $self->_grouping_clause(),
+        },
+    )->all())[0];
+}
+
+=head2 previous_sibling
+
+  my $sibling = $item->previous_sibling();
+
+Returns the sibling that resides one position back.  Undef 
+is returned if the current object is the first one.
+
+=cut
+
+sub previous_sibling {
+    my( $self ) = @_;
+    my $position_column = $self->position_column;
+    my $position = $self->get_column( $position_column );
+    return 0 if ($position==1);
+    return ($self->result_source->resultset->search(
+        {
+            $position_column => $position - 1,
+            $self->_grouping_clause(),
+        }
+    )->all())[0];
+}
+
+=head2 next_sibling
+
+  my $sibling = $item->next_sibling();
+
+Returns the sibling that resides one position foward.  Undef 
+is returned if the current object is the last one.
+
+=cut
+
+sub next_sibling {
+    my( $self ) = @_;
+    my $position_column = $self->position_column;
+    my $position = $self->get_column( $position_column );
+    my $count = $self->result_source->resultset->search({$self->_grouping_clause()})->count();
+    return 0 if ($position==$count);
+    return ($self->result_source->resultset->search(
+        {
+            $position_column => $position + 1,
+            $self->_grouping_clause(),
+        },
+    )->all())[0];
+}
+
+=head2 move_previous
+
+  $item->move_previous();
+
+Swaps position with the sibling on position previous in the list.  
+1 is returned on success, and 0 is returned if the objects is already 
+the first one.
+
+=cut
+
+sub move_previous {
+    my( $self ) = @_;
+    my $position = $self->get_column( $self->position_column() );
+    return $self->move_to( $position - 1 );
+}
+
+=head2 move_next
+
+  $item->move_next();
+
+Swaps position with the sibling in the next position.  1 is returned on 
+success, and 0 is returned if the object is already the last in the list.
+
+=cut
+
+sub move_next {
+    my( $self ) = @_;
+    my $position = $self->get_column( $self->position_column() );
+    my $count = $self->result_source->resultset->search({$self->_grouping_clause()})->count();
+    return 0 if ($position==$count);
+    return $self->move_to( $position + 1 );
+}
+
+=head2 move_first
+
+  $item->move_first();
+
+Moves the object to the first position.  1 is returned on 
+success, and 0 is returned if the object is already the first.
+
+=cut
+
+sub move_first {
+    my( $self ) = @_;
+    return $self->move_to( 1 );
+}
+
+=head2 move_last
+
+  $item->move_last();
+
+Moves the object to the very last position.  1 is returned on 
+success, and 0 is returned if the object is already the last one.
+
+=cut
+
+sub move_last {
+    my( $self ) = @_;
+    my $count = $self->result_source->resultset->search({$self->_grouping_clause()})->count();
+    return $self->move_to( $count );
+}
+
+=head2 move_to
+
+  $item->move_to( $position );
+
+Moves the object to the specified position.  1 is returned on 
+success, and 0 is returned if the object is already at the 
+specified position.
+
+=cut
+
+sub move_to {
+    my( $self, $to_position ) = @_;
+    my $position_column = $self->position_column;
+    my $from_position = $self->get_column( $position_column );
+    return 0 if ( $to_position < 1 );
+    return 0 if ( $from_position==$to_position );
+    my $rs = $self->result_source->resultset->search({
+        -and => [
+            $position_column => { ($from_position>$to_position?'<':'>') => $from_position },
+            $position_column => { ($from_position>$to_position?'>=':'<=') => $to_position },
+        ],
+        $self->_grouping_clause(),
+    });
+    my $op = ($from_position>$to_position) ? '+' : '-';
+    $rs->update({
+        $position_column => \"$position_column $op 1",
+    });
+    $self->set_column( $position_column => $to_position );
+    $self->update();
+    return 1;
+}
+
+=head2 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.
+
+=cut
+
+sub insert {
+    my $self = shift;
+    my $position_column = $self->position_column;
+    $self->set_column( $position_column => $self->result_source->resultset->search( {$self->_grouping_clause()} )->count()+1 ) 
+        if (!$self->get_column($position_column));
+    return $self->next::method( @_ );
+}
+
+=head2 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.
+
+=cut
+
+sub delete {
+    my $self = shift;
+    $self->move_last;
+    return $self->next::method( @_ );
+}
+
+=head1 PRIVATE METHODS
+
+These methods are used internally.  You should never have the 
+need to use them.
+
+=head2 _grouping_clause
+
+This method returns a name=>value pare for limiting a search 
+by the collection column.  If the collection column is not 
+defined then this will return an empty list.
+
+=cut
+
+sub _grouping_clause {
+    my( $self ) = @_;
+    my $col = $self->grouping_column();
+    if ($col) {
+        return ( $col => $self->get_column($col) );
+    }
+    return ();
+}
+
+1;
+__END__
+
+=head1 BUGS
+
+=head2 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 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.
+
+=head2 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.
+
+The 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.
+
+=head1 AUTHOR
+
+Aran Deltac <bluefeet@cpan.org>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
index e0daae0..3541516 100644 (file)
@@ -10,6 +10,7 @@ use Data::Page;
 use Storable;
 use Scalar::Util qw/weaken/;
 
+use DBIx::Class::ResultSetColumn;
 use base qw/DBIx::Class/;
 __PACKAGE__->load_components(qw/AccessorGroup/);
 __PACKAGE__->mk_group_accessors('simple' => qw/result_source result_class/);
@@ -414,6 +415,28 @@ sub single {
   return (@data ? $self->_construct_object(@data) : ());
 }
 
+=head2 get_column
+
+=over 4
+
+=item Arguments: $cond?
+
+=item Return Value: $resultsetcolumn
+
+=back
+
+  my $max_length = $rs->get_column('length')->max;
+
+Returns a ResultSetColumn instance for $column based on $self
+
+=cut
+
+sub get_column {
+  my ($self, $column) = @_;
+
+  my $new = DBIx::Class::ResultSetColumn->new($self, $column);
+  return $new;
+}
 
 =head2 search_like
 
diff --git a/lib/DBIx/Class/ResultSetColumn.pm b/lib/DBIx/Class/ResultSetColumn.pm
new file mode 100644 (file)
index 0000000..35f8fa4
--- /dev/null
@@ -0,0 +1,184 @@
+package DBIx::Class::ResultSetColumn;
+use strict;
+use warnings;
+use base 'DBIx::Class';
+
+=head1 NAME
+
+  DBIx::Class::ResultSetColumn - helpful methods for messing
+  with a single column of the resultset
+
+=head1 SYNOPSIS
+
+  $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
+  $rs_column = $rs->get_column('year');
+  $max_year = $rs_column->max; #returns latest year
+
+=head1 DESCRIPTION
+
+A convenience class used to perform operations on a specific column of a resultset.
+
+=cut
+
+=head1 METHODS
+
+=head2 new
+
+  my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
+
+Creates a new resultset column object from the resultset and column passed as params
+
+=cut
+
+sub new {
+  my ($class, $rs, $column) = @_;
+  $class = ref $class if ref $class;
+
+  my $object_ref = { _column => $column,
+                    _parent_resultset => $rs };
+  
+  my $new = bless $object_ref, $class;
+  $new->throw_exception("column must be supplied") unless ($column);
+  return $new;
+}
+
+=head2 next
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: $value
+
+=back
+
+Returns the next value of the column in the resultset (C<undef> is there is none).
+
+Much like $rs->next but just returning the one value
+
+=cut
+
+sub next {
+  my $self = shift;
+    
+  $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
+  my ($row) = $self->{_resultset}->cursor->next;
+  return $row;
+}
+
+=head2 all
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: @values
+
+=back
+
+Returns all values of the column in the resultset (C<undef> is there are none).
+
+Much like $rs->all but returns values rather than row objects
+
+=cut
+
+sub all {
+  my $self = shift;
+  return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all;
+}
+
+=head2 min
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: $lowest_value
+
+=back
+
+Wrapper for ->func. Returns the lowest value of the column in the resultset (C<undef> is there are none).
+
+=cut
+
+sub min {
+  my $self = shift;
+  return $self->func('MIN');
+}
+
+=head2 max
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: $highest_value
+
+=back
+
+Wrapper for ->func. Returns the highest value of the column in the resultset (C<undef> is there are none).
+
+=cut
+
+sub max {
+  my $self = shift;
+  return $self->func('MAX');
+}
+
+=head2 sum
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: $sum_of_values
+
+=back
+
+Wrapper for ->func. Returns the sum of all the values in the column of the resultset. Use on varchar-like columns at your own risk.
+
+=cut
+
+sub sum {
+  my $self = shift;
+  return $self->func('SUM');
+}
+
+=head2 func
+
+=over 4
+
+=item Arguments: $function
+
+=item Return Value: $function_return_value
+
+=back
+
+Runs a query using the function on the column and returns the value. For example 
+  $rs = $schema->resultset("CD")->search({});
+  $length = $rs->get_column('title')->func('LENGTH');
+
+Produces the following SQL
+  SELECT LENGTH( title ) from cd me
+
+=cut
+
+sub func {
+  my $self = shift;
+  my $function = shift;
+
+  my ($row) = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor->next;
+  return $row;
+}
+
+1;
+
+=head1 AUTHORS
+
+Luke Saunders <luke.saunders@gmail.com>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
index 68d16b2..1caf03c 100644 (file)
@@ -15,7 +15,7 @@ __PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns
   schema from _relationships/);
 
 __PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class
-  result_class/);
+  result_class source_name/);
 
 =head1 NAME
 
@@ -127,7 +127,7 @@ Convenience alias to add_columns.
 sub add_columns {
   my ($self, @cols) = @_;
   $self->_ordered_columns(\@cols) unless $self->_ordered_columns;
-  
+
   my @added;
   my $columns = $self->_columns;
   while (my $col = shift @cols) {
@@ -205,6 +205,41 @@ sub columns {
   return @{$self->{_ordered_columns}||[]};
 }
 
+=head2 remove_columns
+
+  $table->remove_columns(qw/col1 col2 col3/);
+
+Removes columns from the result source.
+
+=head2 remove_column
+
+  $table->remove_column('col');
+
+Convenience alias to remove_columns.
+
+=cut
+
+sub remove_columns {
+  my ($self, @cols) = @_;
+
+  return unless $self->_ordered_columns;
+
+  my $columns = $self->_columns;
+  my @remaining;
+
+  foreach my $col (@{$self->_ordered_columns}) {
+    push @remaining, $col unless grep(/$col/, @cols);
+  }
+
+  foreach (@cols) {
+    undef $columns->{$_};
+  };
+
+  $self->_ordered_columns(\@remaining);
+}
+
+*remove_column = \&remove_columns;
+
 =head2 set_primary_key
 
 =over 4
@@ -339,11 +374,11 @@ the SQL command immediately before C<JOIN>.
 
 An arrayref containing a list of accessors in the foreign class to proxy in
 the main class. If, for example, you do the following:
-  
+
   CD->might_have(liner_notes => 'LinerNotes', undef, {
     proxy => [ qw/notes/ ],
   });
-  
+
 Then, assuming LinerNotes has an accessor named notes, you can do:
 
   my $cd = CD->find(1);
@@ -450,6 +485,113 @@ sub has_relationship {
   return exists $self->_relationships->{$rel};
 }
 
+=head2 reverse_relationship_info
+
+=over 4
+
+=item Arguments: $relname
+
+=back
+
+Returns an array of hash references of relationship information for
+the other side of the specified relationship name.
+
+=cut
+
+sub reverse_relationship_info {
+  my ($self, $rel) = @_;
+  my $rel_info = $self->relationship_info($rel);
+  my $ret = {};
+
+  return $ret unless ((ref $rel_info->{cond}) eq 'HASH');
+
+  my @cond = keys(%{$rel_info->{cond}});
+  my @refkeys = map {/^\w+\.(\w+)$/} @cond;
+  my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
+
+  # Get the related result source for this relationship
+  my $othertable = $self->related_source($rel);
+
+  # Get all the relationships for that source that related to this source
+  # whose foreign column set are our self columns on $rel and whose self
+  # columns are our foreign columns on $rel.
+  my @otherrels = $othertable->relationships();
+  my $otherrelationship;
+  foreach my $otherrel (@otherrels) {
+    my $otherrel_info = $othertable->relationship_info($otherrel);
+
+    my $back = $othertable->related_source($otherrel);
+    next unless $back->name eq $self->name;
+
+    my @othertestconds;
+
+    if (ref $otherrel_info->{cond} eq 'HASH') {
+      @othertestconds = ($otherrel_info->{cond});
+    }
+    elsif (ref $otherrel_info->{cond} eq 'ARRAY') {
+      @othertestconds = @{$otherrel_info->{cond}};
+    }
+    else {
+      next;
+    }
+
+    foreach my $othercond (@othertestconds) {
+      my @other_cond = keys(%$othercond);
+      my @other_refkeys = map {/^\w+\.(\w+)$/} @other_cond;
+      my @other_keys = map {$othercond->{$_} =~ /^\w+\.(\w+)$/} @other_cond;
+      next if (!$self->compare_relationship_keys(\@refkeys, \@other_keys) ||
+               !$self->compare_relationship_keys(\@other_refkeys, \@keys));
+      $ret->{$otherrel} =  $otherrel_info;
+    }
+  }
+  return $ret;
+}
+
+=head2 compare_relationship_keys
+
+=over 4
+
+=item Arguments: $keys1, $keys2
+
+=back
+
+Returns true if both sets of keynames are the same, false otherwise.
+
+=cut
+
+sub compare_relationship_keys {
+  my ($self, $keys1, $keys2) = @_;
+
+  # Make sure every keys1 is in keys2
+  my $found;
+  foreach my $key (@$keys1) {
+    $found = 0;
+    foreach my $prim (@$keys2) {
+      if ($prim eq $key) {
+        $found = 1;
+        last;
+      }
+    }
+    last unless $found;
+  }
+
+  # Make sure every key2 is in key1
+  if ($found) {
+    foreach my $prim (@$keys2) {
+      $found = 0;
+      foreach my $key (@$keys1) {
+        if ($prim eq $key) {
+          $found = 1;
+          last;
+        }
+      }
+      last unless $found;
+    }
+  }
+
+  return $found;
+}
+
 =head2 resolve_join
 
 =over 4
@@ -711,6 +853,26 @@ sub resultset {
   );
 }
 
+=head2 source_name
+
+=over 4
+
+=item Arguments: $source_name
+
+=back
+
+Set the name of the result source when it is loaded into a schema.
+This is usefull if you want to refer to a result source by a name other than
+its class name.
+
+  package ArchivedBooks;
+  use base qw/DBIx::Class/;
+  __PACKAGE__->table('books_archive');
+  __PACKAGE__->source_name('Books');
+
+  # from your schema...
+  $schema->resultset('Books')->find(1);
+
 =head2 throw_exception
 
 See L<DBIx::Class::Schema/"throw_exception">.
index 3ae7ad6..2743bf7 100644 (file)
@@ -8,6 +8,7 @@ use base qw/DBIx::Class/;
 
 sub iterator_class  { shift->result_source_instance->resultset_class(@_) }
 sub resultset_class { shift->result_source_instance->resultset_class(@_) }
+sub source_name { shift->result_source_instance->source_name(@_) }
 
 sub resultset_attributes {
   shift->result_source_instance->resultset_attributes(@_);
@@ -22,6 +23,8 @@ sub add_columns {
   }
 }
 
+*add_column = \&add_columns;
+
 sub has_column {
   my ($self, $column) = @_;
   return $self->result_source_instance->has_column($column);
@@ -32,11 +35,17 @@ sub column_info {
   return $self->result_source_instance->column_info($column);
 }
 
-                                                                                
+
 sub columns {
   return shift->result_source_instance->columns(@_);
 }
-                                                                                
+
+sub remove_columns {
+  return shift->result_source_instance->remove_columns(@_);
+}
+
+*remove_column = \&remove_columns;
+
 sub set_primary_key {
   shift->result_source_instance->set_primary_key(@_);
 }
index 16745b5..d9a29c8 100644 (file)
@@ -359,7 +359,8 @@ sub update_or_insert {
 
 =head2 is_changed
 
-  my @changed_col_names = $obj->is_changed
+  my @changed_col_names = $obj->is_changed();
+  if ($obj->is_changed()) { ... }
 
 =cut
 
@@ -367,6 +368,17 @@ sub is_changed {
   return keys %{shift->{_dirty_columns} || {}};
 }
 
+=head2 is_column_changed
+
+  if ($obj->is_column_changed('col')) { ... }
+
+=cut
+
+sub is_column_changed {
+  my( $self, $col ) = @_;
+  return exists $self->{_dirty_columns}->{$col};
+}
+
 =head2 result_source
 
   Accessor to the ResultSource this object was created from
index 0b9b969..3c96aef 100644 (file)
@@ -20,7 +20,7 @@ DBIx::Class::Schema - composable schemas
 
   package Library::Schema;
   use base qw/DBIx::Class::Schema/;
-  
+
   # load Library::Schema::CD, Library::Schema::Book, Library::Schema::DVD
   __PACKAGE__->load_classes(qw/CD Book DVD/);
 
@@ -36,7 +36,7 @@ DBIx::Class::Schema - composable schemas
     $password,
     { AutoCommit => 0 },
   );
-  
+
   my $schema2 = Library::Schema->connect($coderef_returning_dbh);
 
   # fetch objects using Library::Schema::DVD
@@ -219,15 +219,15 @@ Example:
 
 sub load_classes {
   my ($class, @params) = @_;
-  
+
   my %comps_for;
-  
+
   if (@params) {
     foreach my $param (@params) {
       if (ref $param eq 'ARRAY') {
         # filter out commented entries
         my @modules = grep { $_ !~ /^#/ } @$param;
-        
+
         push (@{$comps_for{$class}}, @modules);
       }
       elsif (ref $param eq 'HASH') {
@@ -267,7 +267,10 @@ sub load_classes {
           die $@ unless $@ =~ /Can't locate.+$comp_class\.pm\sin\s\@INC/;
           warn $@ if $@;
         }
-        push(@to_register, [ $comp, $comp_class ]);
+
+        $comp_class->source_name($comp) unless $comp_class->source_name;
+
+        push(@to_register, [ $comp_class->source_name, $comp_class ]);
       }
     }
   }
index 8bcb424..6e27725 100644 (file)
@@ -240,7 +240,7 @@ use base qw/DBIx::Class/;
 __PACKAGE__->load_components(qw/AccessorGroup/);
 
 __PACKAGE__->mk_group_accessors('simple' =>
-  qw/connect_info _dbh _sql_maker _conn_pid _conn_tid debug debugfh
+  qw/_connect_info _dbh _sql_maker _conn_pid _conn_tid debug debugfh
      cursor on_connect_do transaction_depth/);
 
 sub new {
@@ -277,6 +277,25 @@ This class represents the connection to the database
 
 =cut
 
+=head2 connect_info
+
+Connection information arrayref.  Can either be the same arguments
+one would pass to DBI->connect, or a code-reference which returns
+a connected database handle.  In either case, there is an optional
+final element in the arrayref, which can hold a hashref of
+connection-specific Storage::DBI options.  These include
+C<on_connect_do>, and the sql_maker options C<limit_dialect>,
+C<quote_char>, and C<name_sep>.  Examples:
+
+  ->connect_info([ 'dbi:SQLite:./foo.db' ]);
+  ->connect_info(sub { DBI->connect(...) });
+  ->connect_info([ 'dbi:Pg:dbname=foo',
+                   'postgres',
+                   '',
+                   { AutoCommit => 0 },
+                   { quote_char => q{`}, name_sep => q{@} },
+                 ]);
+
 =head2 on_connect_do
 
 Executes the sql statements given as a listref on every db connect.
@@ -360,9 +379,40 @@ sub sql_maker {
   return $self->_sql_maker;
 }
 
+sub connect_info {
+    my ($self, $info_arg) = @_;
+
+    if($info_arg) {
+        my $info = [ @$info_arg ]; # copy because we can alter it
+        my $last_info = $info->[-1];
+        if(ref $last_info eq 'HASH') {
+            my $used;
+            if(my $on_connect_do = $last_info->{on_connect_do}) {
+               $used = 1;
+               $self->on_connect_do($on_connect_do);
+            }
+            for my $sql_maker_opt (qw/limit_dialect quote_char name_sep/) {
+                if(my $opt_val = $last_info->{$sql_maker_opt}) {
+                    $used = 1;
+                    $self->sql_maker->$sql_maker_opt($opt_val);
+                }
+            }
+
+            # remove our options hashref if it was there, to avoid confusing
+            #   DBI in the case the user didn't use all 4 DBI options, as in:
+            #   [ 'dbi:SQLite:foo.db', { quote_char => q{`} } ]
+            pop(@$info) if $used;
+        }
+
+        $self->_connect_info($info);
+    }
+
+    $self->_connect_info;
+}
+
 sub _populate_dbh {
   my ($self) = @_;
-  my @info = @{$self->connect_info || []};
+  my @info = @{$self->_connect_info || []};
   $self->_dbh($self->_connect(@info));
   my $driver = $self->_dbh->{Driver}->{Name};
   eval "require DBIx::Class::Storage::DBI::${driver}";
index 73c0e80..e2a3832 100644 (file)
@@ -73,16 +73,29 @@ sub parse {
         }
         $table->primary_key($source->primary_columns);
 
+        my @primary = $source->primary_columns;
+        my %unique_constraints = $source->unique_constraints;
+        foreach my $uniq (keys %unique_constraints) {
+            if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
+                $table->add_constraint(
+                            type             => 'unique',
+                            name             => "$uniq",
+                            fields           => $unique_constraints{$uniq}
+                );
+            }
+        }
+
         my @rels = $source->relationships();
         foreach my $rel (@rels)
         {
             my $rel_info = $source->relationship_info($rel);
 
-            my $rel_table = $source->related_source($rel)->name;
-
             # Ignore any rel cond that isn't a straight hash
             next unless ref $rel_info->{cond} eq 'HASH';
 
+            my $othertable = $source->related_source($rel);
+            my $rel_table = $othertable->name;
+
             # Get the key information, mapping off the foreign/self markers
             my @cond = keys(%{$rel_info->{cond}});
             my @refkeys = map {/^\w+\.(\w+)$/} @cond;
@@ -91,47 +104,31 @@ sub parse {
             if($rel_table)
             {
 
-                #Decide if this is a foreign key based on whether the self
-                #items are our primary columns.
+                my $reverse_rels = $source->reverse_relationship_info($rel);
+                my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
 
-                # Make sure every self key is in the primary key list
-                my $found;
-                foreach my $key (@keys) {
-                    $found = 0;
-                    foreach my $prim ($source->primary_columns) {
-                        if ($prim eq $key) {
-                            $found = 1;
-                            last;
-                        }
-                    }
-                    last unless $found;
-                }
+                my $on_delete = '';
+                my $on_update = '';
 
-                # Make sure every primary key column is in the self keys
-                if ($found) {
-                    foreach my $prim ($source->primary_columns) {
-                        $found = 0;
-                        foreach my $key (@keys) {
-                            if ($prim eq $key) {
-                                $found = 1;
-                                last;
-                            }
-                        }
-                        last unless $found;
-                    }
+                if (defined $otherrelationship) {
+                    $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
+                    $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
                 }
 
-                # if $found then the two sets are equal.
+                #Decide if this is a foreign key based on whether the self
+                #items are our primary columns.
 
                 # If the sets are different, then we assume it's a foreign key from
                 # us to another table.
-                if (!$found) {
+                if (!$source->compare_relationship_keys(\@keys, \@primary)) {
                     $table->add_constraint(
                                 type             => 'foreign_key',
                                 name             => "fk_$keys[0]",
                                 fields           => \@keys,
                                 reference_fields => \@refkeys,
                                 reference_table  => $rel_table,
+                                on_delete        => $on_delete,
+                                on_update        => $on_update
                     );
                 }
             }
@@ -141,3 +138,4 @@ sub parse {
 }
 
 1;
+
diff --git a/maint/inheritance_pod.pl b/maint/inheritance_pod.pl
new file mode 100755 (executable)
index 0000000..72ba0ea
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use lib qw(lib t/lib);
+
+# USAGE:
+# maint/inheritance_pod.pl Some::Module
+
+my $module = $ARGV[0];
+eval(" require $module; ");
+
+my @modules = Class::C3::calculateMRO($module);
+shift( @modules );
+
+print "=head1 INHERITED METHODS\n\n";
+
+foreach my $module (@modules) {
+    print "=head2 $module\n\n";
+    print "=over 4\n\n";
+    my $file = $module;
+    $file =~ s/::/\//g;
+    $file .= '.pm';
+    foreach my $path (@INC){
+        if (-e "$path/$file") {
+            open(MODULE,"<$path/$file");
+            while (my $line = <MODULE>) {
+                if ($line=~/^\s*sub ([a-z][a-z_]+) \{/) {
+                    my $method = $1;
+                    print "=item *\n\n";
+                    print "L<$method|$module/$method>\n\n";
+                }
+            }
+            close(MODULE);
+            last;
+        }
+    }
+    print "=back\n\n";
+}
+
+1;
diff --git a/t/basicrels/28result_set_column.t b/t/basicrels/28result_set_column.t
new file mode 100644 (file)
index 0000000..cff21d7
--- /dev/null
@@ -0,0 +1,7 @@
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+use DBICTest::BasicRels;
+
+require "t/run/28result_set_column.tl";
+run_tests(DBICTest->schema);
index 4ea58ec..1362252 100644 (file)
@@ -8,7 +8,7 @@ plan skip_all => 'SQL::Translator required' if $@;
 
 my $schema = DBICTest::Schema;
 
-plan tests => 27;
+plan tests => 31;
 
 my $translator           =  SQL::Translator->new( 
     parser_args          => {
@@ -23,7 +23,7 @@ $translator->producer('SQLite');
 
 my $output = $translator->translate();
 
-my @constraints = 
+my @fk_constraints = 
  (
   {'display' => 'twokeys->cd',
    'selftable' => 'twokeys', 'foreigntable' => 'cd', 
@@ -32,11 +32,11 @@ my @constraints =
   {'display' => 'twokeys->artist',
    'selftable' => 'twokeys', 'foreigntable' => 'artist', 
    'selfcols'  => ['artist'], 'foreigncols' => ['artistid'],
-   'needed' => 1, on_delete => '', on_update => ''},
+   'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
   {'display' => 'cd_to_producer->cd',
    'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', 
    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
-   'needed' => 1, on_delete => '', on_update => ''},
+   'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
   {'display' => 'cd_to_producer->producer',
    'selftable' => 'cd_to_producer', 'foreigntable' => 'producer', 
    'selfcols'  => ['producer'], 'foreigncols' => ['producerid'],
@@ -44,7 +44,7 @@ my @constraints =
   {'display' => 'self_ref_alias -> self_ref for self_ref',
    'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 
    'selfcols'  => ['self_ref'], 'foreigncols' => ['id'],
-   'needed' => 1, on_delete => '', on_update => ''},
+   'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
   {'display' => 'self_ref_alias -> self_ref for alias',
    'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 
    'selfcols'  => ['alias'], 'foreigncols' => ['id'],
@@ -52,19 +52,19 @@ my @constraints =
   {'display' => 'cd -> artist',
    'selftable' => 'cd', 'foreigntable' => 'artist', 
    'selfcols'  => ['artist'], 'foreigncols' => ['artistid'],
-   'needed' => 1, on_delete => '', on_update => ''},
+   'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
   {'display' => 'artist_undirected_map -> artist for id1',
    'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
    'selfcols'  => ['id1'], 'foreigncols' => ['artistid'],
-   'needed' => 1, on_delete => '', on_update => ''},
+   'needed' => 1, on_delete => 'CASCADE', on_update => ''},
   {'display' => 'artist_undirected_map -> artist for id2',
    'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
    'selfcols'  => ['id2'], 'foreigncols' => ['artistid'],
-   'needed' => 1, on_delete => '', on_update => ''},
+   'needed' => 1, on_delete => 'CASCADE', on_update => ''},
   {'display' => 'track->cd',
    'selftable' => 'track', 'foreigntable' => 'cd', 
    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
-   'needed' => 2, on_delete => '', on_update => ''},
+   'needed' => 2, on_delete => 'CASCADE', on_update => 'CASCADE'},
   {'display' => 'treelike -> treelike for parent',
    'selftable' => 'treelike', 'foreigntable' => 'treelike', 
    'selfcols'  => ['parent'], 'foreigncols' => ['id'],
@@ -76,46 +76,109 @@ my @constraints =
   {'display' => 'tags -> cd',
    'selftable' => 'tags', 'foreigntable' => 'cd', 
    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
-   'needed' => 1, on_delete => '', on_update => ''},
+   'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
  );
 
+my @unique_constraints = (
+  {'display' => 'cd artist and title unique',
+   'table' => 'cd', 'cols' => ['artist', 'title'],
+   'needed' => 1},
+  {'display' => 'twokeytreelike name unique',
+   'table' => 'twokeytreelike', 'cols'  => ['name'],
+   'needed' => 1},
+);
+
 my $tschema = $translator->schema();
 for my $table ($tschema->get_tables) {
     my $table_name = $table->name;
     for my $c ( $table->get_constraints ) {
-        next unless $c->type eq 'FOREIGN KEY';
-
-        ok(check($table_name, scalar $c->fields, 
-              $c->reference_table, scalar $c->reference_fields, 
-              $c->on_delete, $c->on_update), "Constraint on $table_name matches an expected constraint");
+        if ($c->type eq 'FOREIGN KEY') {
+            ok(check_fk($table_name, scalar $c->fields, 
+                  $c->reference_table, scalar $c->reference_fields, 
+                  $c->on_delete, $c->on_update), "Foreign key constraint on $table_name matches an expected constraint");
+        }
+        elsif ($c->type eq 'UNIQUE') {
+            ok(check_unique($table_name, scalar $c->fields),
+                  "Unique constraint on $table_name matches an expected constraint");
+        }
     }
 }
 
+# Make sure all the foreign keys are done.
 my $i;
-for ($i = 0; $i <= $#constraints; ++$i) {
- ok(!$constraints[$i]->{'needed'}, "Constraint $constraints[$i]->{display}");
+for ($i = 0; $i <= $#fk_constraints; ++$i) {
+ ok(!$fk_constraints[$i]->{'needed'}, "Constraint $fk_constraints[$i]->{display}");
+}
+# Make sure all the uniques are done.
+for ($i = 0; $i <= $#unique_constraints; ++$i) {
+ ok(!$unique_constraints[$i]->{'needed'}, "Constraint $unique_constraints[$i]->{display}");
 }
 
-sub check {
+sub check_fk {
  my ($selftable, $selfcol, $foreigntable, $foreigncol, $ondel, $onupd) = @_;
 
  $ondel = '' if (!defined($ondel));
  $onupd = '' if (!defined($onupd));
 
  my $i;
- for ($i = 0; $i <= $#constraints; ++$i) {
-     if ($selftable eq $constraints[$i]->{'selftable'} &&
-         $foreigntable eq $constraints[$i]->{'foreigntable'} &&
-         ($ondel eq $constraints[$i]->{on_delete}) &&
-         ($onupd eq $constraints[$i]->{on_update})) {
+ for ($i = 0; $i <= $#fk_constraints; ++$i) {
+     if ($selftable eq $fk_constraints[$i]->{'selftable'} &&
+         $foreigntable eq $fk_constraints[$i]->{'foreigntable'} &&
+         ($ondel eq $fk_constraints[$i]->{on_delete}) &&
+         ($onupd eq $fk_constraints[$i]->{on_update})) {
          # check columns
 
          my $found = 0;
          for (my $j = 0; $j <= $#$selfcol; ++$j) {
              $found = 0;
-             for (my $k = 0; $k <= $#{$constraints[$i]->{'selfcols'}}; ++$k) {
-                 if ($selfcol->[$j] eq $constraints[$i]->{'selfcols'}->[$k] &&
-                     $foreigncol->[$j] eq $constraints[$i]->{'foreigncols'}->[$k]) {
+             for (my $k = 0; $k <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$k) {
+                 if ($selfcol->[$j] eq $fk_constraints[$i]->{'selfcols'}->[$k] &&
+                     $foreigncol->[$j] eq $fk_constraints[$i]->{'foreigncols'}->[$k]) {
+                     $found = 1;
+                     last;
+                 }
+             }
+             last unless $found;
+         }
+
+         if ($found) {
+             for (my $j = 0; $j <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$j) {
+                 $found = 0;
+                 for (my $k = 0; $k <= $#$selfcol; ++$k) {
+                     if ($selfcol->[$k] eq $fk_constraints[$i]->{'selfcols'}->[$j] &&
+                         $foreigncol->[$k] eq $fk_constraints[$i]->{'foreigncols'}->[$j]) {
+                         $found = 1;
+                         last;
+                     }
+                 }
+                 last unless $found;
+             }
+         }
+
+         if ($found) {
+             --$fk_constraints[$i]->{needed};
+             return 1;
+         }
+     }
+ }
+ return 0;
+}
+
+sub check_unique {
+ my ($selftable, $selfcol) = @_;
+
+ $ondel = '' if (!defined($ondel));
+ $onupd = '' if (!defined($onupd));
+
+ my $i;
+ for ($i = 0; $i <= $#unique_constraints; ++$i) {
+     if ($selftable eq $unique_constraints[$i]->{'table'}) {
+
+         my $found = 0;
+         for (my $j = 0; $j <= $#$selfcol; ++$j) {
+             $found = 0;
+             for (my $k = 0; $k <= $#{$unique_constraints[$i]->{'cols'}}; ++$k) {
+                 if ($selfcol->[$j] eq $unique_constraints[$i]->{'cols'}->[$k]) {
                      $found = 1;
                      last;
                  }
@@ -124,11 +187,10 @@ sub check {
          }
 
          if ($found) {
-             for (my $j = 0; $j <= $#{$constraints[$i]->{'selfcols'}}; ++$j) {
+             for (my $j = 0; $j <= $#{$unique_constraints[$i]->{'cols'}}; ++$j) {
                  $found = 0;
                  for (my $k = 0; $k <= $#$selfcol; ++$k) {
-                     if ($selfcol->[$k] eq $constraints[$i]->{'selfcols'}->[$j] &&
-                         $foreigncol->[$k] eq $constraints[$i]->{'foreigncols'}->[$j]) {
+                     if ($selfcol->[$k] eq $unique_constraints[$i]->{'cols'}->[$j]) {
                          $found = 1;
                          last;
                      }
@@ -138,7 +200,7 @@ sub check {
          }
 
          if ($found) {
-             --$constraints[$i]->{needed};
+             --$unique_constraints[$i]->{needed};
              return 1;
          }
      }
diff --git a/t/helperrels/27ordered.t b/t/helperrels/27ordered.t
new file mode 100644 (file)
index 0000000..352a730
--- /dev/null
@@ -0,0 +1,7 @@
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+use DBICTest::HelperRels;
+
+require "t/run/27ordered.tl";
+run_tests(DBICTest->schema);
diff --git a/t/helperrels/28result_set_column.t b/t/helperrels/28result_set_column.t
new file mode 100644 (file)
index 0000000..105b5c7
--- /dev/null
@@ -0,0 +1,7 @@
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+use DBICTest::HelperRels;
+
+require "t/run/28result_set_column.tl";
+run_tests(DBICTest->schema);
index fbf5383..e882ee7 100644 (file)
@@ -1,4 +1,4 @@
-package # hide from PAUSE 
+package # hide from PAUSE
     DBICTest::Schema;
 
 use base qw/DBIx::Class::Schema/;
@@ -7,6 +7,7 @@ no warnings qw/qw/;
 
 __PACKAGE__->load_classes(qw/
   Artist
+  Employee
   CD
   #dummy
   Track
@@ -24,6 +25,7 @@ __PACKAGE__->load_classes(qw/
     '#dummy',
     'SelfRef',
     'ArtistUndirectedMap',
+    'ArtistSourceName',
     'Producer',
     'CD_to_Producer',
   ),
diff --git a/t/lib/DBICTest/Schema/ArtistSourceName.pm b/t/lib/DBICTest/Schema/ArtistSourceName.pm
new file mode 100644 (file)
index 0000000..c4c8a8b
--- /dev/null
@@ -0,0 +1,8 @@
+package # hide from PAUSE
+    DBICTest::Schema::ArtistSourceName;
+
+use base 'DBICTest::Schema::Artist';
+
+__PACKAGE__->source_name('SourceNameArtists');
+
+1;
diff --git a/t/lib/DBICTest/Schema/Employee.pm b/t/lib/DBICTest/Schema/Employee.pm
new file mode 100644 (file)
index 0000000..63b6209
--- /dev/null
@@ -0,0 +1,39 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Employee;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw( Ordered PK::Auto Core ));
+
+__PACKAGE__->table('employees');
+
+__PACKAGE__->add_columns(
+    employee_id => {
+        data_type => 'integer',
+        is_auto_increment => 1
+    },
+    position => {
+        data_type => 'integer',
+    },
+    group_id => {
+        data_type => 'integer',
+        is_nullable => 1,
+    },
+    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',
+    group_id    => 'collection column',
+    name        => 'employee name',
+});
+
+1;
index 9547baf..c7258e0 100644 (file)
@@ -16,6 +16,7 @@ __PACKAGE__->add_columns(
  },
 );
 __PACKAGE__->set_primary_key(qw/id1 id2/);
+__PACKAGE__->add_unique_constraint('tktlnameunique' => ['name']);
 __PACKAGE__->belongs_to('parent', 'TwoKeyTreeLike',
                           { 'foreign.id1' => 'self.parent1', 'foreign.id2' => 'self.parent2'});
 
index f59aca8..ae029c5 100644 (file)
@@ -1,10 +1,20 @@
 -- 
 -- Created by SQL::Translator::Producer::SQLite
--- Created on Sun Mar 19 19:16:50 2006
+-- Created on Fri Mar 24 15:47:00 2006
 -- 
 BEGIN TRANSACTION;
 
 --
+-- Table: employees
+--
+CREATE TABLE employees (
+  employee_id INTEGER PRIMARY KEY NOT NULL,
+  position integer NOT NULL,
+  group_id integer,
+  name varchar(100)
+);
+
+--
 -- Table: serialized
 --
 CREATE TABLE serialized (
@@ -13,12 +23,12 @@ 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)
 );
 
 --
@@ -30,15 +40,6 @@ CREATE TABLE liner_notes (
 );
 
 --
--- Table: cd_to_producer
---
-CREATE TABLE cd_to_producer (
-  cd integer NOT NULL,
-  producer integer NOT NULL,
-  PRIMARY KEY (cd, producer)
-);
-
---
 -- Table: artist
 --
 CREATE TABLE artist (
@@ -56,17 +57,6 @@ CREATE TABLE self_ref_alias (
 );
 
 --
--- 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 (
@@ -77,24 +67,6 @@ 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 (
@@ -105,10 +77,19 @@ CREATE TABLE track (
 );
 
 --
--- Table: producer
+-- Table: self_ref
 --
-CREATE TABLE producer (
-  producerid INTEGER PRIMARY KEY NOT NULL,
+CREATE TABLE self_ref (
+  id INTEGER PRIMARY KEY NOT NULL,
+  name varchar(100) NOT NULL
+);
+
+--
+-- Table: treelike
+--
+CREATE TABLE treelike (
+  id INTEGER PRIMARY KEY NOT NULL,
+  parent integer NOT NULL,
   name varchar(100) NOT NULL
 );
 
@@ -122,20 +103,49 @@ CREATE TABLE tags (
 );
 
 --
--- Table: self_ref
+-- Table: twokeys
 --
-CREATE TABLE self_ref (
-  id INTEGER PRIMARY KEY NOT NULL,
+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: producer
+--
+CREATE TABLE producer (
+  producerid INTEGER PRIMARY KEY NOT NULL,
   name varchar(100) NOT NULL
 );
 
 --
--- Table: treelike
+-- Table: onekey
 --
-CREATE TABLE treelike (
+CREATE TABLE onekey (
   id INTEGER PRIMARY KEY NOT NULL,
-  parent integer NOT NULL,
-  name varchar(100) NOT NULL
+  artist integer NOT NULL,
+  cd integer NOT NULL
 );
 
 COMMIT;
index c0505c2..1901380 100644 (file)
@@ -1,7 +1,7 @@
 sub run_tests {
 my $schema = shift;
 
-plan tests => 46;
+plan tests => 51;
 
 # figure out if we've got a version of sqlite that is older than 3.2.6, in
 # which case COUNT(DISTINCT()) doesn't work
@@ -200,18 +200,42 @@ ok($schema->storage(), 'Storage available');
   is($art->name, 'Test _cond_for_update_delete', 'updated second artist name');
 }
 
-#test cascade_delete thru many_many relations
-my $art_del = $schema->resultset("Artist")->find({ artistid => 1 });
-$art_del->delete;
-cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.');
-cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.');
+# test source_name
+{
+  # source_name should be set for normal modules
+  is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker');
+
+  # test the result source that sets source_name explictly
+  ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists');
+
+  my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' });
+  cmp_ok(@artsn, '==', 4, "Four artists returned");
+}
+
+# test cascade_delete through many_to_many relations
+{
+  my $art_del = $schema->resultset("Artist")->find({ artistid => 1 });
+  $art_del->delete;
+  cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.');
+  cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.');
+}
+
+# test column_info
+{
+  $schema->source("Artist")->{_columns}{'artistid'} = {};
 
-$schema->source("Artist")->{_columns}{'artistid'} = {};
+  my $typeinfo = $schema->source("Artist")->column_info('artistid');
+  is($typeinfo->{data_type}, 'INTEGER', 'column_info ok');
+  $schema->source("Artist")->column_info('artistid');
+  ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set');
+}
 
-my $typeinfo = $schema->source("Artist")->column_info('artistid');
-is($typeinfo->{data_type}, 'INTEGER', 'column_info ok');
-$schema->source("Artist")->column_info('artistid');
-ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set');
+# test remove_columns
+{
+  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]);
+  $schema->source('CD')->remove_columns('year');
+  is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]);
+}
 
 }
 
index daea4fe..4865d96 100644 (file)
@@ -44,7 +44,7 @@ my $test_type_info = {
     'name' => {
         'data_type' => 'varchar',
         'is_nullable' => 0,
-    }
+    },
 };
 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
 
index aa721b1..31e3461 100644 (file)
@@ -14,10 +14,7 @@ DBICTest::Schema->compose_connection('DB2Test' => $dsn, $user, $pass);
 
 my $dbh = DB2Test->schema->storage->dbh;
 
-{
-    local $SIG{__WARN__} = sub {};
-    $dbh->do("DROP TABLE artist;");
-}
+$dbh->do("DROP TABLE artist", { RaiseError => 0, PrintError => 0 });
 
 $dbh->do("CREATE TABLE artist (artistid INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR(255), charfield CHAR(10));");
 
diff --git a/t/run/27ordered.tl b/t/run/27ordered.tl
new file mode 100644 (file)
index 0000000..5ced6bf
--- /dev/null
@@ -0,0 +1,102 @@
+# vim: filetype=perl
+
+sub run_tests {
+
+    plan tests => 321;
+    my $schema = shift;
+
+    my $employees = $schema->resultset('Employee');
+    $employees->delete();
+
+    foreach (1..5) {
+        $employees->create({ name=>'temp' });
+    }
+    $employees = $employees->search(undef,{order_by=>'position'});
+    ok( check_rs($employees), "intial positions" );
+
+    hammer_rs( $employees );
+
+    DBICTest::Employee->grouping_column('group_id');
+    $employees->delete();
+    foreach my $group_id (1..3) {
+        foreach (1..6) {
+            $employees->create({ name=>'temp', group_id=>$group_id });
+        }
+    }
+    $employees = $employees->search(undef,{order_by=>'group_id,position'});
+
+    foreach my $group_id (1..3) {
+        my $group_employees = $employees->search({group_id=>$group_id});
+        $group_employees->all();
+        ok( check_rs($group_employees), "group intial positions" );
+        hammer_rs( $group_employees );
+    }
+
+}
+
+sub hammer_rs {
+    my $rs = shift;
+    my $employee;
+    my $count = $rs->count();
+    my $position_column = $rs->result_class->position_column();
+
+    foreach my $position (1..$count) {
+
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_previous();
+        ok( check_rs($rs), "move_previous( $position )" );
+
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_next();
+        ok( check_rs($rs), "move_next( $position )" );
+
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_first();
+        ok( check_rs($rs), "move_first( $position )" );
+
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_last();
+        ok( check_rs($rs), "move_last( $position )" );
+
+        foreach my $to_position (1..$count) {
+            $row = $rs->find({ $position_column=>$position });
+            $row->move_to($to_position);
+            ok( check_rs($rs), "move_to( $position => $to_position )" );
+        }
+
+        $row = $rs->find({ position=>$position });
+        if ($position==1) {
+            ok( !$row->previous_sibling(), 'no previous sibling' );
+            ok( !$row->first_sibling(), 'no first sibling' );
+        }
+        else {
+            ok( $row->previous_sibling(), 'previous sibling' );
+            ok( $row->first_sibling(), 'first sibling' );
+        }
+        if ($position==$count) {
+            ok( !$row->next_sibling(), 'no next sibling' );
+            ok( !$row->last_sibling(), 'no last sibling' );
+        }
+        else {
+            ok( $row->next_sibling(), 'next sibling' );
+            ok( $row->last_sibling(), 'last sibling' );
+        }
+
+    }
+}
+
+sub check_rs {
+    my( $rs ) = @_;
+    $rs->reset();
+    my $position_column = $rs->result_class->position_column();
+    my $expected_position = 0;
+    while (my $row = $rs->next()) {
+        $expected_position ++;
+        if ($row->get_column($position_column)!=$expected_position) {
+            return 0;
+        }
+    }
+    return 1;
+}
+
+1;
diff --git a/t/run/28result_set_column.tl b/t/run/28result_set_column.tl
new file mode 100644 (file)
index 0000000..e62cb62
--- /dev/null
@@ -0,0 +1,23 @@
+sub run_tests {
+my $schema = shift;
+
+plan tests => 5; 
+
+my $rs = $cd = $schema->resultset("CD")->search({});
+
+my $rs_title = $rs->get_column('title');
+my $rs_year = $rs->get_column('year');
+
+is($rs_title->next, 'Spoonful of bees', "next okay");
+
+my @all = $rs_title->all;
+cmp_ok(scalar @all, '==', 5, "five titles returned");
+
+cmp_ok($rs_year->max, '==', 2001, "max okay for year");
+is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
+
+cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
+
+}
+
+1;