[Bast-commits] r3298 - in branches/DBIx-Class/bulk_create:
lib/DBIx/Class t
matthewt at dev.catalyst.perl.org
matthewt at dev.catalyst.perl.org
Fri May 11 04:40:55 GMT 2007
Author: matthewt
Date: 2007-05-11 04:40:44 +0100 (Fri, 11 May 2007)
New Revision: 3298
Modified:
branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSource.pm
branches/DBIx-Class/bulk_create/lib/DBIx/Class/Row.pm
branches/DBIx-Class/bulk_create/t/96file_column.t
branches/DBIx-Class/bulk_create/t/96multi_create.t
Log:
multi-create working
Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm 2007-05-11 03:18:37 UTC (rev 3297)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm 2007-05-11 03:40:44 UTC (rev 3298)
@@ -350,11 +350,13 @@
my (%related, $info);
- foreach my $key (keys %$input_query) {
+ KEY: foreach my $key (keys %$input_query) {
if (ref($input_query->{$key})
&& ($info = $self->result_source->relationship_info($key))) {
+ my $val = delete $input_query->{$key};
+ next KEY if (ref($val) eq 'ARRAY'); # has_many for multi_create
my $rel_q = $self->result_source->resolve_condition(
- $info->{cond}, delete $input_query->{$key}, $key
+ $info->{cond}, $val, $key
);
die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
@related{keys %$rel_q} = values %$rel_q;
Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSource.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSource.pm 2007-05-11 03:18:37 UTC (rev 3297)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSource.pm 2007-05-11 03:40:44 UTC (rev 3298)
@@ -771,6 +771,8 @@
#warn %ret;
} elsif (!defined $for) { # undef, i.e. "no object"
$ret{$k} = undef;
+ } elsif (ref $as eq 'HASH') { # reverse hashref
+ $ret{$v} = $as->{$k};
} elsif (ref $as) { # reverse object
$ret{$v} = $as->get_column($k);
} elsif (!defined $as) { # undef, i.e. "no reverse object"
Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/Row.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/Row.pm 2007-05-11 03:18:37 UTC (rev 3297)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/Row.pm 2007-05-11 03:40:44 UTC (rev 3298)
@@ -64,7 +64,6 @@
my ($related,$inflated);
## Pretend all the rels are actual objects, unset below if not, for insert() to fix
$new->{_rel_in_storage} = 1;
- print STDERR "Attrs: ", Dumper($attrs), "\n";
foreach my $key (keys %$attrs) {
if (ref $attrs->{$key}) {
## Can we extract this lot to use with update(_or .. ) ?
@@ -73,11 +72,9 @@
&& $info->{attrs}{accessor} eq 'single')
{
my $rel_obj = delete $attrs->{$key};
- print STDERR "REL: $key ", ref($rel_obj), "\n";
if(!Scalar::Util::blessed($rel_obj)) {
- $rel_obj = $new->new_related($key, $rel_obj);
- print STDERR "REL: $key ", ref($rel_obj), "\n";
- $new->{_rel_in_storage} = 0;
+ $rel_obj = $new->find_or_new_related($key, $rel_obj);
+ $new->{_rel_in_storage} = 0 unless ($rel_obj->in_storage);
}
$new->set_from_related($key, $rel_obj);
$related->{$key} = $rel_obj;
@@ -85,30 +82,33 @@
} elsif ($info && $info->{attrs}{accessor}
&& $info->{attrs}{accessor} eq 'multi'
&& ref $attrs->{$key} eq 'ARRAY') {
- my $others = delete $attrs->{$key};
- foreach my $rel_obj (@$others) {
- if(!Scalar::Util::blessed($rel_obj)) {
- $rel_obj = $new->new_related($key, $rel_obj);
- $new->{_rel_in_storage} = 0;
- }
+ my $others = delete $attrs->{$key};
+ foreach my $rel_obj (@$others) {
+ if(!Scalar::Util::blessed($rel_obj)) {
+ $rel_obj = $new->new_related($key, $rel_obj);
+ $new->{_rel_in_storage} = 0;
}
- $related->{$key} = $others;
- next;
- } elsif ($class->has_column($key)
- && exists $class->column_info($key)->{_inflate_info})
+ }
+ $related->{$key} = $others;
+ next;
+ } elsif ($info && $info->{attrs}{accessor}
+ && $info->{attrs}{accessor} eq 'filter')
{
## 'filter' should disappear and get merged in with 'single' above!
- my $rel_obj = $attrs->{$key};
+ my $rel_obj = delete $attrs->{$key};
if(!Scalar::Util::blessed($rel_obj)) {
$rel_obj = $new->new_related($key, $rel_obj);
$new->{_rel_in_storage} = 0;
}
$inflated->{$key} = $rel_obj;
next;
+ } elsif ($class->has_column($key)
+ && $class->column_info($key)->{_inflate_info}) {
+ $inflated->{$key} = $attrs->{$key};
+ next;
}
}
use Data::Dumper;
- print STDERR "Key: ", Dumper($key), "\n";
$new->throw_exception("No such column $key on $class")
unless $class->has_column($key);
$new->store_column($key => $attrs->{$key});
@@ -151,11 +151,10 @@
## 'filter' reltype..
## These are the FK rels, need their IDs for the insert.
foreach my $relname (keys %related_stuff) {
- my $relobj = $related_stuff{$relname};
- if(ref $relobj ne 'ARRAY') {
- $relobj->insert() if(!$relobj->in_storage);
- print STDERR "Inserting: ", ref($relobj), "\n";
- $self->set_from_related($relname, $relobj);
+ my $rel_obj = $related_stuff{$relname};
+ if(Scalar::Util::blessed($rel_obj) && $rel_obj->isa('DBIx::Class::Row')) {
+ $rel_obj->insert();
+ $self->set_from_related($relname, $rel_obj);
}
}
@@ -184,7 +183,6 @@
my $info = $self->relationship_info($relname);
## What about multi-col FKs ?
my $key = $1 if($info && (keys %{$info->{cond}})[0] =~ /^foreign\.(\w+)/);
- print STDERR "Inserting: ", ref($obj), "\n";
$obj->set_from_related($key, $self);
$obj->insert() if(!$obj->in_storage);
}
Modified: branches/DBIx-Class/bulk_create/t/96file_column.t
===================================================================
--- branches/DBIx-Class/bulk_create/t/96file_column.t 2007-05-11 03:18:37 UTC (rev 3297)
+++ branches/DBIx-Class/bulk_create/t/96file_column.t 2007-05-11 03:40:44 UTC (rev 3298)
@@ -12,4 +12,4 @@
my $fh = new IO::File('t/96file_column.t','r');
eval { $schema->resultset('FileColumn')->create({file => {handle => $fh, filename =>'96file_column.t'}})};
-ok(!$@,'FileColumn checking if file handled properly.');
+cmp_ok($@,'eq','','FileColumn checking if file handled properly.');
Modified: branches/DBIx-Class/bulk_create/t/96multi_create.t
===================================================================
--- branches/DBIx-Class/bulk_create/t/96multi_create.t 2007-05-11 03:18:37 UTC (rev 3297)
+++ branches/DBIx-Class/bulk_create/t/96multi_create.t 2007-05-11 03:40:44 UTC (rev 3298)
@@ -8,7 +8,7 @@
my $schema = DBICTest->init_schema();
-plan tests => 12;
+plan tests => 17;
my $cd2 = $schema->resultset('CD')->create({ artist =>
{ name => 'Fred Bloggs' },
@@ -90,10 +90,11 @@
CREATE_RELATED2 :{
+ my $artist = $schema->resultset('Artist')->first;
- my $cd_result = $schema->resultset('Artist')->first->create_related('cds', {
+ my $cd_result = $artist->create_related('cds', {
- title => 'TestOneCD1',
+ title => 'TestOneCD2',
year => 2007,
tracks => [
@@ -108,7 +109,7 @@
});
ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
- ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
+ ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
my $tracks = $cd_result->tracks;
@@ -119,9 +120,3 @@
ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
}
}
-
-my $now = DateTime->now;
-
-ok( $schema->resultset('Event')
- ->create( { starts_at => $now, created_on => $now } ),
- 'object with inflated non-rels ok');
More information about the Bast-commits
mailing list