SQLEngine/Record/Class.pm

filter: Statementstmt 76.6% 46 / 60 Branchbran 35.0% 7 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 65.6% 67 / 102 CC 13 SCAR 16.6
CC
13
Cov
65%
import
7.8
new_subclass
7.4
generate_subclass_name_for_table
6.7
CRAP 5.3
« SQLEngine/Utility/CloneWithParams.pm ↑ Summary SQLEngine/Criteria/Compound.pm »
1 =head1 NAME
2  
3 DBIx::SQLEngine::Record::Class - Factory for Record Classes
4  
5 =head1 SYNOPSIS
6  
7 B<Setup:> Several ways to create a class.
8  
9 $sqldb = DBIx::SQLEngine->new( ... );
10
11 $class_name = $sqldb->record_class( $table_name, @traits );
12
13 $sqldb->record_class( $table_name, $class_name, @traits );
14
15 package My::Record;
16 use DBIx::SQLEngine::Record::Class '-isasubclass', 'Table', 'Hooks';
17 My::Record->table( $sqldb->table($table_name) );
18  
19 B<Basics:> Common operations on a record.
20
21 $record = $class_name->new_with_values(somefield => 'My Value');
22
23 print $record->get_values( 'somefield' );
24  
25 $record->change_values( somefield => 'New Value' );
26  
27 B<Fetch:> Retrieve records by ID or other query.
28  
29 $record = $class_name->select_record( $primary_key );
30
31 @records = $class_name->fetch_select(%clauses)->records;
32  
33 B<Modify:> Write changes to the data source.
34  
35 $record->insert_record();
36
37 $record->update_record();
38
39 $record->delete_record();
40  
41 =head1 DESCRIPTION
42  
43 DBIx::SQLEngine::Record::Class is a factory for Record classes.
44  
45 You can use this package to create a class whose instances represent each of the rows in a SQL database table.
46  
47 =cut
48  
49 ########################################################################
50  
51 package DBIx::SQLEngine::Record::Class;
52  
53 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 5 require DBIx::SQLEngine::Record::Base;
57  
58 ########################################################################
59  
60 =head1 CLASS INSTANTIATION
61  
62 =head2 Subclass Factory
63  
64 =over 4
65  
66 =item import
67  
68 package My::Record;
69 use DBIx::SQLEngine::Record::Class '-isasubclass';
70 use DBIx::SQLEngine::Record::Class '-isasubclass', @Traits;
71  
72 Allows for a simple declaration of inheritance.
73  
74 =back
75  
76 =cut
77  
78 0 ▶sub import {
Subroutine import: not called CC=3 SCAR=20.6
79 0 my $factory = shift;
80  
81 0 ▶ return unless ( @_ );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
82
83 0 ▶ if ( $_[0] eq '-isasubclass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq '-isasubclass') { } 0 0 0
84 0 shift;
85 0 my $base = $factory->class( @_ );
86  
87 0 my $target_class = ( caller )[0];
88 5 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 0 push @{"$target_class\::ISA"}, $base;
90 } else {
91 0 croak("Unsupported import '$_[0]'")
92 }
93 }
94  
95 ########################################################################
96  
97 =head2 Mixin Class Factory
98  
99 =over 4
100  
101 =item class()
102  
103 $factory->class() : $base_class
104 $factory->class( @traits ) : $class_name
105  
106 Build an ad-hod class with mixins.
107  
108 See the documentation for Class::MixinFactory to learn more about developing custom mixin classes.
109  
110 =back
111  
112 The MixinFactory is configured with the following package names:
113  
114 =over 12
115  
116 =item base_class
117  
118 DBIx::SQLEngine::Record::Base
119  
120 =item mixin_prefix
121  
122 DBIx::SQLEngine::Record
123  
124 =item mixed_prefix
125  
126 DBIx::SQLEngine::Record::AUTO
127  
128 =back
129  
130 =cut
131  
132 5 ▶use Class::MixinFactory -isafactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
133 5 __PACKAGE__->base_class( "DBIx::SQLEngine::Record::Base" );
134 5 __PACKAGE__->mixin_prefix( "DBIx::SQLEngine::Record" );
135 5 __PACKAGE__->mixed_prefix( "DBIx::SQLEngine::Record::AUTO" );
136  
137 ########################################################################
138  
139 =head2 Record Class Creation
140  
141 =over 4
142  
143 =item new_subclass()
144  
145 DBIx::SQLEngine::Record::Class->new_subclass( %options ) : $class_name
146  
147 Subclass constructor. Accepts a hash of options with the following keys:
148  
149 =over 4
150  
151 =item 'name'
152  
153 If you do not supply a class name, one is generated based on the table name, which must be provided.
154  
155 If the class name does not contain a "::" package separator, it is prepended
156 with DBIx::SQLEngine::Record::Auto:: to keep the namespace conflict-free.
157  
158 =item 'table'
159  
160 You may provde a DBIx::SQLEngine::Schema::Table object or create the class without it and initialize it later.
161  
162 =item 'traits'
163  
164 You may pass a reference to one or more trait names as a "traits" argument.
165  
166 =back
167  
168 =item subclass_for_table()
169  
170 DBIx::SQLEngine::Record::Class->subclass_for_table( $table, $name, @traits ) : $class_name
171  
172 Convenience method for common parameters.
173 You are expected to provde a DBIx::SQLEngine::Schema::Table object.
174  
175 =item generate_subclass_name_for_table()
176  
177 Called internally by new_subclass() if no class name is provided.
178  
179 =back
180  
181 Cross-constructors from other objects:
182  
183 =over 4
184  
185 =item SQLEngine->record_class()
186  
187 $sqldb->record_class( $tablename ) : $class_name
188 $sqldb->record_class( $tablename, $name ) : $class_name
189 $sqldb->record_class( $tablename, $name, @traits ) : $class_name
190  
191 Convenience method to create a record class with the given table name.
192  
193 =item Table->record_class()
194  
195 $table->record_class( ) : $class_name
196 $table->record_class( $name ) : $class_name
197 $table->record_class( $name, @traits ) : $class_name
198  
199 Convenience method to create a record class for a given table object.
200  
201 =back
202  
203 =cut
204  
205 5 ▶sub new_subclass {
Subroutine new_subclass: called CC=7 SCAR=20.0
Pod documented
206 5 my ( $factory, %options ) = @_;
207  
208 5 my $traits = $options{traits};
209  
210 5 my $table = $options{table};
211 5 ▶ ( ! $table ) or ( grep { $_ eq 'Table' } @$traits ) or push @$traits, 'Table';
Branch: 1 branch
BranchTrueFalseTotal
not $table or grep {$_ eq "Table";} @$traits or push @$traits, "Table" 5 0 5
Condition33%1/3
not $table or grep {$_ eq "Table";} @$traits
l!l&&r!l&&!r
050
MC/DC0%0/2
not $table or grep {$_ eq "Table";} @$traits
not $table grep {$_ eq "Table";} @$traits
Truth table33%1/3
not(A) or B
A= not $tableB= grep {$_ eq "Table";} @$traits
ABresult
1X1
011
000
212  
213 5 my $base = $factory->class( $options{traits} );
214
215 5 ▶ my $name = $options{name} || do {
Condition66%2/3
$options{'name'} || do { $table or croak("$factory new_subclass without a name requires a table"); $factory->generate_subclass_name_for_table($table) }
l!l&&r!l&&!r
410
MC/DC0%0/2
$options{'name'} || do { $table or croak("$factory new_subclass without a name requires a table"); $factory->generate_subclass_name_for_table($table) }
$options{'name'} do { $table or croak("$factory new_subclass without a name requires a table"); $factory->generate_subclass_name_for_table($table) }
Truth table66%2/3
A || B
A= $options{'name'}B= do { $table or croak("$factory new_subclass without a name requires a table"); $factory->generate_subclass_name_for_table($table) }
ABresult
1X1
011
000
216 1 ▶ $table or croak("$factory new_subclass without a name requires a table");
Branch: 1 branch
BranchTrueFalseTotal
$table or croak("$factory new_subclass without a name requires a table") 1 0 1
217 1 $factory->generate_subclass_name_for_table($table);
218 };
219 5 ▶ $name = "DBIx::SQLEngine::Record::Auto::" . $name unless ( $name =~ /::/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /::/ 1 4 5
220  
221 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
222 5 @{"$name\::ISA"} = $base;
223  
224 5 ▶ $name->table( $table ) if ( $table );
Branch: 1 branch
BranchTrueFalseTotal
if $table 5 0 5
225
226 5 return $name;
227 }
228  
229 5 ▶sub subclass_for_table {
Subroutine subclass_for_table: called CC=2 SCAR=7.0
Pod documented
230 5 my ($factory, $table, $classname, @traits) = @_;
231 5 ▶ ( grep { $_ eq 'Table' } @traits ) or push @traits, 'Table';
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq "Table";} @traits or push @traits, "Table" 0 5 5
232 5 $factory->new_subclass(
233 table => $table, name => $classname, traits => \@traits
234 )
235 }
236  
237 5 my %generated_names_for_table;
238 1 ▶sub generate_subclass_name_for_table {
Subroutine generate_subclass_name_for_table: called CC=4 SCAR=19.1
Pod documented
239 1 my ($factory, $table) = @_;
240 1 my $tname = $table->name;
241 1 my $tsqle = $table->sqlengine;
242 1 my $t_str = "$tname-$tsqle";
243  
244 1 ▶ if ( my $names = $generated_names_for_table{ $tname } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $names = $generated_names_for_table{$tname}) { } 0 1 1
245 0 ▶ return $tname if ( $names->{ $tname } eq $t_str );
Branch: 1 branch
BranchTrueFalseTotal
if $names->{$tname} eq $t_str 0 0 0
246 0 my $t_index = $tname . "_1";
247 0 ▶ until ( $names->{ $t_index } eq $t_str ) { $t_index ++ }
Branch: 1 branch
BranchTrueFalseTotal
unless ($names->{$t_index} eq $t_str) 0 0 0
248 0 return $t_index;
249 } else {
250 1 $generated_names_for_table{ $tname } = { $tname => $t_str };
251 1 return $tname;
252 }
253 }
254  
255 ########################################################################
256  
257 ########################################################################
258  
259 =head1 RECORD CLASS TRAITS
260  
261 Depending on application, there are several different sets of features that one might or might not wish to have available on their record class.
262  
263 =head2 Included Trait Classes
264  
265 The following trait classes are included with this distribution:
266  
267 =over 4
268  
269 =item Accessors
270  
271 Generates methods for getting and setting values in each record object.
272  
273 =item Cache
274  
275 Provides a caching layer to avoid repeated selections of the same information.
276  
277 =item Hooks
278  
279 Adds ok_, pre_, and post_ hooks to key methods. Any number of code refs can be registered to be called at key times, by class or for specific instances.
280  
281 =back
282  
283 =cut
284  
285 ########################################################################
286  
287 ########################################################################
288  
289 =head1 SEE ALSO
290  
291 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
292  
293 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
294 this distribution, including installation and license information.
295  
296 =cut
297  
298 ########################################################################
299  
300 5 1;
« SQLEngine/Utility/CloneWithParams.pm ↑ Summary SQLEngine/Criteria/Compound.pm »