line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Persistence::Relationship::ToOne; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
152983
|
use strict; |
|
17
|
|
|
|
|
34
|
|
|
17
|
|
|
|
|
2160
|
|
4
|
17
|
|
|
17
|
|
168
|
use warnings; |
|
17
|
|
|
|
|
32
|
|
|
17
|
|
|
|
|
540
|
|
5
|
17
|
|
|
17
|
|
85
|
use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION); |
|
17
|
|
|
|
|
41
|
|
|
17
|
|
|
|
|
1109
|
|
6
|
|
|
|
|
|
|
|
7
|
17
|
|
|
17
|
|
94
|
use Abstract::Meta::Class ':all'; |
|
17
|
|
|
|
|
44
|
|
|
17
|
|
|
|
|
2763
|
|
8
|
17
|
|
|
17
|
|
92
|
use base qw (Exporter Persistence::Relationship); |
|
17
|
|
|
|
|
28
|
|
|
17
|
|
|
|
|
2410
|
|
9
|
17
|
|
|
17
|
|
143
|
use Carp 'confess'; |
|
17
|
|
|
|
|
33
|
|
|
17
|
|
|
|
|
10586
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = 0.01; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
@EXPORT_OK = qw(to_one); |
14
|
|
|
|
|
|
|
%EXPORT_TAGS = (all => \@EXPORT_OK); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 NAME |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Persistence::Relationship::ToOne - To one relationship |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 CLASS HIERARCHY |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Persistence::Relationship |
24
|
|
|
|
|
|
|
| |
25
|
|
|
|
|
|
|
+----Persistence::Relationship::ToOne |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 SYNOPSIS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
use Persistence::Relationship::ToOne ':all'; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#entity defintion |
32
|
|
|
|
|
|
|
my $entity_manager = Persistence::Entity::Manager->new(name => 'my_manager', connection_name => 'test'); |
33
|
|
|
|
|
|
|
my $emp_entity = Persistence::Entity->new( |
34
|
|
|
|
|
|
|
name => 'emp', |
35
|
|
|
|
|
|
|
alias => 'ep', |
36
|
|
|
|
|
|
|
primary_key => ['empno'], |
37
|
|
|
|
|
|
|
columns => [ |
38
|
|
|
|
|
|
|
sql_column(name => 'empno'), |
39
|
|
|
|
|
|
|
sql_column(name => 'ename', unique => 1), |
40
|
|
|
|
|
|
|
sql_column(name => 'job'), |
41
|
|
|
|
|
|
|
sql_column(name => 'deptno'), |
42
|
|
|
|
|
|
|
], |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $dept_entity = Persistence::Entity->new( |
46
|
|
|
|
|
|
|
name => 'dept', |
47
|
|
|
|
|
|
|
alias => 'dt', |
48
|
|
|
|
|
|
|
primary_key => ['deptno'], |
49
|
|
|
|
|
|
|
columns => [ |
50
|
|
|
|
|
|
|
sql_column(name => 'deptno'), |
51
|
|
|
|
|
|
|
sql_column(name => 'dname'), |
52
|
|
|
|
|
|
|
sql_column(name => 'loc') |
53
|
|
|
|
|
|
|
], |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$dept_entity->add_to_many_relationships(sql_relationship(target_entity => $emp_entity, join_columns => ['deptno'], order_by => 'deptno, empno')); |
57
|
|
|
|
|
|
|
$entity_manager->add_entities($dept_entity, $emp_entity); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
#object mapping |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
package Employee; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
use Abstract::Meta::Class ':all'; |
64
|
|
|
|
|
|
|
use Persistence::Entity ':all'; |
65
|
|
|
|
|
|
|
use Persistence::ORM ':all'; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
entity 'emp'; |
68
|
|
|
|
|
|
|
column empno=> has('$.id'); |
69
|
|
|
|
|
|
|
column ename => has('$.name'); |
70
|
|
|
|
|
|
|
column job => has '$.job'; |
71
|
|
|
|
|
|
|
to_one 'dept' => ( |
72
|
|
|
|
|
|
|
attribute => has ('$.dept', associated_class => 'Department'), |
73
|
|
|
|
|
|
|
cascade => ALL, |
74
|
|
|
|
|
|
|
fetch_method => EAGER, |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
package Department; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
use Abstract::Meta::Class ':all'; |
80
|
|
|
|
|
|
|
use Persistence::Entity ':all'; |
81
|
|
|
|
|
|
|
use Persistence::ORM ':all'; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
entity 'dept'; |
84
|
|
|
|
|
|
|
column deptno => has('$.id'); |
85
|
|
|
|
|
|
|
column dname => has('$.name'); |
86
|
|
|
|
|
|
|
column loc => has('$.location'); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 DESCRIPTION |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Represents to one relationship. |
92
|
|
|
|
|
|
|
Supports eager, lazy fetch, cascading operation (inert/update/delete). |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 EXPORT |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
to_one method by ':all' tag. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 METHODS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=over |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item to_one |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Create a new instance of to one relation. |
107
|
|
|
|
|
|
|
Takes associated entity's id as parameters |
108
|
|
|
|
|
|
|
and list of named parameters for Persistence::Relationship::OneToMany constructor. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
one_to_many 'wsus_user_service' => ( |
111
|
|
|
|
|
|
|
attribute => has('@.membership' => (associated_class => 'Membership')), |
112
|
|
|
|
|
|
|
fetch_method => EAGER, |
113
|
|
|
|
|
|
|
cascade => ALL, |
114
|
|
|
|
|
|
|
); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub to_one { |
120
|
0
|
|
|
0
|
1
|
|
my $package = caller(); |
121
|
0
|
|
|
|
|
|
__PACKAGE__->add_relationship($package, @_); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item deserialise_attribute |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Deserialises relation attribute |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub deserialise_attribute { |
132
|
0
|
|
|
0
|
1
|
|
my ($self, $object, $entity_manager, $orm) = @_; |
133
|
0
|
|
|
|
|
|
my $entity = $entity_manager->entity($orm->entity_name); |
134
|
0
|
|
|
|
|
|
my $attribute = $self->attribute; |
135
|
|
|
|
|
|
|
#avoid cycles while have eager retrieval |
136
|
0
|
0
|
|
|
|
|
if (my $pending_value = $entity_manager->has_pending_operation($self->name)) { |
137
|
0
|
|
|
|
|
|
$attribute->set_value($object, $pending_value); |
138
|
0
|
|
|
|
|
|
return ; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
my @rows = $entity->relationship_query( |
142
|
|
|
|
|
|
|
$self->name, |
143
|
|
|
|
|
|
|
ref($object) => $attribute->associated_class, |
144
|
|
|
|
|
|
|
$orm->column_values($object, $entity->primary_key) |
145
|
|
|
|
|
|
|
); |
146
|
|
|
|
|
|
|
|
147
|
0
|
0
|
|
|
|
|
if (@rows) { |
148
|
0
|
0
|
|
|
|
|
confess "relationhip " . $self->name . "to one returned " . (@rows) . " rows" if(@rows > 1); |
149
|
0
|
|
|
|
|
|
my $mutator = $attribute->mutator; |
150
|
0
|
|
|
|
|
|
$object->$mutator($rows[0]); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item insert |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Inserts relationship data. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub insert { |
162
|
0
|
|
|
0
|
1
|
|
my ($self, $orm, $entity, $dataset, $object) = @_; |
163
|
0
|
|
|
|
|
|
my $attribute = $self->attribute; |
164
|
0
|
0
|
|
|
|
|
my $value = $attribute->get_value($object) or return; |
165
|
0
|
|
|
|
|
|
$entity->relationship_insert($self->name, $dataset, $value); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item merge |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Merges relationship data. #what if lazy |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub merge { |
176
|
0
|
|
|
0
|
1
|
|
my ($self, $orm, $entity, $dataset, $object) = @_; |
177
|
0
|
|
|
|
|
|
my $value = $self->value($object); |
178
|
0
|
|
|
|
|
|
$entity->relationship_merge($self->name, $dataset, $value); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item delete |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Merges relationship data. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub delete { |
190
|
0
|
|
|
0
|
1
|
|
my ($self, $orm, $entity, $dataset, $object) = @_; |
191
|
0
|
|
|
|
|
|
my $value = $self->value($object); |
192
|
0
|
|
|
|
|
|
$entity->relationship_delete($self->name, $dataset, $value); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
1; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
__END__ |