| 1 | package Fey::Meta::HasOne::ViaSelect; | ||||||||||||||
| 2 | { | ||||||||||||||
| 3 | 9 | $Fey::Meta::HasOne::ViaSelect::VERSION = '0.46'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 10 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 10 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 10 | ▶ | use Fey::ORM::Types qw( CodeRef ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 10 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 10 | ▶ | use MooseX::StrictConstructor; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 9 | with 'Fey::Meta::Role::Relationship::HasOne'; | |||||||||||||
| 16 | |||||||||||||||
| 17 | 9 | has 'select' => ( | |||||||||||||
| 18 | is => 'ro', | ||||||||||||||
| 19 | does => 'Fey::Role::SQL::ReturnsData', | ||||||||||||||
| 20 | required => 1, | ||||||||||||||
| 21 | ); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 9 | has 'bind_params' => ( | |||||||||||||
| 24 | is => 'ro', | ||||||||||||||
| 25 | isa => CodeRef, | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | # Since we don't know the content of the SQL, we just assume it can | ||||||||||||||
| 29 | # undef | ||||||||||||||
| 30 | sub _build_allows_undef { | ||||||||||||||
| 31 | 1 | ▶ | return 1; | ||||||||||||
|
Subroutine
_build_allows_undef: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | sub _make_subref { | ||||||||||||||
| 35 | 2 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_make_subref: called CC=1 SCAR=4.9
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | 2 | my $foreign_table = $self->foreign_table(); | |||||||||||||
| 38 | 2 | my $select = $self->select(); | |||||||||||||
| 39 | 2 | my $bind = $self->bind_params(); | |||||||||||||
| 40 | |||||||||||||||
| 41 | # XXX - this is really similar to | ||||||||||||||
| 42 | # Fey::Object::Table->_get_column_values() and needs some serious | ||||||||||||||
| 43 | # cleanup. | ||||||||||||||
| 44 | return sub { | ||||||||||||||
| 45 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Subroutine
most_recent_child: not called CC=3 SCAR=24.8
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | my $dbh = $self->_dbh($select); | |||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | my $sth = $dbh->prepare( $select->sql($dbh) ); | |||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | ▶ | $sth->execute( $bind ? $self->$bind() : () ); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | my %col_values; | |||||||||||||
| 54 | 0 | $sth->bind_columns( \( @col_values{ @{ $sth->{NAME} } } ) ); | |||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | my $fetched = $sth->fetch(); | |||||||||||||
| 57 | |||||||||||||||
| 58 | 0 | $sth->finish(); | |||||||||||||
| 59 | |||||||||||||||
| 60 | 0 | ▶ | return unless $fetched; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | $self->meta()->ClassForTable($foreign_table) | |||||||||||||
| 63 | ->new( %col_values, _from_query => 1 ); | ||||||||||||||
| 64 | 2 | }; | |||||||||||||
| 65 | |||||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 9 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||
| 69 | |||||||||||||||
| 70 | 9 | 1; | |||||||||||||
| 71 | |||||||||||||||
| 72 | # ABSTRACT: A parent for has-one metaclasses based on a query object | ||||||||||||||
| 73 | |||||||||||||||
| 74 | __END__ | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =pod | ||||||||||||||
| 77 | |||||||||||||||
| 78 | =head1 NAME | ||||||||||||||
| 79 | |||||||||||||||
| 80 | Fey::Meta::HasOne::ViaSelect - A parent for has-one metaclasses based on a query object | ||||||||||||||
| 81 | |||||||||||||||
| 82 | =head1 VERSION | ||||||||||||||
| 83 | |||||||||||||||
| 84 | version 0.46 | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =head1 DESCRIPTION | ||||||||||||||
| 87 | |||||||||||||||
| 88 | This class implements a has-one relationship for a class, based on a | ||||||||||||||
| 89 | provided (or deduced) query object. | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =head1 CONSTRUCTOR OPTIONS | ||||||||||||||
| 92 | |||||||||||||||
| 93 | This class accepts the following constructor options: | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =over 4 | ||||||||||||||
| 96 | |||||||||||||||
| 97 | =item * select | ||||||||||||||
| 98 | |||||||||||||||
| 99 | An object which does the L<Fey::Role::SQL::ReturnsData> role. This query | ||||||||||||||
| 100 | defines the relationship between the tables. | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =item * bind_params | ||||||||||||||
| 103 | |||||||||||||||
| 104 | An optional subroutine reference which will be called when the SQL is | ||||||||||||||
| 105 | executed. It is called as a method on the object of this object's | ||||||||||||||
| 106 | associated class. | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =item * allows_undef | ||||||||||||||
| 109 | |||||||||||||||
| 110 | This defaults to true. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =back | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 METHODS | ||||||||||||||
| 115 | |||||||||||||||
| 116 | Besides the methods provided by L<Fey::Meta::Role::Relationship::HasOne>, this | ||||||||||||||
| 117 | class also provides the following methods: | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =head2 $ho->select() | ||||||||||||||
| 120 | |||||||||||||||
| 121 | Corresponds to the value passed to the constructor. | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =head2 $ho->bind_params() | ||||||||||||||
| 124 | |||||||||||||||
| 125 | Corresponds to the value passed to the constructor. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =head1 AUTHOR | ||||||||||||||
| 128 | |||||||||||||||
| 129 | Dave Rolsky <autarch@urth.org> | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 132 | |||||||||||||||
| 133 | This software is copyright (c) 2011 by Dave Rolsky. | ||||||||||||||
| 134 | |||||||||||||||
| 135 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 136 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =cut | ||||||||||||||