blib/lib/DBIx/Class/RDBOHelpers.pm

filter: Statementstmt 73.2% 82 / 112 Branchbran 43.1% 19 / 44 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 76.9% 10 / 13 Podpod 100% 8 / 8 totaltotal 67.0% 122 / 182 CC 29 SCAR 26.2
CC
29
Cov
65%
has_related_pages
42.0
primary_key_uri_escaped
30.0
relationship_info
9.1
CRAP 13.7
↑ Summary
1 package DBIx::Class::RDBOHelpers;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.12';
10  
11 =head1 NAME
12  
13 DBIx::Class::RDBOHelpers - DBIC compat with Rose::DBx::Object::MoreHelpers
14  
15 =head1 SYNOPSIS
16  
17 package MyDB::Schema::Foo;
18 use strict;
19
20 use base 'DBIx::Class';
21
22 __PACKAGE__->load_components(qw( RDBOHelpers Core ));
23 # ... rest of schema setup here
24
25  
26 =head1 DESCRIPTION
27  
28 DBIx::Class::RDBOHelpers implements several methods with the same names
29 as those in Rose::DBx::Object::MoreHelpers. This class helps ease
30 compatability issues when using packages that expect these methods
31 to exist, like Rose::HTMLx::Form::Related and CatalystX::CRUD::YUI.
32 Most of these are convenience wrappers rather than implementing any
33 new features.
34  
35 =head1 METHODS
36
37 =cut
38  
39 =head2 has_related( I<rel_method_name> )
40  
41 Returns the number of related objects defined by the I<rel_method_name>
42 accessor. B<NOTE:> If the relationship is a many-to-many, this must
43 be the B<many-to-many method name>, not the relationship name.
44  
45 Just a wrapper around the B<count> method.
46  
47 Example:
48  
49 if (my $group_count = $user->has_related('groups')) {
50 printf("user %s has %d related groups\n",
51 $user->name, $group_count);
52 }
53 else {
54 printf("User %s has no related groups\n",
55 $user->name);
56 }
57  
58 =cut
59  
60 2 ▶sub has_related {
Subroutine has_related: called CC=1 SCAR=0.0
Pod documented
61 2 my $self = shift;
62 2 my $rel = shift;
63 2 my $c = $self->$rel->count;
64 2 return $c;
65 }
66  
67 =head2 has_related_pages( I<relationship_name>, I<page_size> )
68  
69 Returns the number of "pages" given I<page_size> for the count of related
70 object for I<relationship_name>. Useful for creating pagers.
71  
72 =cut
73  
74 0 ▶sub has_related_pages {
Subroutine has_related_pages: not called CC=6 SCAR=37.4
Pod documented
75 0 my $self = shift;
76 0 ▶ my $rel = shift or croak "need Relationship name";
Branch: 1 branch
BranchTrueFalseTotal
my $rel = shift() or croak("need Relationship name") 0 0 0
77 0 ▶ my $pgsize = shift or croak "need page_size";
Branch: 1 branch
BranchTrueFalseTotal
my $pgsize = shift() or croak("need page_size") 0 0 0
78 0 ▶ if ( $pgsize =~ m/\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pgsize =~ /\D/) 0 0 0
79 0 croak "page_size must be an integer";
80 }
81 0 my $n = $self->has_related($rel);
82 0 ▶ return 0 if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
83 0 ▶ if ( $n % $pgsize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % $pgsize) { } 0 0 0
84 0 return int( $n / $pgsize ) + 1;
85 }
86 else {
87 0 return $n / $pgsize;
88 }
89 }
90  
91 =head2 primary_key_uri_escaped
92  
93 Returns the primary key value, URI-escaped. If there are multiple
94 columns comprising the primary key, they are joined into a single
95 string.
96  
97 If there are no values set for any of the column(s) comprising
98 the primary key, returns 0.
99  
100 Otherwise, returns all column values joined with C<;;> as per
101 CatalystX::CRUD::Controller API.
102  
103 =cut
104  
105 0 ▶sub primary_key_uri_escaped {
Subroutine primary_key_uri_escaped: not called CC=5 SCAR=34.0
Pod documented
106 0 my $self = shift;
107 0 my $val = $self->primary_key_value;
108 0 ▶ my @vals = ref $val ? @$val : ($val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 0 0 0
109 0 my @esc;
110 0 for my $v (@vals) {
111 0 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
112 0 $v =~ s/;/\%3b/g;
113 0 push @esc, $v;
114 }
115 0 ▶ if ( !grep { length($_) } @esc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {length $_;} @esc) 0 0 0
116 0 return 0;
117 }
118 0 my $pk = join( ';;', @esc );
119 0 $pk =~ s!/!\%2f!g;
120 0 return $pk;
121 }
122  
123 =head2 primary_key_value
124  
125 Returns the value of the primary key column(s). If the
126 value is comprised of multiple column values, the return
127 value will be an array ref of values, otherwise it will
128 be a simple scalar.
129  
130 =cut
131  
132 0 ▶sub primary_key_value {
Subroutine primary_key_value: not called CC=2 SCAR=17.9
Pod documented
133 0 my $self = shift;
134 0 my @vals = map { $self->$_ } $self->primary_columns;
135 0 ▶ return scalar(@vals) > 1 ? \@vals : $vals[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @vals > 1 ? : 0 0 0
136 }
137  
138 2 __PACKAGE__->mk_classdata( ___my_m2m_metadata => {} );
139  
140 =head2 many_to_many( I<accessor_name>, I<link_rel_name>, I<foreign_rel_name> [, I<attr>] )
141  
142 Overrides the base Relationship::ManyToMany method of the same name,
143 in order to cache the name of the m2m method. Call it
144 just like you would many_to_many() as documented in DBIx::Class::Relationship.
145  
146 =cut
147  
148 3 ▶sub many_to_many {
Subroutine many_to_many: called CC=3 SCAR=11.2
Pod documented
149 3 my $class = shift;
150 3 my ( $meth_name, $rel_name, $map_to ) = @_;
151 3 my $store = $class->___my_m2m_metadata;
152 croak("many_to_many metadata for $meth_name already exists")
153 3 ▶ if exists $store->{$meth_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $store->{$meth_name} 0 3 3
154  
155 3 ▶ my $attrs = {
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 ? : 0 3 3
156 class => $class,
157 method_name => $meth_name,
158 rel_name => $rel_name, # the o2m relationship name
159 map_to => $map_to, # i.e., foreign class method name
160 ( @_ > 3 ? ( attrs => $_[3] ) : () ), # only store if exists
161 };
162  
163 # inheritable data workaround
164 3 $class->___my_m2m_metadata( { $rel_name => $attrs, %$store } );
165  
166 3 $class->next::method(@_);
167 }
168  
169 =head2 relationship_info( I<rel_name> )
170  
171 Overrides base method of the same name. Returns hash ref of information
172 about I<rel_name>, with the addition of a C<m2m> key if I<rel_name>
173 represents a many-to-many relationship.
174  
175 =cut
176  
177 10 ▶sub relationship_info {
Subroutine relationship_info: called CC=9 SCAR=22.0
Pod documented
178 10 my $self = shift;
179 10 my $rel_name = shift;
180 10 my $info = $self->next::method($rel_name);
181  
182 10 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 1 9 10
183  
184 #carp dump $self;
185  
186 # if this is a m2m relname, construct hash ref of
187 # m2m + foreign_relation info
188 10 ▶ if ( exists $class->___my_m2m_metadata->{$rel_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $class->___my_m2m_metadata->{$rel_name}) 3 7 10
189  
190 # return if we've already set it up.
191 3 ▶ return $info if exists $info->{m2m};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"m2m"} 0 3 3
192  
193 # set up
194 3 my %m2m = %{ $class->___my_m2m_metadata->{$rel_name} };
195 3 $m2m{map_class} = $info->{class};
196  
197 # find the missing map_from value
198 3 for my $map_rel ( $m2m{map_class}->relationships ) {
199 6 my $map_rel_info = $m2m{map_class}->relationship_info($map_rel);
200  
201 #warn
202 # "$rel_name : map_rel_info for class $class with map_class $m2m{map_class}"
203 # . dump $map_rel_info;
204  
205 # gah. this is broken for Catalyst because each ResultSource
206 # is blessed into a Model::Schema::$moniker class
207 # so can't compare with 'eq'. must trust isa() instead.
208  
209 #warn "class->isa $class -> $map_rel_info->{class}";
210  
211 6 ▶ if ( scalar keys %{ $map_rel_info->{cond} } > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$map_rel_info{"cond"};} > 1) 0 6 6
212 0 warn
213 "multi-key conditions for m2m relationships are not yet supported";
214 0 next;
215 }
216  
217 6 for my $foreign ( keys %{ $map_rel_info->{cond} } ) {
218 6 my $local = $map_rel_info->{cond}->{$foreign};
219 6 $local =~ s/^self\.//;
220 6 $foreign =~ s/^foreign\.//;
221  
222 6 ▶ if ( $class->isa( $map_rel_info->{class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa($map_rel_info->{'class'})) { } 4 2 6
223  
224 # because this might be a many2many related to itself,
225 # we double check whether map_from eq map_to
226 # and skip on a match
227  
228 4 ▶ if ( $map_rel eq $m2m{map_to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map_rel eq $m2m{'map_to'}) { } 1 3 4
229 1 $m2m{foreign_class} = $map_rel_info->{class};
230 1 $m2m{foreign_column} = $foreign;
231 1 $m2m{map_to_column} = $local;
232 }
233 else {
234 3 $m2m{class_column} = $foreign;
235 3 $m2m{map_from} = $map_rel;
236 3 $m2m{map_from_column} = $local;
237 }
238  
239 }
240 else {
241 2 $m2m{foreign_class} = $map_rel_info->{class};
242 2 $m2m{foreign_column} = $foreign;
243 2 $m2m{map_to_column} = $local;
244 }
245  
246 # only deal with first one defined.
247 # TODO could there be more?
248 6 last;
249 }
250  
251 }
252  
253 #carp "made m2m: " . dump \%m2m;
254  
255 # stash it away
256 3 $info->{m2m} = \%m2m;
257  
258 }
259  
260 10 return $info;
261  
262 }
263  
264 =head2 column_is_boolean( I<column_name> )
265  
266 Returns true if the column info for I<column_name> indicates it is a boolean
267 type.
268  
269 Will return false if I<column_name> is not a column or has no
270 explicit data_type or if data_type is not 'boolean'.
271  
272 =cut
273  
274 2 ▶sub column_is_boolean {
Subroutine column_is_boolean: called CC=5 SCAR=16.4
Pod documented
275 2 my $self = shift;
276 2 my $col_name = shift;
277 2 ▶ croak "column_name required" unless defined $col_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_name 0 2 2
278  
279 2 ▶ return 0 unless $self->has_column($col_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column($col_name) 0 2 2
280  
281 2 my $col_info = $self->column_info($col_name);
282 2 ▶ if ( exists $col_info->{data_type}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $col_info->{"data_type"} and $col_info->{"data_type"} eq "boolean") 1 1 2
Condition66%2/3
exists $col_info->{"data_type"} and $col_info->{"data_type"} eq "boolean"
!ll&&!rl&&r
101
MC/DC50%1/2
exists $col_info->{"data_type"} and $col_info->{"data_type"} eq "boolean"
exists $col_info->{"data_type"} $col_info->{"data_type"} eq "boolean"
Truth table66%2/3
A and B
A= exists $col_info->{"data_type"}B= $col_info->{"data_type"} eq "boolean"
ABresult
0X0
100
111
283 and $col_info->{data_type} eq 'boolean' )
284 {
285 1 return 1;
286 }
287  
288 1 return 0;
289 }
290  
291 =head2 unique_value
292  
293 Returns the first single-column unique value from the object by default.
294 This is intended for the common case where you use a serial integer as
295 the primary key but want to display a more human-friendly value
296 programmatically, like a name.
297  
298 If no unique single-column values are found, returns the primary_columns()
299 values joined with by a single space.
300  
301 =cut
302  
303 2 ▶sub unique_value {
Subroutine unique_value: called CC=5 SCAR=16.1
Pod documented
304 2 my $self = shift;
305  
306 2 my @pk = $self->primary_columns;
307 2 my %is_pk = map { $_ => 1 } @pk;
308  
309 # find the first unique single-col column of type char/varchar
310 2 for my $constraint ( $self->unique_constraint_names ) {
311 3 my @u = $self->unique_constraint_columns($constraint);
312 3 ▶ next if @u > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @u > 1 0 3 3
313 3 for my $col (@u) {
314 3 ▶ next if $is_pk{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $is_pk{$col} 2 1 3
315 1 my $method = $col;
316 1 return $self->$method;
317 }
318 }
319  
320 # couldn't find a unique column. use PK
321 1 return join( ' ', map { $self->$_ } @pk );
322  
323 }
324  
325 2 1;
326  
327 __END__
↑ Summary