[Bast-commits] r5397 - in DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk: lib/DBIx/Class/ResultSet t/lib t/lib/DBSchema/Result t/var

zby at dev.catalyst.perl.org zby at dev.catalyst.perl.org
Mon Feb 2 16:13:45 GMT 2009


Author: zby
Date: 2009-02-02 16:13:45 +0000 (Mon, 02 Feb 2009)
New Revision: 5397

Added:
   DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Onekey.pm
   DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Twokeys_belongsto.pm
Modified:
   DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
   DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Dvd.pm
   DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/RunTests.pm
   DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/var/dvdzbr.db
Log:
got rid of the requirement to add id => undef

Modified: DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm
===================================================================
--- DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm	2009-02-02 14:49:33 UTC (rev 5396)
+++ DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm	2009-02-02 16:13:45 UTC (rev 5397)
@@ -11,64 +11,74 @@
 
 sub recursive_update { 
     my( $self, $updates, $fixed_fields ) = @_;
+#    warn 'entering: ' . $self->result_source->from();
     if( blessed( $updates ) && $updates->isa( 'DBIx::Class::Row' ) ){
         return $updates;
     }
+    my $object;
+#    warn 'fixed_fields: ' . Dumper( $fixed_fields ); use Data::Dumper;
+    if( $fixed_fields ){
+        carp if !( ref( $fixed_fields ) eq 'HASH' );
+        $updates = { %$updates, %$fixed_fields };
+    }
     my %columns;
-    for my $name ( keys %$updates ){ 
+    for my $name ( keys %$updates ){
         if( $self->is_for_column( $name, $updates->{$name} ) ){
             $columns{$name} = $updates->{$name};
         }
     }
-    my $object;
-#    warn 'cond: ' . Dumper( $self->{cond} ); use Data::Dumper;
-#    warn 'where: ' . Dumper( $self->{attrs}{where} ); use Data::Dumper;
-    my @missing = grep { !exists $updates->{$_} && !exists $fixed_fields->{$_} } $self->result_source->primary_columns;
-    if( defined $self->{cond} && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $self->{cond} ){
-        $self->{cond} = undef;
-        $self->{attrs}{where} = undef;
-        if( ! scalar @missing ){
-            $object = $self->find( \%columns, { key => 'primary' } );
-        }
-    }
-    else{
+#    warn 'columns: ' . Dumper( \%columns ); use Data::Dumper;
+    my @missing = grep { !exists $columns{$_} } $self->result_source->primary_columns;
+    if( ! scalar @missing ){
         $object = $self->find( \%columns, { key => 'primary' } );
     }
     $object ||= $self->new( {} );
 
     # first update columns and other accessors - so that later related records can be found
     for my $name ( keys %columns ){ 
-            $object->$name( $updates->{$name} );
+        $object->$name( $updates->{$name} );
     }
     for my $name ( keys %$updates ){ 
-        if($object->can($name) && !$self->is_for_column( $name, $updates->{$name} ) ){
-
+        next if exists $columns{ $name };
+        if(
+            #$object->can($name) 
+            $object->result_source->has_relationship($name)
+            && !$self->is_for_column( $object, $name, $updates->{$name} ) 
+        ){
             # updating relations that that should be done before the row is inserted into the database
             # like belongs_to
                 my $info = $object->result_source->relationship_info( $name );
-                if( $info and not $info->{attrs}{accessor} eq 'multi'
+                if( $info #and not $info->{attrs}{accessor} eq 'multi'
                         and 
                     _master_relation_cond( $object->result_source, $info->{cond}, $self->_get_pk_for_related( $name ) )
                 ){
-                    my $related_result = $object->related_resultset( $name );
+                    my $related_result = $self->related_resultset( $name )->result_source->resultset;
                     my $resolved =  $self->result_source->resolve_condition(
                         $info->{cond}, $name, $object
                     );
 #                    warn 'resolved: ' . Dumper( $resolved ); use Data::Dumper;
-                    my $sub_object = $related_result->recursive_update( $updates->{$name} );
-                    $object->set_from_related( $name, $sub_object );
+                    $resolved = undef if $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved;
+                    if( ref $updates->{$name} eq 'ARRAY' ){
+                        for my $sub_updates ( @{$updates->{$name}} ) {
+                            my $sub_object = $related_result->recursive_update( $sub_updates, $resolved );
+                        }
+                    }
+                    else { 
+                        my $sub_object = $related_result->recursive_update( $updates->{$name}, $resolved );
+                        $object->set_from_related( $name, $sub_object );
+                    }
                 }
         }
     }
     $self->_delete_empty_auto_increment($object);
     # don't allow insert to recurse to related objects - we do the recursion ourselves
 #    $object->{_rel_in_storage} = 1;
-#    warn Dumper( $object->{_column_data} );
     $object->update_or_insert;
 
     # updating relations that can be done only after the row is inserted into the database
     # like has_many and many_to_many
     for my $name ( keys %$updates ){
+        next if exists $columns{ $name };
         my $value = $updates->{$name};
         # many to many case
         if( $self->is_m2m( $name ) ) {
@@ -88,15 +98,22 @@
         }
         elsif( $object->result_source->has_relationship($name) ){
             my $info = $object->result_source->relationship_info( $name );
+            next if ( _master_relation_cond( $object->result_source, $info->{cond}, $self->_get_pk_for_related( $name ) ) );
             # has many case (and similar)
+            my $resolved =  $self->result_source->resolve_condition(
+                        $info->{cond}, $name, $object
+            );
+#            warn 'resolved: ' . Dumper( $resolved ); use Data::Dumper;
+            $resolved = undef if $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $resolved;
+            my $related_result = $self->related_resultset( $name )->result_source->resultset;
             if( ref $updates->{$name} eq 'ARRAY' ){
                 for my $sub_updates ( @{$updates->{$name}} ) {
-                    my $sub_object = $object->search_related( $name )->recursive_update( $sub_updates );
+                    my $sub_object = $related_result->recursive_update( $sub_updates, $resolved );
                 }
             }
             # might_have and has_one case
-            elsif ( ! _master_relation_cond( $object->result_source, $info->{cond}, $self->_get_pk_for_related( $name ) ) ){
-                my $sub_object = $object->search_related( $name )->recursive_update( $value );
+            else{
+                my $sub_object = $related_result->recursive_update( $value, $resolved );
                 #$object->set_from_related( $name, $sub_object );
             }
         }
@@ -166,7 +183,6 @@
 
 sub _get_pk_for_related {
     my ( $self, $relation ) = @_;
-
     my $result_source;
     if( $self->result_source->has_relationship( $relation ) ){
         $result_source = $self->result_source->related_source( $relation );
@@ -201,7 +217,6 @@
     return;
 }
 
-
 1; # Magic true value required at end of module
 __END__
 

Modified: DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Dvd.pm
===================================================================
--- DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Dvd.pm	2009-02-02 14:49:33 UTC (rev 5396)
+++ DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Dvd.pm	2009-02-02 16:13:45 UTC (rev 5397)
@@ -40,19 +40,25 @@
     data_type => 'datetime',
     is_nullable => 1,
   },
+  'twokeysfk' => {
+    data_type => 'integer',
+    is_nullable => 1,
+  },
 );
 __PACKAGE__->set_primary_key('id');
 __PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', { id => 'owner' });
 __PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', { id => 'current_borrower' });
 __PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.id' });
-__PACKAGE__->has_many('viewings', 'Viewing', { 'foreign.dvd_id' => 'self.id' });
+__PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.id' });
 __PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag');
 __PACKAGE__->might_have(
     liner_notes => 'DBSchema::Result::LinerNotes', undef,
     { proxy => [ qw/notes/ ] },
 );
 __PACKAGE__->add_relationship('like_has_many', 'DBSchema::Result::Twokeys', { 'foreign.dvd_name' => 'self.name' }, { accessor_name => 'like_has_many' } );
+__PACKAGE__->add_relationship('like_has_many2', 'DBSchema::Result::Twokeys_belongsto', 
+    { 'foreign.key1' => 'self.twokeysfk' }, 
+); 
 
-
 1;
 

Added: DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Onekey.pm
===================================================================
--- DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Onekey.pm	                        (rev 0)
+++ DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Onekey.pm	2009-02-02 16:13:45 UTC (rev 5397)
@@ -0,0 +1,24 @@
+package DBSchema::Result::Onekey;
+
+# Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components("PK::Auto", "Core");
+__PACKAGE__->table("onekey");
+__PACKAGE__->add_columns(
+    "id" => { data_type => 'integer', is_auto_increment => 1 },
+    name => { data_type => 'varchar', size => 100, is_nullable => 1 },
+);
+__PACKAGE__->set_primary_key("id");
+
+__PACKAGE__->might_have(
+        twokeys_belongsto => 'DBSchema::Result::Twokeys_belongsto', 'key1',
+);
+
+
+1;
+

Added: DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Twokeys_belongsto.pm
===================================================================
--- DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Twokeys_belongsto.pm	                        (rev 0)
+++ DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/DBSchema/Result/Twokeys_belongsto.pm	2009-02-02 16:13:45 UTC (rev 5397)
@@ -0,0 +1,24 @@
+package DBSchema::Result::Twokeys_belongsto;
+
+# Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components("PK::Auto", "Core");
+__PACKAGE__->table("twokeys_belongsto");
+__PACKAGE__->add_columns(
+    "key1" => { data_type => 'integer' },
+    "key2" => { data_type => 'integer' },
+);
+__PACKAGE__->set_primary_key("key1", "key2");
+
+__PACKAGE__->add_relationship('like_belongs_to', 'DBSchema::Result::Dvd', { 'foreign.twokeysfk' => 'self.key1' }, );
+
+__PACKAGE__->belongs_to('onekey', 'DBSchema::Result::Onekey', { 'foreign.id' => 'self.key1' }, );
+
+
+1;
+

Modified: DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/RunTests.pm
===================================================================
--- DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/RunTests.pm	2009-02-02 14:49:33 UTC (rev 5396)
+++ DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/lib/RunTests.pm	2009-02-02 16:13:45 UTC (rev 5397)
@@ -9,7 +9,7 @@
 sub run_tests{
     my $schema = shift;
 
-    plan tests => 25;
+    plan tests => 27;
     
     my $dvd_rs = $schema->resultset( 'Dvd' );
     my $user_rs = $schema->resultset( 'User' );
@@ -23,7 +23,6 @@
     
     my $updates;
     $updates = {
-        id => undef,
             name => 'Test name 2',
             viewings => [ { user_id => $owner->id } ],
             owner => { id => $another_owner->id },
@@ -40,7 +39,6 @@
     
     # creating new records
     my $updates = {
-        id => undef,
             aaaa => undef,
             tags => [ '2', { id => '3' } ], 
             name => 'Test name',
@@ -56,6 +54,12 @@
             like_has_many => [
             { key2 => 1 }
             ],
+            like_has_many2 => [ 
+                {
+                    onekey => { name => 'aaaaa' },
+                    key2 => 1
+                }
+            ],
     };
     
     my $dvd = $dvd_rs->recursive_update( $updates );
@@ -69,7 +73,12 @@
     is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
     is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
     ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
-    # changing existing records
+    my $onekey = $schema->resultset( 'Onekey' )->search( name => 'aaaaa' )->first;
+    ok ( $onekey, 'Onekey created' );
+    ok ( $schema->resultset( 'Twokeys_belongsto' )->find( { key1 => $onekey->id, key2 => 1 } ), 'Twokeys created' );
+
+
+# changing existing records
     
     my $num_of_users = $user_rs->count;
     $updates = {
@@ -86,6 +95,7 @@
             liner_notes => {
                 notes => 'test note changed',
             },
+
     };
     $dvd = $dvd_rs->recursive_update( $updates );
     
@@ -100,18 +110,15 @@
     # repeatable
     
     $updates = {
-        id => undef,
         name  => 'temp name',
         username => 'temp username',
         password => 'temp username',
         owned_dvds =>[
         {
-            'id' => undef,
             'name' => 'temp name 1',
             'tags' => [ 1, 2 ],
         },
         {
-            'id' => undef,
             'name' => 'temp name 2',
             'tags' => [ 2, 3 ],
         }

Modified: DBIx-Class-ResultSet-RecursiveUpdate/1.000/trunk/t/var/dvdzbr.db
===================================================================
(Binary files differ)




More information about the Bast-commits mailing list