line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::NinjaORM::StaticClassInfo; |
2
|
|
|
|
|
|
|
|
3
|
73
|
|
|
73
|
|
48639
|
use 5.010; |
|
73
|
|
|
|
|
272
|
|
|
73
|
|
|
|
|
3067
|
|
4
|
|
|
|
|
|
|
|
5
|
73
|
|
|
73
|
|
423
|
use strict; |
|
73
|
|
|
|
|
141
|
|
|
73
|
|
|
|
|
2269
|
|
6
|
73
|
|
|
73
|
|
422
|
use warnings; |
|
73
|
|
|
|
|
146
|
|
|
73
|
|
|
|
|
2744
|
|
7
|
|
|
|
|
|
|
|
8
|
73
|
|
|
73
|
|
435
|
use Carp; |
|
73
|
|
|
|
|
141
|
|
|
73
|
|
|
|
|
6075
|
|
9
|
73
|
|
|
73
|
|
48826
|
use Data::Validate::Type; |
|
73
|
|
|
|
|
506425
|
|
|
73
|
|
|
|
|
40684
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
DBIx::NinjaORM::StaticClassInfo - Hold the configuration information for L classes. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 VERSION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Version 3.0.2 |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = '3.0.2'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 DESCRIPTION |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
This package is used to store and retrieve defaults as well as general |
29
|
|
|
|
|
|
|
information for a specific class. It allows for example indicating what table |
30
|
|
|
|
|
|
|
the objects will be related to, or what database handle to use. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Here's the full list of the options that can be set or overridden: |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over 4 |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=item * default_dbh |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
The database handle to use when performing queries. The methods that interact |
39
|
|
|
|
|
|
|
with the database always provide a C argument to allow using a specific |
40
|
|
|
|
|
|
|
database handle, but setting it here means you won't have to systematically |
41
|
|
|
|
|
|
|
pass that argument. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$info->{'default_dbh'} = DBI->connect( |
44
|
|
|
|
|
|
|
"dbi:mysql:[database_name]:localhost:3306", |
45
|
|
|
|
|
|
|
"[user]", |
46
|
|
|
|
|
|
|
"[password]", |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item * memcache |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Optionally, C uses memcache to cache objects and queries, |
52
|
|
|
|
|
|
|
in conjunction with the C and C arguments. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
If you want to enable the cache features, you can set this to a valid |
55
|
|
|
|
|
|
|
C object (or a compatible module, such as |
56
|
|
|
|
|
|
|
C). |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$info->{'memcache'} = Cache::Memcached::Fast->new( |
59
|
|
|
|
|
|
|
{ |
60
|
|
|
|
|
|
|
servers => |
61
|
|
|
|
|
|
|
[ |
62
|
|
|
|
|
|
|
'localhost:11211', |
63
|
|
|
|
|
|
|
], |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item * table_name |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Mandatory, the name of the table that this class will be the interface for. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Interface with a 'books' table. |
72
|
|
|
|
|
|
|
$info->{'table_name'} = 'books'; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * primary_key_name |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
The name of the primary key on the table specified with C. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
$info->{'primary_key_name'} = 'book_id'; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item * list_cache_time |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Control the list cache, which is an optional cache system in |
83
|
|
|
|
|
|
|
C to store how search criteria translate into object IDs. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
By default it is disabled (with C), but it is activated by setting it to |
86
|
|
|
|
|
|
|
an integer that represents the cache time in seconds. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Cache for 10 seconds. |
89
|
|
|
|
|
|
|
$info->{'list_cache_time'} = 10; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Don't cache. |
92
|
|
|
|
|
|
|
$info->{'list_cache_time'} = undef; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
A good use case for this would be retrieving a list of books for a given author. |
95
|
|
|
|
|
|
|
We would pass the author ID as a search criteria, and the resulting list of book |
96
|
|
|
|
|
|
|
objects does not change often. Provided that you can tolerate a 1 hour delay |
97
|
|
|
|
|
|
|
for a new book to show up associated with a given author, then it makes sense |
98
|
|
|
|
|
|
|
to set the list_cache_time to 3600 and save most of the queries to find what |
99
|
|
|
|
|
|
|
book otherwise belongs to the author. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item * object_cache_time |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Control the object cache, which is an optional cache system in |
104
|
|
|
|
|
|
|
C to store the objects returned and be able to look them up |
105
|
|
|
|
|
|
|
by object ID. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
By default it is disabled (with C), but it is activated by setting it to |
108
|
|
|
|
|
|
|
an integer that represents the cache time in seconds. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Cache for 10 seconds. |
111
|
|
|
|
|
|
|
$info->{'object_cache_time'} = 10; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Don't cache. |
114
|
|
|
|
|
|
|
$info->{'object_cache_time'} = undef; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
A good use case for this are objects that are expensive to build. You will see |
117
|
|
|
|
|
|
|
more in C on how to cache objects. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item * unique_fields |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
The list of unique fields on the object. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Note: L does not support unique indexes made of more than one |
124
|
|
|
|
|
|
|
field. If you add more than one field in this arrayref, the ORM will treat them |
125
|
|
|
|
|
|
|
as separate unique indexes. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Declare books.isbn as unique. |
128
|
|
|
|
|
|
|
$info->{'unique_fields'} = [ 'isbn' ]; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Declare books.isbn and books.upc as unique. |
131
|
|
|
|
|
|
|
$info->{'unique_fields'} = [ 'isbn', 'upc' ]; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item * filtering_fields |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
The list of fields that can be used to filter on in C. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Allow filtering based on the book name and author ID. |
138
|
|
|
|
|
|
|
$info->{'unique_fields'} = [ 'name', 'author_id' ]; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * readonly_fields |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
The list of fields that cannot be set directly. They will be populated in |
143
|
|
|
|
|
|
|
C, but you won't be able to insert / update / set them directly. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item * has_created_field |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Indicate whether the table has a field name C to store the UNIX time |
148
|
|
|
|
|
|
|
at which the row was created. Default: 1. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# The table doesn't have a 'created' field. |
151
|
|
|
|
|
|
|
$info->{'has_created_field'} = 0; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item * has_modified_field |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Indicate whether the table has a field name C to store the UNIX time |
156
|
|
|
|
|
|
|
at which the row was modified. Default: 1. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# The table doesn't have a 'modified' field. |
159
|
|
|
|
|
|
|
$info->{'has_modified_field'} = 0; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=item * cache_key_field |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
By default, the object cache uses the primary key value to make cached objects |
164
|
|
|
|
|
|
|
available to look up, but this allows specifying a different field for that |
165
|
|
|
|
|
|
|
purpose. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
For example, you may want to use books.isbn instead of books.book_id to cache |
168
|
|
|
|
|
|
|
objects: |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$info->{'cache_key_field'} = 'isbn'; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item * verbose |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Add debugging and tracing information, 0 by default. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Show debugging information for operations on this class. |
177
|
|
|
|
|
|
|
$info->{'verbose'} = 1; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item * verbose_cache_operations |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Add information in the logs regarding cache operations and uses. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=back |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 SYNOPSIS |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
my $static_class_info = $class->SUPER::static_class_info(); |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# Set or override information. |
191
|
|
|
|
|
|
|
$static_class_info->set( |
192
|
|
|
|
|
|
|
{ |
193
|
|
|
|
|
|
|
table_name => 'books', |
194
|
|
|
|
|
|
|
primary_key_name => 'book_id', |
195
|
|
|
|
|
|
|
default_dbh => DBI->connect( |
196
|
|
|
|
|
|
|
"dbi:mysql:[database_name]:localhost:3306", |
197
|
|
|
|
|
|
|
"[user]", |
198
|
|
|
|
|
|
|
"[password]", |
199
|
|
|
|
|
|
|
), |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
); |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
# Retrieve information. |
204
|
|
|
|
|
|
|
my $table_name = $static_class_info->get('table_name'); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 METHODS |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 new() |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Create a new L object. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
my $static_class_info = DBIx::NinjaORM::StaticClassInfo->new(); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub new |
218
|
|
|
|
|
|
|
{ |
219
|
82
|
|
|
82
|
1
|
3617
|
my ( $class ) = @_; |
220
|
|
|
|
|
|
|
|
221
|
82
|
|
|
|
|
1831
|
return bless( |
222
|
|
|
|
|
|
|
{ |
223
|
|
|
|
|
|
|
'default_dbh' => undef, |
224
|
|
|
|
|
|
|
'memcache' => undef, |
225
|
|
|
|
|
|
|
'table_name' => undef, |
226
|
|
|
|
|
|
|
'primary_key_name' => undef, |
227
|
|
|
|
|
|
|
'list_cache_time' => undef, |
228
|
|
|
|
|
|
|
'object_cache_time' => undef, |
229
|
|
|
|
|
|
|
'unique_fields' => [], |
230
|
|
|
|
|
|
|
'filtering_fields' => [], |
231
|
|
|
|
|
|
|
'readonly_fields' => [], |
232
|
|
|
|
|
|
|
'has_created_field' => 1, |
233
|
|
|
|
|
|
|
'has_modified_field' => 1, |
234
|
|
|
|
|
|
|
'cache_key_field' => undef, |
235
|
|
|
|
|
|
|
'verbose' => 0, |
236
|
|
|
|
|
|
|
'verbose_cache_operations' => 0, |
237
|
|
|
|
|
|
|
}, |
238
|
|
|
|
|
|
|
$class, |
239
|
|
|
|
|
|
|
); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head2 get() |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Retrieve the value of one of the configuration variables. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
my $value = $static_class_info->get( $key ); |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=cut |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub get |
252
|
|
|
|
|
|
|
{ |
253
|
965
|
|
|
965
|
1
|
4498
|
my ( $self, $key ) = @_; |
254
|
|
|
|
|
|
|
|
255
|
965
|
100
|
|
|
|
3121
|
croak "The key name must be defined" |
256
|
|
|
|
|
|
|
if !defined( $key ); |
257
|
964
|
100
|
|
|
|
2785
|
croak "The key '$key' is not valid" |
258
|
|
|
|
|
|
|
if !exists( $self->{ $key } ); |
259
|
|
|
|
|
|
|
|
260
|
963
|
|
|
|
|
6169
|
return $self->{ $key }; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=head2 set() |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
Set values for one or more configuration variables. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
$static_class_info->set( |
268
|
|
|
|
|
|
|
{ |
269
|
|
|
|
|
|
|
unique_fields => [], |
270
|
|
|
|
|
|
|
filtering_fields => [], |
271
|
|
|
|
|
|
|
... |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
); |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=cut |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
sub set ## no critic (NamingConventions::ProhibitAmbiguousNames, Subroutines::RequireArgUnpacking) |
278
|
|
|
|
|
|
|
{ |
279
|
64
|
100
|
|
64
|
1
|
3276836
|
croak 'The first argument passed must be a hashref' |
280
|
|
|
|
|
|
|
if !Data::Validate::Type::is_hashref( $_[1] ); |
281
|
|
|
|
|
|
|
|
282
|
62
|
|
|
|
|
2320
|
my ( $self, $values ) = @_; |
283
|
|
|
|
|
|
|
|
284
|
62
|
|
|
|
|
299
|
foreach my $key ( keys %$values ) |
285
|
|
|
|
|
|
|
{ |
286
|
257
|
100
|
|
|
|
951
|
croak "The key '$key' is not valid" |
287
|
|
|
|
|
|
|
if !exists( $self->{ $key } ); |
288
|
|
|
|
|
|
|
|
289
|
256
|
|
|
|
|
642
|
$self->{ $key } = $values->{ $key }; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
61
|
|
|
|
|
388
|
return; |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head1 BUGS |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
Please report any bugs or feature requests through the web interface at |
299
|
|
|
|
|
|
|
L. |
300
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
301
|
|
|
|
|
|
|
your bug as I make changes. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head1 SUPPORT |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
perldoc DBIx::NinjaORM::StaticClassInfo |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
You can also look for information at: |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=over 4 |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=item * GitHub's request tracker |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
L |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
L |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=item * CPAN Ratings |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
L |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=item * MetaCPAN |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
L |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=back |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
=head1 AUTHOR |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
Guillaume Aubert, C<< >>. |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
Copyright 2009-2014 Guillaume Aubert. |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it under |
344
|
|
|
|
|
|
|
the terms of the GNU General Public License version 3 as published by the Free |
345
|
|
|
|
|
|
|
Software Foundation. |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY |
348
|
|
|
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
349
|
|
|
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details. |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with |
352
|
|
|
|
|
|
|
this program. If not, see http://www.gnu.org/licenses/ |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=cut |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
1; |