| 1 | package DBIx::Class::CustomPrefetch; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use Sub::Name; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use base qw(DBIx::Class); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use Module::Load; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use Sub::Name; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use DBIx::Class::ResultSet::CustomPrefetch; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | my $rs_class = 'DBIx::Class::ResultSet::CustomPrefetch'; | |||||||||||
| 11 | |||||||||||||
| 12 | =head1 NAME | ||||||||||||
| 13 | |||||||||||||
| 14 | DBIx::Class::CustomPrefetch - Custom prefetches for DBIx::Class | ||||||||||||
| 15 | |||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||
| 17 | |||||||||||||
| 18 | DBIx::Class onle allows joins for prefetches. But sometimes you can't use JOIN for prefetch. E.g. for prefetching | ||||||||||||
| 19 | many related objects to resultset with paging. | ||||||||||||
| 20 | |||||||||||||
| 21 | Also you can use this module to create cross-database prefetches. | ||||||||||||
| 22 | |||||||||||||
| 23 | This module provides other logic for prefetching data to resultsets. | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 VERSION | ||||||||||||
| 26 | |||||||||||||
| 27 | Version 0.09 | ||||||||||||
| 28 | |||||||||||||
| 29 | =cut | ||||||||||||
| 30 | |||||||||||||
| 31 | 3 | our $VERSION = '0.09'; | |||||||||||
| 32 | |||||||||||||
| 33 | =head1 SYNOPSIS | ||||||||||||
| 34 | |||||||||||||
| 35 | package MyApp::Schema::Foo; | ||||||||||||
| 36 | |||||||||||||
| 37 | __PACKAGE__->load_components( qw(Core CustomPrefetch) ); | ||||||||||||
| 38 | __PACKAGE__->add_column( qw(id artist_id) ); | ||||||||||||
| 39 | __PACKAGE__->resultset_class( 'DBIx::Class::ResultSet::CustomPrefetch' ); | ||||||||||||
| 40 | __PACKAGE__->custom_relation( artist => sub { MyOtherResultSetClass->new } => { | ||||||||||||
| 41 | 'foreign.id' => 'self.artist_id' | ||||||||||||
| 42 | }); | ||||||||||||
| 43 | |||||||||||||
| 44 | And your code: | ||||||||||||
| 45 | |||||||||||||
| 46 | my $resultset = $schema->resultset('Foo')->search( undef, { rows => 10 } ); | ||||||||||||
| 47 | foreach ($resultset->all) { | ||||||||||||
| 48 | say $_->artist->name; | ||||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | will make only two SQL requests: | ||||||||||||
| 52 | |||||||||||||
| 53 | SELECT id, artist_id FROM foo LIMIT 10; | ||||||||||||
| 54 | SELECT * FROM artists WHERE id IN (1, 2, 3, 5, 8, 13, 21, 34, 55, 89); | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 METHODS | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 custom_relation | ||||||||||||
| 59 | |||||||||||||
| 60 | Makes IN relation. In can be has_one, might_have or many_to_many | ||||||||||||
| 61 | |||||||||||||
| 62 | Args: $relation_name, $resultset_callback, $condition | ||||||||||||
| 63 | |||||||||||||
| 64 | =cut | ||||||||||||
| 65 | |||||||||||||
| 66 | 2 | ▶ | sub custom_relation { | ||||||||||
|
Subroutine
custom_relation: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||
| 67 | 2 | ▶ | die 'Usage: __PACKAGE__->custom_relation( name, resultset, columns );' if scalar(@_) != 4; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 2 | my ( $class, $name, @relation_args ) = @_; | |||||||||||
| 69 | 2 | ▶ | $class->result_source_instance->{_custom_relations} ||= {}; | ||||||||||
|
Condition100%2/2
$class->result_source_instance->{"_custom_relations"} ||= {}
MC/DC100%1/1
$class->result_source_instance->{"_custom_relations"} ||= {}
$class->result_source_instance->{"_custom_relations"}
Truth table100%2/2
A
A= $class->result_source_instance->{"_custom_relations"}
| |||||||||||||
| 70 | 2 | $class->result_source_instance->{_custom_relations}->{$name} = | |||||||||||
| 71 | [ $name, @relation_args ]; | ||||||||||||
| 72 | 2 | my $resultset = $relation_args[0]; | |||||||||||
| 73 | 2 | my ( $foreign_column, $self_column ) = %{ $relation_args[1] }; | |||||||||||
| 74 | 2 | $foreign_column =~ s/foreign.//; | |||||||||||
| 75 | 2 | $self_column =~ s/self.//; | |||||||||||
| 76 | 3 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 77 | 2 | ▶ | *{"${class}::$name"} = subname $name => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.5
| |||||||||||||
| 78 | 50 | my ($self,$arg) = @_; | |||||||||||
| 79 | 50 | my $name = [ caller(0) ]->[3]; | |||||||||||
| 80 | 50 | my $package = __PACKAGE__; | |||||||||||
| 81 | 50 | $name =~ s/^${package}:://; | |||||||||||
| 82 | 50 | ▶ | if (@_ > 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 30 | return $self->{"__cr_$name"} = $arg; | |||||||||||
| 84 | } | ||||||||||||
| 85 | 20 | ▶ | return $self->{"__cr_$name" } if exists $self->{"__cr_$name"}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | my $rs = $resultset->($self->result_source->schema); | |||||||||||
| 87 | 0 | ▶ | return unless $rs; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | $rs->find( { $foreign_column => $self->get_column($self_column) } ); | |||||||||||
| 89 | 2 | }; | |||||||||||
| 90 | 2 | return; | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | =head1 AUTHOR | ||||||||||||
| 94 | |||||||||||||
| 95 | Andrey Kostenko, C<< <andrey at kostenko.name> >> | ||||||||||||
| 96 | |||||||||||||
| 97 | =head1 BUGS | ||||||||||||
| 98 | |||||||||||||
| 99 | Please report any bugs or feature requests to C<bug-dbix-class-customprefetch at rt.cpan.org>, or through | ||||||||||||
| 100 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-CustomPrefetch>. I will be notified, and then you'll | ||||||||||||
| 101 | automatically be notified of progress on your bug as I make changes. | ||||||||||||
| 102 | |||||||||||||
| 103 | |||||||||||||
| 104 | |||||||||||||
| 105 | |||||||||||||
| 106 | =head1 SUPPORT | ||||||||||||
| 107 | |||||||||||||
| 108 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 109 | |||||||||||||
| 110 | perldoc DBIx::Class::CustomPrefetch | ||||||||||||
| 111 | |||||||||||||
| 112 | |||||||||||||
| 113 | You can also look for information at: | ||||||||||||
| 114 | |||||||||||||
| 115 | =over 4 | ||||||||||||
| 116 | |||||||||||||
| 117 | =item * RT: CPAN's request tracker | ||||||||||||
| 118 | |||||||||||||
| 119 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-CustomPrefetch> | ||||||||||||
| 120 | |||||||||||||
| 121 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 122 | |||||||||||||
| 123 | L<http://annocpan.org/dist/DBIx-Class-CustomPrefetch> | ||||||||||||
| 124 | |||||||||||||
| 125 | =item * CPAN Ratings | ||||||||||||
| 126 | |||||||||||||
| 127 | L<http://cpanratings.perl.org/d/DBIx-Class-CustomPrefetch> | ||||||||||||
| 128 | |||||||||||||
| 129 | =item * Search CPAN | ||||||||||||
| 130 | |||||||||||||
| 131 | L<http://search.cpan.org/dist/DBIx-Class-CustomPrefetch/> | ||||||||||||
| 132 | |||||||||||||
| 133 | =back | ||||||||||||
| 134 | |||||||||||||
| 135 | |||||||||||||
| 136 | =head1 ACKNOWLEDGEMENTS | ||||||||||||
| 137 | |||||||||||||
| 138 | |||||||||||||
| 139 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 140 | |||||||||||||
| 141 | Copyright 2009 Andrey Kostenko, all rights reserved. | ||||||||||||
| 142 | |||||||||||||
| 143 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 144 | under the same terms as Perl itself. | ||||||||||||
| 145 | |||||||||||||
| 146 | |||||||||||||
| 147 | =cut | ||||||||||||
| 148 | |||||||||||||
| 149 | 3 | 1; # End of DBIx::Class::CustomPrefetch | |||||||||||