| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Persistence::Relationship::OneToMany; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 17 |  |  | 17 |  | 145113 | use strict; | 
|  | 17 |  |  |  |  | 33 |  | 
|  | 17 |  |  |  |  | 589 |  | 
| 4 | 17 |  |  | 17 |  | 88 | use warnings; | 
|  | 17 |  |  |  |  | 33 |  | 
|  | 17 |  |  |  |  | 489 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 17 |  |  | 17 |  | 86 | use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION); | 
|  | 17 |  |  |  |  | 24 |  | 
|  | 17 |  |  |  |  | 1111 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 17 |  |  | 17 |  | 95 | use Abstract::Meta::Class ':all'; | 
|  | 17 |  |  |  |  | 33 |  | 
|  | 17 |  |  |  |  | 2769 |  | 
| 9 | 17 |  |  | 17 |  | 93 | use base qw (Exporter Persistence::Relationship); | 
|  | 17 |  |  |  |  | 30 |  | 
|  | 17 |  |  |  |  | 2363 |  | 
| 10 | 17 |  |  | 17 |  | 91 | use Carp 'confess'; | 
|  | 17 |  |  |  |  | 30 |  | 
|  | 17 |  |  |  |  | 8510 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | $VERSION = 0.01; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | @EXPORT_OK = qw(one_to_many); | 
| 15 |  |  |  |  |  |  | %EXPORT_TAGS = (all => \@EXPORT_OK); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =head1 NAME | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | Persistence::Relationship::OneToMany - One to many relationship. | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 CLASS HIERARCHY | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | Persistence::Relationship | 
| 24 |  |  |  |  |  |  | | | 
| 25 |  |  |  |  |  |  | +----Persistence::Relationship::OneToMany | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | #.... entities definition | 
| 30 |  |  |  |  |  |  | my $membership_entity = Persistence::Entity->new( | 
| 31 |  |  |  |  |  |  | name    => 'wsus_user_service', | 
| 32 |  |  |  |  |  |  | alias   => 'us', | 
| 33 |  |  |  |  |  |  | primary_key => ['user_id', 'service_id'], | 
| 34 |  |  |  |  |  |  | columns => [ | 
| 35 |  |  |  |  |  |  | sql_column(name => 'user_id'), | 
| 36 |  |  |  |  |  |  | sql_column(name => 'service_id'), | 
| 37 |  |  |  |  |  |  | sql_column(name => 'agreement_flag') | 
| 38 |  |  |  |  |  |  | ], | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | my $user_entity = Persistence::Entity->new( | 
| 42 |  |  |  |  |  |  | name    => 'wsus_user', | 
| 43 |  |  |  |  |  |  | alias   => 'ur', | 
| 44 |  |  |  |  |  |  | primary_key => ['id'], | 
| 45 |  |  |  |  |  |  | columns => [ | 
| 46 |  |  |  |  |  |  | sql_column(name => 'id'), | 
| 47 |  |  |  |  |  |  | sql_column(name => 'username', unique => 1), | 
| 48 |  |  |  |  |  |  | sql_column(name => 'password'), | 
| 49 |  |  |  |  |  |  | sql_column(name => 'email'), | 
| 50 |  |  |  |  |  |  | ], | 
| 51 |  |  |  |  |  |  | to_many_relationships => [sql_relationship(target_entity => $membership_entity, join_columns => ['user_id'], order_by => 'service_id, user_id')] | 
| 52 |  |  |  |  |  |  | ); | 
| 53 |  |  |  |  |  |  | $entity_manager->add_entities($membership_entity, $user_entity); | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | # object mapping | 
| 56 |  |  |  |  |  |  | package User; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | use Abstract::Meta::Class ':all'; | 
| 59 |  |  |  |  |  |  | use Persistence::Entity ':all'; | 
| 60 |  |  |  |  |  |  | use Persistence::ORM ':all'; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | entity 'wsus_user'; | 
| 63 |  |  |  |  |  |  | column id => has('$.id'); | 
| 64 |  |  |  |  |  |  | column username => has('$.name'); | 
| 65 |  |  |  |  |  |  | column password => has('$.password'); | 
| 66 |  |  |  |  |  |  | column email => has('$.email'); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | one_to_many 'wsus_user_service' => ( | 
| 69 |  |  |  |  |  |  | attribute    => has('@.membership' => (associated_class => 'Membership')), | 
| 70 |  |  |  |  |  |  | fetch_method => EAGER, | 
| 71 |  |  |  |  |  |  | cascade      => ALL, | 
| 72 |  |  |  |  |  |  | ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | Represents one to many relationship. Allows cascading operation (inert/update/delete). | 
| 77 |  |  |  |  |  |  | Supports eager, lazy fetch, cascading operation (inert/update/delete). | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =head1 EXPORT | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | one_to_many method by ':all' tag. | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =head2 METHODS | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =over | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =cut | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =item one_to_many | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | Create a new instance of one to many relation. | 
| 92 |  |  |  |  |  |  | Takes associated entity's id as parameters | 
| 93 |  |  |  |  |  |  | and list of named parameters for Persistence::Relationship::OneToMany constructor. | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | one_to_many 'wsus_user_service' => ( | 
| 96 |  |  |  |  |  |  | attribute    => has('@.membership' => (associated_class => 'Membership')), | 
| 97 |  |  |  |  |  |  | fetch_method => EAGER, | 
| 98 |  |  |  |  |  |  | cascade      => ALL, | 
| 99 |  |  |  |  |  |  | ); | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =cut | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub one_to_many { | 
| 105 | 0 |  |  | 0 | 1 |  | my $package = caller(); | 
| 106 | 0 |  |  |  |  |  | __PACKAGE__->add_relationship($package, @_); | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =item deserialise_attribute | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | Deserialises relation attribute | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =cut | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub deserialise_attribute { | 
| 117 | 0 |  |  | 0 | 1 |  | my ($self, $object, $entity_manager, $orm) = @_; | 
| 118 | 0 |  |  |  |  |  | my $entity = $entity_manager->entity($orm->entity_name); | 
| 119 | 0 |  |  |  |  |  | my $attribute = $self->attribute; | 
| 120 | 0 |  |  |  |  |  | my @rows = $entity->relationship_query( | 
| 121 |  |  |  |  |  |  | $self->name, | 
| 122 |  |  |  |  |  |  | ref($object) => $attribute->associated_class, | 
| 123 |  |  |  |  |  |  | $orm->column_values($object, $entity->primary_key) | 
| 124 |  |  |  |  |  |  | ); | 
| 125 |  |  |  |  |  |  |  | 
| 126 | 0 | 0 |  |  |  |  | if (@rows) { | 
| 127 | 0 |  |  |  |  |  | my $mutator = $attribute->mutator; | 
| 128 | 0 |  |  |  |  |  | $object->$mutator(\@rows); | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =item insert | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | Inserts relationship data. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =cut | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | sub insert { | 
| 140 | 0 |  |  | 0 | 1 |  | my ($self, $orm, $entity, $unique_values, $object) = @_; | 
| 141 | 0 |  |  |  |  |  | my $values = $self->values($object); | 
| 142 | 0 |  |  |  |  |  | $entity->relationship_insert($self->name, $unique_values, @$values); | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =item merge | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | Merges relationship data. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =cut | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | sub merge { | 
| 153 | 0 |  |  | 0 | 1 |  | my ($self, $orm, $entity, $unique_values, $object) = @_; | 
| 154 | 0 |  |  |  |  |  | my $values = $self->values($object); | 
| 155 | 0 |  |  |  |  |  | $entity->relationship_merge($self->name, $unique_values, @$values); | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =item delete | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | Merges relationship data. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =cut | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | sub delete { | 
| 167 | 0 |  |  | 0 | 1 |  | my ($self, $orm, $entity, $unique_values, $object) = @_; | 
| 168 | 0 |  |  |  |  |  | my $values = $self->values($object); | 
| 169 | 0 |  |  |  |  |  | $entity->relationship_delete($self->name, $unique_values, @$values); | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | 1; | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | __END__ |