line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Persistence::ValueGenerator; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
119553
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
202
|
|
4
|
6
|
|
|
6
|
|
33
|
use warnings; |
|
6
|
|
|
|
|
21
|
|
|
6
|
|
|
|
|
207
|
|
5
|
6
|
|
|
6
|
|
31
|
use vars qw($VERSION); |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
249
|
|
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
1179
|
use Abstract::Meta::Class ':all'; |
|
6
|
|
|
|
|
12837
|
|
|
6
|
|
|
|
|
3487
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
abstract_class; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = 0.01; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Persistence::ValueGenerator - Unqiue value generator. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use Persistence::ValueGenerator::TableGenerator; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $generator = Persistence::ValueGenerator::TableGenerator->new( |
24
|
|
|
|
|
|
|
entity_manager => $entity_manager_name, |
25
|
|
|
|
|
|
|
name => 'empno_generator', |
26
|
|
|
|
|
|
|
table => 'seq_generator', |
27
|
|
|
|
|
|
|
primary_key_column_name => 'pk_column', |
28
|
|
|
|
|
|
|
primary_key_column_value => 'empno', |
29
|
|
|
|
|
|
|
value_column => 'value_column', |
30
|
|
|
|
|
|
|
allocation_size => 5, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $entity = Persistence::Entity->new( |
34
|
|
|
|
|
|
|
name => 'emp', |
35
|
|
|
|
|
|
|
unique_expression => 'empno', |
36
|
|
|
|
|
|
|
primary_key => ['empno'], |
37
|
|
|
|
|
|
|
columns => [ |
38
|
|
|
|
|
|
|
sql_column(name => 'ename'), |
39
|
|
|
|
|
|
|
sql_column(name => 'empno'), |
40
|
|
|
|
|
|
|
sql_column(name => 'deptno') |
41
|
|
|
|
|
|
|
], |
42
|
|
|
|
|
|
|
value_generators => {empno => 'empno_generator'}, |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
# or |
45
|
|
|
|
|
|
|
# $entity->add_value_generators(empno => 'empno_generator'); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$entity_manager->add_entities($entity); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 DESCRIPTION |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Abstract class for value generator's class. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 EXPORT |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
None |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 ATTRIBUTES |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=over |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=item name |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Defines the name of the Persistence::ValueGenerator::TableGenerator instance and is the name referenced in the |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
has '$.name' => (required => 1); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item allocation_size |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Defined how much the counter will be incremented when entity queries the table for a new value, |
74
|
|
|
|
|
|
|
This feature is to cache blocks so that it doesn't have to go to the database every time it needs a new ID. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
has '$.allocation_size' => (default => 20); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item _cached_seq |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Stores counter for current seq and allocation_size |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
has '$._cached_seq'; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item entity_manager_name |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Entity manager name |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
has '$.entity_manager_name' => (required => 1); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=item _entity_manager |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Caches entity manager instance. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
has '$._entity_manager' => (associated_class => 'Persistence::Entity::Manager'); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=back |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 METHODS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=over |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
{ |
116
|
|
|
|
|
|
|
my %generators; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item initialise |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub initialise { |
123
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
124
|
0
|
|
|
|
|
|
$generators{$self->name} = $self; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item generator |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Returns generator instance, takes table generator name. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=cut |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub generator { |
135
|
0
|
|
|
0
|
1
|
|
my ($class, $name) = @_; |
136
|
0
|
|
|
|
|
|
$generators{$name}; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item nextval |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Returns next value for the instance generator |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub nextval { |
148
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
149
|
0
|
0
|
|
|
|
|
my $result = $self->has_cached_seq ? $self->_cached_seq : $self->retrieve_next_value; |
150
|
0
|
|
|
|
|
|
$self->_cached_seq($result + 1); |
151
|
0
|
|
|
|
|
|
$result; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item retrieve_next_value |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Abstract method retrieve_next_value |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
abstract 'retrieve_next_value'; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item has_cached_seq |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Return true if objects holds cached_seq. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub has_cached_seq { |
171
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
172
|
0
|
0
|
|
|
|
|
my $cached_seq = $self->_cached_seq or return; |
173
|
0
|
0
|
|
|
|
|
my $allocation_size = $self->allocation_size or return; |
174
|
0
|
0
|
0
|
|
|
|
return if($cached_seq && ! (($cached_seq -1) % ($allocation_size))); |
175
|
0
|
|
|
|
|
|
$self; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item entity_manager |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Returns entity manager. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub entity_manager { |
186
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
187
|
0
|
0
|
|
|
|
|
$self->_entity_manager || $self->_entity_manager(Persistence::Entity::Manager->manager($self->entity_manager_name)); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
__END__ |