Skip to content

Commit

Permalink
many to many prefetch testing
Browse files Browse the repository at this point in the history
  • Loading branch information
vti committed Jun 28, 2009
1 parent c2a1c98 commit b177cec
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 60 deletions.
39 changes: 19 additions & 20 deletions lib/Async/ORM.pm
Expand Up @@ -759,8 +759,8 @@ sub _load_relationship {

my $relationship = $self->schema->relationships->{$name};

if ($relationship->{type} eq 'proxy') {
my $proxy_key = $relationship->{proxy_key};
if ($relationship->type eq 'proxy') {
my $proxy_key = $relationship->proxy_key;

die "proxy_key is required for $name" unless $proxy_key;

Expand Down Expand Up @@ -931,17 +931,19 @@ sub find_related {
( $relationship->map_class->schema->table . '.'
. $to => $self->column($from));

($from, $to) =
%{$relationship->map_class->schema->relationships->{$map_to}->{map}};
#($from, $to) =
#%{$relationship->map_class->schema->relationships->{$map_to}->{map}};

my $table = $relationship->class->schema->table;
my $map_table = $relationship->map_class->schema->table;
$args->{source} = [
{ name => $map_table,
join => 'left',
constraint => ["$table.$to" => "$map_table.$from"]
}
];
#my $table = $relationship->class->schema->table;
#my $map_table = $relationship->map_class->schema->table;
#$args->{source} = [
#{ name => $map_table,
#join => 'left',
#constraint => ["$table.$to" => "$map_table.$from"]
#}
#];

$args->{source} = [$relationship->to_map_source, $relationship->to_self_source];
}
else {
my ($from, $to) = %{$relationship->{map}};
Expand Down Expand Up @@ -1270,14 +1272,11 @@ sub _resolve_with {
die $class . ": unknown relationship '$name'";
}

#if ( $relationship->type eq 'many to one'
#|| $relationship->type eq 'one to one')
#{
$sql->source($relationship->to_source);
#}
#else {
#die $relationship->type . ' is not supported';
#}
if ($relationship->type eq 'many to many') {
$sql->source($relationship->to_map_source);
}

$sql->source($relationship->to_source);

if ($last) {
my @columns;
Expand Down
19 changes: 19 additions & 0 deletions lib/Async/ORM/Relationship/ManyToMany.pm
Expand Up @@ -90,6 +90,25 @@ sub to_map_source {
};
}

sub to_self_source {
my $self = shift;

my $map_from = $self->map_from;
my $map_to = $self->map_to;

my ($from, $to) =
%{$self->map_class->schema->relationships->{$map_from}->map};

my $table = $self->orig_class->schema->table;
my $map_table = $self->map_class->schema->table;

return {
name => $table,
join => 'left',
constraint => ["$table.$to" => "$map_table.$from"]
};
}

1;
__END__
Expand Down
4 changes: 2 additions & 2 deletions t/many-to-many/find-related.t
Expand Up @@ -15,7 +15,7 @@ my $dbh = TestDB->dbh;

my @articles;

Article->new(name => 'foo', tags => {name => 0})->create(
Article->new(name => 'foo', tags => {name => 'foo'})->create(
$dbh => sub {
my ($dbh, $article) = @_;

Expand All @@ -32,7 +32,7 @@ Article->new(id => $articles[0]->column('id'))->load(
my ($dbh, $tags) = @_;

is(@$tags, 1);
is($tags->[0]->column('name'), 0);
is($tags->[0]->column('name'), 'foo');
}
);
}
Expand Down
13 changes: 9 additions & 4 deletions t/many-to-many/find.t
@@ -1,7 +1,7 @@
use strict;
use warnings;

use Test::More tests => 2;
use Test::More tests => 5;

use lib 't/lib';

Expand All @@ -14,7 +14,7 @@ my $dbh = TestDB->dbh;

my @articles;

Article->new(name => 'foo', tags => {name => 'bar'})->create(
Article->new(name => 'foo', tags => [{name => 'bar'},{name => 'baz'}])->create(
$dbh => sub {
my ($dbh, $article) = @_;

Expand All @@ -31,12 +31,17 @@ Article->find(
);

Article->find(
$dbh => {where => ['tags.name' => 'bar']} => sub {
$dbh => {with => 'tags'} => sub {
my ($dbh, $articles) = @_;

is(@$articles, 1);

my $tags = $articles->[0]->related('tags');
is(@$tags, 2);
is($tags->[0]->column('name'), 'bar');
is($tags->[1]->column('name'), 'baz');
}
);

$articles[0]->delete($dbh => sub { });
Tag->delete($dbh => {where => [name => [qw/foo/]]} => sub { });
Tag->delete($dbh => {where => [name => [qw/bar baz/]]} => sub { });
6 changes: 3 additions & 3 deletions t/many-to-many/load-related.t
Expand Up @@ -15,7 +15,7 @@ my $dbh = TestDB->dbh;

my @articles;

Article->new(name => 'foo', tags => {name => 0})->create(
Article->new(name => 'foo', tags => {name => 'foo'})->create(
$dbh => sub {
my ($dbh, $article) = @_;

Expand All @@ -32,12 +32,12 @@ Article->new(id => $articles[0]->column('id'))->load(
my ($dbh, $tags) = @_;

is(@$tags, 1);
is($tags->[0]->column('name'), 0);
is($tags->[0]->column('name'), 'foo');

$tags = $article->related('tags');

is(@$tags, 1);
is($tags->[0]->column('name'), 0);
is($tags->[0]->column('name'), 'foo');
}
);
}
Expand Down
37 changes: 37 additions & 0 deletions t/many-to-many/load.t
@@ -0,0 +1,37 @@
use strict;
use warnings;

use Test::More tests => 2;

use lib 't/lib';

use TestDB;

use Article;
use Tag;

my $dbh = TestDB->dbh;

my @articles;

Article->new(name => 'foo', tags => {name => 'foo'})->create(
$dbh => sub {
my ($dbh, $article) = @_;

push @articles, $article;
}
);

Article->new(id => $articles[0]->column('id'))->load(
$dbh => {with => 'tags'} => sub {
my ($dbh, $article) = @_;

my $tags = $article->related('tags');

is(@$tags, 1);
is($tags->[0]->column('name'), 'foo');
}
);

$articles[0]->delete($dbh => sub { });
Tag->delete($dbh => {where => [name => [qw/foo/]]} => sub { });
60 changes: 30 additions & 30 deletions t/many-to-many/set-related.t
Expand Up @@ -7,58 +7,58 @@ use lib 't/lib';

use TestDB;

use Author;
use AuthorAdmin;
use Admin;
use Article;
use Tag;

my $dbh = TestDB->dbh;

my @authors;
my @articles;

Author->new(name => 'foo')->create(
Article->new(name => 'foo')->create(
$dbh => sub {
my ($dbh, $author) = @_;
my ($dbh, $article) = @_;

push @authors, $author;
push @articles, $article;

$author->set_related(
$dbh => 'author_admin' => {beard => 1} => sub {
my ($dbh, $author_admin) = @_;
$article->set_related(
$dbh => 'tags' => {name => 'foo'} => sub {
my ($dbh, $tags) = @_;

ok($author_admin);
is($author_admin->column('beard'), 1);
is(@$tags, 1);
is($tags->[0]->column('name'), 'foo');
}
);
}
);

Author->new(id => $authors[0]->column('id'))->load(
$dbh => {with => 'author_admin'} => sub {
my ($dbh, $author) = @_;
Article->new(id => $articles[0]->column('id'))->load(
$dbh => {with => 'tags'} => sub {
my ($dbh, $article) = @_;

my $author_admin = $author->related('author_admin');
ok($author_admin);
is($author_admin->column('beard'), 1);
my $tags = $article->related('tags');
is(@$tags, 1);
is($tags->[0]->column('name'), 'foo');
}
);

$authors[0]->set_related(
$dbh => 'author_admin' => {beard => 0} => sub {
my ($dbh, $author_admin) = @_;
$articles[0]->set_related(
$dbh => 'tags' => {name => 'bar'} => sub {
my ($dbh, $tags) = @_;

ok($author_admin);
is($author_admin->column('beard'), 0);
is(@$tags, 1);
is($tags->[0]->column('name'), 'bar');
}
);

Author->new(id => $authors[0]->column('id'))->load(
$dbh => {with => 'author_admin'} => sub {
my ($dbh, $author) = @_;
Article->new(id => $articles[0]->column('id'))->load(
$dbh => {with => 'tags'} => sub {
my ($dbh, $article) = @_;

my $author_admin = $author->related('author_admin');
ok($author_admin);
is($author_admin->column('beard'), 0);
my $tags = $article->related('tags');
is(@$tags, 1);
is($tags->[0]->column('name'), 'bar');
}
);

$authors[0]->delete($dbh => sub { });
$articles[0]->delete($dbh => sub { });
Tag->delete($dbh => {where => [name => [qw/foo bar/]]} => sub {});
12 changes: 11 additions & 1 deletion t/relationship/many-to-many.t
@@ -1,4 +1,7 @@
use Test::More tests => 4;
use strict;
use warnings;

use Test::More tests => 5;

use Async::ORM::Relationship::ManyToMany;

Expand Down Expand Up @@ -31,3 +34,10 @@ is_deeply($rel->to_source,
}
);

is_deeply($rel->to_self_source,
{
name => 'article',
join => 'left',
constraint => ['article.id' => 'article_tag_map.article_id']
}
);

0 comments on commit b177cec

Please sign in to comment.