SQLEngine/Record/Hooks.pm

filter: Statementstmt 90.1% 64 / 71 Branchbran 56.2% 9 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 76.8% 96 / 125 CC 13 SCAR 10.5
CC
13
Cov
76%
insert_record
5.1
update_record
5.1
delete_record
3.7
CRAP 2.9
« SQLEngine/Schema/TableSet.pm ↑ Summary SQLEngine/Driver/Trait/DatabaseFlavors.pm »
1 =head1 NAME
2  
3 DBIx::SQLEngine::Record::Hooks - Flexible Pre/Post Hooks
4  
5 =head1 SYNOPSIS
6  
7 B<Setup:> Several ways to create a class.
8  
9 my $sqldb = DBIx::SQLEngine->new( ... );
10  
11 $class_name = $sqldb->record_class( $table_name, undef, 'Hooks' );
12
13 $sqldb->record_class( $table_name, 'My::Record', 'Hooks' );
14
15 package My::Record;
16 use DBIx::SQLEngine::Record::Class '-isasubclass', 'Hooks';
17 My::Record->table( $sqldb->table($table_name) );
18  
19 B<Hooks:> Register subs for callbacks.
20  
21 DBIx::SQLEngine::Record::Hooks->install_hooks(
22 post_new => sub { warn "Record $_[0] created" },
23 post_fetch => sub { warn "Record $_[0] loaded" },
24 post_insert => sub { warn "Record $_[0] inserted" },
25 post_update => sub { warn "Record $_[0] updated" },
26 post_delete => sub { warn "Record $_[0] deleted" },
27 );
28
29 $class_name->install_hooks( %hook_subs );
30  
31 $record->install_hooks( %hook_subs );
32  
33 B<Basics:> Layered over superclass.
34  
35 # Calls post_fetch hooks on record
36 $record = $class_name->fetch_record( $primary_key );
37  
38 # Calls post_fetch hooks on each record
39 @records = $class_name->fetch_select(%clauses)->records;
40
41 # Calls post_new hooks on empty record
42 $record = $class_name->new_with_values(somefield => 'My Value');
43  
44 # Calls ok_insert, pre_insert, and post_insert hooks
45 $record->insert_record();
46
47 # Calls ok_update, pre_update, and post_update hooks
48 $record->update_record();
49
50 # Calls ok_delete, pre_delete, and post_delete hooks
51 $record->delete_record();
52  
53  
54 =head1 DESCRIPTION
55  
56 This package provides a callback layer for DBIx::SQLEngine::Record objects.
57  
58 Don't use this module directly; instead, pass its name as a trait when you create a new record class. This package provides a multiply-composable collection of functionality for Record classes. It is combined with the base class and other traits by DBIx::SQLEngine::Record::Class.
59  
60 =cut
61  
62 ########################################################################
63  
64 package DBIx::SQLEngine::Record::Hooks;
65  
66 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 ########################################################################
70  
71 ########################################################################
72  
73 =head1 HOOKS INTERFACE
74  
75 Many of the methods below are labeled "Inheritable Hook." These methods allow you to register callbacks which are then invoked at specific points in each record's lifecycle. You can add these callbacks to all record classes, to a particular class, or even to a particular object instance.
76  
77 These hooks act like the triggers supported by some databases; you can ensure that every time a record is updated in a specific table, certain other actions occur automatically.
78  
79 To register a callback, call the install_hooks method, and pass it pairs of a hook method name, and a subroutine reference, as follows: I<callee>->install_hooks( I<methodname> => I<coderef>, ... ).
80  
81 =over 4
82  
83 =item install_hooks()
84  
85 $classname->install_hooks( $hook_name => \&my_sub, ... )
86 $record->install_hooks( $hook_name => \&my_sub, ... )
87  
88 Registers one or more callbacks. Accepts pairs of a hook method name, and a subroutine reference.
89  
90 For more about the implementation of the Hook mechanism, see L<Class::MakeMethods::Composite::Inheritable>.
91  
92 =back
93  
94 B<Examples:>
95  
96 Here are a few examples to show the possibilities this provides you with:
97  
98 =over 4
99  
100 =item *
101  
102 To have each record write to a log when it's loaded from the database:
103  
104 sub log_fetch { my $record = shift; warn "Loaded record $record->{id}" } );
105 MyClass->install_hooks( post_fetch => \&log_fetch );
106  
107 =item *
108  
109 To make a class "read-only" by preventing all inserts, updates, and deletes:
110  
111 my $refusal = sub { return 0 };
112 MyClass->install_hooks(
113 ok_insert => $refusal,
114 ok_update => $refusal,
115 ok_delete => $refusal,
116 );
117  
118 =item *
119  
120 To have a particular record automatically save any changes you've made to it when it goes out of scope:
121  
122 my $record = MyClass->fetch_one( ... );
123 $record->install_hooks( pre_destroy => sub { (shift)->save_record } );
124  
125 =back
126  
127 =cut
128  
129 2 ▶sub install_hooks {
Subroutine install_hooks: called CC=2 SCAR=6.9
Pod documented
130 2 my $callee = shift;
131 2 ▶ while ( my( $method_name, $code_ref ) = splice( @_, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 5 2 7
132 5 $callee->$method_name(
133 Class::MakeMethods::Composite::Inheritable->Hook( $code_ref )
134 );
135 }
136 }
137  
138 ########################################################################
139  
140 ########################################################################
141  
142 =head1 SIMPLE RECORD INTERFACE
143  
144 =head2 Constructor
145  
146 You may create your own records for new instances, or fetch records from the database as described in L</"FETCHING DATA (SQL DQL)">
147  
148 =over 4
149  
150 =item new_empty_record()
151  
152 $class_name->new_empty_record() : $empty_record
153  
154 Adds support for post_new hook.
155  
156 =item post_new()
157  
158 Inheritable Hook. Add functions which should be called immediately after each record is created and initialized.
159  
160 =back
161  
162 =cut
163  
164 6 ▶sub new_empty_record {
Subroutine new_empty_record: called CC=1 SCAR=0.0
Pod documented
165 6 my $record = (shift)->NEXT('new_empty_record', @_ );
166 6 $record->post_new;
167 6 return $record;
168 }
169  
170 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'post_new' );
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 ########################################################################
173  
174 =head2 Destructor
175  
176 Automatically invoked when record is being garbage collected.
177  
178 =over 4
179  
180 =item DESTROY()
181  
182 $record->DESTROY()
183  
184 Adds support for pre_destroy hook.
185  
186 =item pre_destroy
187  
188 $record->pre_destroy ()
189  
190 Inheritable Hook. Add functions which should be called when an individual record is being garbage collected.
191  
192 =back
193  
194 =cut
195  
196 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'pre_destroy');
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
199 8 my $self = shift;
200 8 $self->pre_destroy();
201 8 $self->NEXT('DESTROY');
202 8 $self;
203 }
204  
205 ########################################################################
206  
207 ########################################################################
208  
209 =head1 FETCHING DATA (SQL DQL)
210  
211 =head2 Vivifying Records From The Database
212  
213 These methods are called internally by the various select methods and do not need to be called directly.
214  
215 =over 4
216  
217 =item record_from_db_data()
218  
219 $class_name->record_from_db_data( $hash_ref )
220 $class_name->record_from_db_data( $hash_ref ) : $record
221 $class_name->record_from_db_data( %hash_contents ) : $record
222  
223 Adds support for post_fetch hook.
224  
225 =item record_set_from_db_data()
226  
227 $class_name->record_set_from_db_data( $hash_array_ref )
228 $class_name->record_set_from_db_data( $hash_array_ref ) : $record_set
229 $class_name->record_set_from_db_data( @hash_refs ) : $record_set
230  
231 Adds support for post_fetch hook.
232  
233 =item post_fetch()
234  
235 Inheritable Hook. Add functions which should be called immediately after each record is retrieved from the database.
236  
237 =back
238  
239 =cut
240  
241  
242 1 ▶use Class::MakeMethods::Composite::Inheritable( hook=>'post_fetch' );
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 # $row_class->record_from_db_data( $hash_ref );
245 # $row = $row_class->record_from_db_data( $hash_ref );
246 # $row = $row_class->record_from_db_data( %hash_contents );
247 2 ▶sub record_from_db_data {
Subroutine record_from_db_data: called CC=2 SCAR=7.1
Pod documented
248 2 ▶ my $record = (shift)->NEXT('record_from_db_data', @_ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $record = (shift())->NEXT("record_from_db_data", @_) or return 2 0 2
249 2 $record->post_fetch;
250 2 return $record;
251 }
252  
253 2 ▶sub record_set_from_db_data {
Subroutine record_set_from_db_data: called CC=3 SCAR=12.0
Pod documented
254 2 ▶ my $recordset = (shift)->NEXT('record_set_from_db_data', @_ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $recordset = (shift())->NEXT("record_set_from_db_data", @_) or return 2 0 2
255 0 foreach my $record ( @$recordset ) { $record->post_fetch }
256 2 return $recordset;
257 }
258  
259 ########################################################################
260  
261 ########################################################################
262  
263 =head1 EDITING DATA (SQL DML)
264  
265 =head2 Insert to Add Records
266  
267 After constructing a record with one of the new_*() methods, you may save any changes by calling insert_record.
268  
269 =over 4
270  
271 =item insert_record
272  
273 $record_obj->insert_record() : $flag
274  
275 Attempt to insert the record into the database.
276  
277 Calls ok_insert to ensure that it's OK to insert this row, and aborts if any of hook subroutines return 0.
278  
279 Calls any pre_insert hooks, then calls the superclass insert_row method, then calls any post_insert hooks.
280  
281 Returns undef if the update was aborted, or the record if the insert was successful.
282  
283 =item ok_insert
284  
285 Inheritable Hook. Add functions which should be called to validate rows before they are inserted.
286  
287 =item pre_insert
288  
289 Inheritable Hook. Add functions which should be called immediately before a row is inserted.
290  
291 =item post_insert
292  
293 Inheritable Hook. Add functions which should be called after a row is inserted.
294  
295 =back
296  
297 =cut
298  
299 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'ok_insert pre_insert post_insert');
Subroutine BEGIN: called CC=1 SCAR=0.0
300  
301 # $record->insert_record()
302 2 ▶sub insert_record {
Subroutine insert_record: called CC=4 SCAR=16.3
Pod documented
303 2 my $self = shift;
304 2 ▶ my $class = ref( $self ) or croak("Not a class method");
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or croak("Not a class method") 2 0 2
305
306 2 my @flags = $self->ok_insert();
307 0 ▶ if ( grep { length $_ and ! $_ } @flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {length $_ and not $_;} @flags) 0 2 2
Condition0%0/3
length $_ && !$_
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && !$_
length $_ !$_
Truth table0%0/3
A && not(B)
A= length $_B= !$_
ABresult
0X0
100
111
308 # warn "Cancelling insert of $self: " . join(', ', map "'$_'", @flags);
309 0 return undef;
310 }
311 2 $self->pre_insert();
312 2 $self->NEXT('insert_record');
313 2 $self->post_insert();
314 2 $self;
315 }
316  
317 ########################################################################
318  
319 =head2 Update to Change Records
320  
321 After retrieving a record with one of the fetch methods, you may save any changes by calling update_record.
322  
323 =over 4
324  
325 =item update_record
326  
327 $record_obj->update_record() : $record_count
328  
329 Attempts to update the record using its primary key as a unique identifier.
330  
331 Calls ok_update to ensure that it's OK to update this row, and aborts if any of hook subroutines return 0.
332  
333 Calls any pre_update hooks, then calls the superclass method, then calls any post_update hooks.
334  
335 Returns undef if the update was aborted, or the record if the update was successful.
336  
337 =item ok_update
338  
339 Inheritable Hook. Add functions which to use to validate rows before they are updated. Return 0 to abort the update.
340  
341 =item pre_update
342  
343 Inheritable Hook. Add functions which should be called immediately before a row is updated.
344  
345 =item post_update
346  
347 Inheritable Hook. Add functions which should be called immediately after a row is updated.
348  
349 =back
350  
351 =cut
352  
353 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'ok_update pre_update post_update');
Subroutine BEGIN: called CC=1 SCAR=0.0
354  
355 # $record->update_record()
356 1 ▶sub update_record {
Subroutine update_record: called CC=4 SCAR=16.3
Pod documented
357 1 my $self = shift;
358 1 ▶ my $class = ref( $self ) or croak("Not a class method");
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or croak("Not a class method") 1 0 1
359 1 my @flags = $self->ok_update;
360 0 ▶ if ( grep { length $_ and ! $_ } @flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {length $_ and not $_;} @flags) 0 1 1
Condition0%0/3
length $_ && !$_
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && !$_
length $_ !$_
Truth table0%0/3
A && not(B)
A= length $_B= !$_
ABresult
0X0
100
111
361 # warn "Cancelling update of $self: " . join(', ', map "'$_'", @flags );
362 0 return undef;
363 }
364 # warn "About to update $self: " . join(', ', map "'$_'", @flags );
365 1 $self->pre_update();
366 1 $self->NEXT('update_record');
367 1 $self->post_update();
368 1 $self;
369 }
370  
371 ########################################################################
372  
373 =head2 Delete to Remove Records
374  
375 =over 4
376  
377 =item delete_record()
378  
379 $record_obj->delete_record() : $record_count
380  
381 Delete this existing record based on its primary key.
382  
383 Checks to see if any of the pre_delete results is "0". If not, calls the superclass method.
384  
385 Returns 1 if the deletion was successful, or 0 if it was aborted.
386  
387 =item ok_delete
388  
389 $record->ok_delete () : @booleans
390  
391 Inheritable Hook. Add functions which should be used to use to validate rows before they are updated. Return 0 to abort the deletion.
392  
393 =item pre_delete
394  
395 $record->pre_delete ()
396  
397 Inheritable Hook. Add functions which should be called before a row is deleted.
398  
399 =item post_delete
400  
401 $record->post_delete ()
402  
403 Inheritable Hook. Add functions which should be called after a row is deleted.
404  
405 =back
406  
407 =cut
408  
409 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'ok_delete pre_delete post_delete');
Subroutine BEGIN: called CC=1 SCAR=0.0
410  
411 # $success = $record->delete_record();
412 1 ▶sub delete_record {
Subroutine delete_record: called CC=3 SCAR=13.1
Pod documented
413 1 my $self = shift;
414 1 my @flags = $self->ok_delete;
415 0 ▶ if ( grep { length $_ and ! $_ } @flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {length $_ and not $_;} @flags) 0 1 1
Condition0%0/3
length $_ && !$_
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && !$_
length $_ !$_
Truth table0%0/3
A && not(B)
A= length $_B= !$_
ABresult
0X0
100
111
416 # warn "Cancelling delete of $self: " . join(', ', map "'$_'", @flags );
417 0 return 0;
418 }
419 # warn "About to delete $self: " . join(', ', map "'$_'", @flags );
420 1 $self->pre_delete();
421 1 $self->NEXT('delete_record');
422 1 $self->post_delete();
423 1 return 1;
424 }
425  
426 ########################################################################
427  
428 ########################################################################
429  
430 =head1 SEE ALSO
431  
432 For more about the Record classes, see L<DBIx::SQLEngine::Record::Class>.
433  
434 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
435  
436 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
437 this distribution, including installation and license information.
438  
439 =cut
440  
441 ########################################################################
442  
443 1 1;
« SQLEngine/Schema/TableSet.pm ↑ Summary SQLEngine/Driver/Trait/DatabaseFlavors.pm »