Snowflake.pm

filter: Statementstmt 98.7% 158 / 160 Branchbran 92.1% 59 / 64 Conditioncond 83.3% 25 / 30 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 93.7% 272 / 290 CC 47 SCAR 20.5
CC
47
Cov
95%
_resolve_dimension_to_attribute
17.2
_make_ignore_hash
7.0
attributes
6.0
CRAP 7.8
↑ Summary Snowflake/Fact.pm »
1 package DBIx::Class::Snowflake;
2 4 our $VERSION = '0.10';
3  
4  
5 =head1 NAME
6  
7 DBIx::Class::Snowflake
8  
9 =head1 VERSION
10  
11 version .04
12  
13 =cut
14  
15 # ABSTRACT: Easily use DBIC with snowflake schemas.
16  
17 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use base 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 my %ignores;
24  
25 =head2 ignore_columns
26  
27 B<ignore_columns> sets the columnns to be ignored. Please see the manual
28 L<DBIx::Class::Snowflake::Manual> for further explanation.
29  
30 =cut
31 sub ignore_columns
32 396 ▶{
Subroutine ignore_columns: called CC=3 SCAR=11.0
Pod documented
33 396 my ( $self, $class ) = _normalize_location_for_ignore_columns(shift);
34 396 my $ic = shift;
35  
36 396 ▶ if ( not defined( $ignores{$class} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ignores{$class}) 21 375 396
37 {
38 21 $ignores{$class} = {};
39 }
40  
41 396 ▶ if ( ref($self) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 16 380 396
42 {
43 16 $self->{'_ignore_columns'} = $self->_make_ignore_hash($ic);
44 16 return { %{ $self->{'_ignore_columns'} }, %{ $ignores{$class} } };
45 }
46 else
47 {
48 380 $ignores{$class} = $self->_make_ignore_hash($ic);
49 380 return $ignores{$class};
50 }
51  
52 }
53  
54 sub _normalize_location_for_ignore_columns
55 607 ▶{
Subroutine _normalize_location_for_ignore_columns: called CC=3 SCAR=11.0
56 607 my $self = shift;
57 607 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
2393680
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
58 607 ▶ if ( $class =~ /ResultSet/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /ResultSet/) 180 427 607
59 {
60 180 $self = $self->result_class;
61 180 $class = $self;
62 }
63 607 return ( $self, $class );
64 }
65  
66 =head2 get_ignore_columns
67  
68 Returns the ignored columns in the internal representation without modifying
69 them. Please see L<DBIx::Class::Snowflake::Manual> for more information.
70  
71 =cut
72 sub get_ignore_columns
73 211 ▶{
Subroutine get_ignore_columns: called CC=4 SCAR=13.9
Pod documented
74 211 my ( $self, $class ) = _normalize_location_for_ignore_columns(shift);
75 211 my $ic = shift;
76  
77 211 ▶ if ( not defined( $ignores{$class} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ignores{$class}) 0 211 211
78 {
79 0 $ignores{$class} = {};
80 }
81  
82 211 ▶ if ( ref($self) and ref( $self->{'_ignore_columns'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and ref $self->{'_ignore_columns'}) { } 24 187 211
Condition100%3/3
ref $self and ref $self->{"_ignore_columns"}
!ll&&!rl&&r
1681924
MC/DC100%2/2
ref $self and ref $self->{"_ignore_columns"}
ref $self ref $self->{"_ignore_columns"}
Truth table100%3/3
A and B
A= ref $selfB= ref $self->{"_ignore_columns"}
ABresult
0X0
100
111
83 {
84 24 return { %{ $self->{'_ignore_columns'} }, %{ $ignores{$class} } };
85 }
86 else
87 {
88 187 return $ignores{$class};
89 }
90 }
91  
92 sub _make_ignore_hash
93 410 ▶{
Subroutine _make_ignore_hash: called CC=7 SCAR=19.5
94 410 my $self = shift;
95 410 my $ignore = shift;
96 410 my %list;
97 my $ref;
98  
99 410 $ref = ref($ignore);
100  
101 410 ▶ if ( defined $ignore )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ignore) 40 370 410
102 {
103 40 ▶ if ( not $ref )
Branch: 5 branches
BranchTrueFalseTotal
if (not $ref) { } 30 10 40
elsif ($ref eq 'ARRAY') { } 2 8 10
elsif ($ref eq 'HASH') { } 2 6 8
elsif ($ref eq 'SCALAR') { } 2 4 6
elsif ($ref eq 'CODE') { } 2 2 4
104 {
105 30 $list{$ignore} = 1;
106 }
107 elsif ( $ref eq 'ARRAY' )
108 {
109 2 %list = map( { $_ => 1 } @$ignore );
110 }
111 elsif ( $ref eq 'HASH' )
112 {
113 2 %list = %$ignore;
114 }
115 elsif ( $ref eq 'SCALAR' )
116 {
117 2 $list{$$ignore} = 1;
118 }
119 elsif ( $ref eq 'CODE' )
120 {
121 2 %list = %{ $self->_make_ignore_hash( &$ignore() ) };
122 }
123 else
124 {
125 2 $self->throw_exception( "Unable to determine what columns to ignore, I don't know what to do with a '$ref'."
126 );
127 }
128 }
129 408 return \%list;
130 }
131  
132 =head2 _resolve_metrics
133  
134 The B<resolve_values> subroutine will recursively walk through the dimensions
135 of a fact until it finds the column for the values we are looking for. It
136 will then return an array that indicates the steps to take in the SQL query
137 to get to that dimension.
138  
139 For example, assume there is a fact_produced table and it had a dim_date table with
140 a column for the day of the week (dow) and dim_date is a dimension of fact_produced.
141 This call:
142 $produced->_resolve_metrics('dow');
143 would result in a reference to an array like so:
144 [ fact_produced, dim_date, dow ]
145  
146 =cut
147  
148 sub _resolve_metrics
149 80 ▶{
Subroutine _resolve_metrics: called CC=4 SCAR=13.9
150 80 my $self = shift;
151 80 my $metric = shift;
152 80 my $relationship = shift;
153 80 my @stack;
154 80 my $ignores = $self->get_ignore_columns();
155 80 my ( $needed_prefix, $needed_column ) = split( /\./, $metric );
156 80 my $prefix = $self->_get_result_source->from();
157 80 ▶ if ( not defined $needed_column )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $needed_column) 20 60 80
158 {
159 20 $needed_column = $needed_prefix;
160 20 $needed_prefix = undef;
161 }
162  
163 80 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_stop_search($needed_prefix, $needed_column, $prefix, $ignores)) { } 20 60 80
164 $self->_stop_search(
165 $needed_prefix, $needed_column, $prefix, $ignores
166 )
167 )
168 {
169  
170 20 ▶ $metric = "$relationship.$needed_column" if( defined $relationship );
Branch: 1 branch
BranchTrueFalseTotal
if defined $relationship 18 2 20
171 20 unshift( @stack, $metric );
172 20 return \@stack;
173 }
174 else
175 {
176 60 return $self->_keep_looking( $metric, $ignores );
177 }
178 }
179  
180 sub _get_rel
181 332 ▶{
Subroutine _get_rel: called CC=2 SCAR=6.9
182 332 my $self = shift;
183 332 my $source = shift;
184 332 my $column = shift;
185  
186 332 ▶ if ( not $source->has_relationship($column) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($source->has_relationship($column)) 246 86 332
187 {
188 246 return undef;
189 }
190  
191 86 return $source->relationship_info($column)->{'source'};
192 }
193  
194 sub _keep_looking
195 60 ▶{
Subroutine _keep_looking: called CC=5 SCAR=16.1
196 60 my $self = shift;
197 60 my $metric = shift;
198 60 my $ignores = shift;
199  
200 60 my $stack;
201 60 my $source = $self->_get_result_source;
202 60 my $rel_source;
203  
204 60 foreach my $column ( $source->columns )
205 {
206 190 ▶ if ( not $ignores->{$column} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignores->{$column}) 184 6 190
207 {
208 184 ▶ if ( $rel_source = $self->_get_rel( $source, $column ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_source = $self->_get_rel($source, $column)) 50 134 184
209 {
210 50 $stack =
211 $self->result_source->schema->resultset($rel_source)
212 ->_resolve_metrics( $metric, $column );
213 50 ▶ if ( defined $stack )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stack) 24 26 50
214 {
215 24 unshift( @$stack, $column );
216 24 last;
217 }
218 }
219 }
220 }
221 60 return $stack;
222 }
223  
224 sub _stop_search
225 138 ▶{
Subroutine _stop_search: called CC=4 SCAR=13.9
226 138 my $self = shift;
227 138 my $needed_prefix = shift;
228 138 my $needed_column = shift;
229 138 my $prefix = shift;
230 138 my $ignores = shift;
231 138 my %columns = $self->_columns_as_hash();
232 return (
233 (
234 not defined $needed_prefix
235 or $needed_prefix eq $prefix
236 )
237 and $columns{$needed_column}
238 138 ▶ and not $ignores->{$needed_column}
Condition100%9/9
(!defined($needed_prefix) || $needed_prefix eq $prefix) && $columns{$needed_column} && !$ignores->{$needed_column}
!ll&&!rl&&r
98634
!defined($needed_prefix) || $needed_prefix eq $prefix and $columns{$needed_column}
!ll&&!rl&&r
722640
not defined $needed_prefix or $needed_prefix eq $prefix
l!l&&r!l&&!r
363072
MC/DC100%4/4
(!defined($needed_prefix) || $needed_prefix eq $prefix) && $columns{$needed_column} && !$ignores->{$needed_column}
not defined $needed_prefix $needed_prefix eq $prefix $columns{$needed_column} !$ignores->{$needed_column}
Truth table85%6/7
not(A) or B and C && not(D)
A= not defined $needed_prefixB= $needed_prefix eq $prefixC= $columns{$needed_column}D= !$ignores->{$needed_column}
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
239 );
240 }
241  
242 =pod
243  
244 This function recursively calls itself until it finds
245  
246 =cut
247  
248 sub _resolve_dimension_to_attribute
249 58 ▶{
Subroutine _resolve_dimension_to_attribute: called CC=17 SCAR=28.5
250 58 my $self = shift;
251 58 my $attribute = shift;
252 58 my $value = shift;
253 58 ▶ my $depth = shift || 0;
Condition100%2/2
shift() || 0
l!l
3622
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
254 58 ▶ my $relation = shift || '';
Condition100%2/2
shift() || ''
l!l
3622
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
255 58 my ( %columns, @ret_values, $ret_values, $column );
256 58 my $ignores = $self->get_ignore_columns();
257 58 my $source = $self->_get_result_source();
258 58 my $pk = ( $source->primary_columns() )[0];
259 58 my ( $needed_prefix, $needed_column ) = split( /\./, $attribute );
260 58 my $prefix = $source->from();
261 58 my $rel_source;
262  
263 58 ▶ if ( not defined $needed_column )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $needed_column) 16 42 58
264 {
265 16 $needed_column = $needed_prefix;
266 16 $needed_prefix = undef;
267 }
268  
269 58 %columns = $self->_columns_as_hash();
270  
271 58 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_stop_search($needed_prefix, $needed_column, $prefix, $ignores)) 14 44 58
272 $self->_stop_search(
273 $needed_prefix, $needed_column, $prefix, $ignores
274 )
275 )
276 {
277 14 @ret_values = $self->result_source->resultset->search(
278 { $needed_column => $value } )->get_column($pk)->all();
279 14 ▶ if ( $depth == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) { } 4 10 14
280 {
281 4 return { 'me.' . $needed_column => \@ret_values };
282 }
283 else
284 {
285 10 return \@ret_values;
286 }
287 }
288  
289 44 ▶ if ( not @ret_values )
Branch: 1 branch
BranchTrueFalseTotal
unless (@ret_values) 44 0 44
290 {
291 44 foreach $column ( $source->columns )
292 {
293 154 ▶ if ( not $ignores->{$column} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignores->{$column}) 148 6 154
294 {
295 148 ▶ if ( $rel_source = $self->_get_rel( $source, $column ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_source = $self->_get_rel($source, $column)) 36 112 148
296 {
297 36 $ret_values =
298 $self->result_source->schema->resultset($rel_source)
299 ->_resolve_dimension_to_attribute( $attribute, $value,
300 $depth + 1, $column );
301 }
302 }
303  
304 154 ▶ if ( $depth != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($depth != 0) { } 92 62 154
305 {
306 92 ▶ if ( ref($ret_values) eq 'ARRAY' and @$ret_values )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret_values eq "ARRAY" and @$ret_values) 4 88 92
Condition66%2/3
ref $ret_values eq "ARRAY" and @$ret_values
!ll&&!rl&&r
8804
MC/DC50%1/2
ref $ret_values eq "ARRAY" and @$ret_values
ref $ret_values eq "ARRAY" @$ret_values
Truth table66%2/3
A and B
A= ref $ret_values eq "ARRAY"B= @$ret_values
ABresult
0X0
100
111
307 {
308 4 @ret_values = $source->resultset->search(
309 { $column => { 'in' => \@$ret_values } } )
310 ->get_column($pk)->all();
311  
312 4 return \@ret_values;
313 }
314 }
315 else
316 {
317 62 ▶ if ( ref($ret_values) eq 'ARRAY' and @$ret_values )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret_values eq "ARRAY" and @$ret_values) 10 52 62
Condition66%2/3
ref $ret_values eq "ARRAY" and @$ret_values
!ll&&!rl&&r
52010
MC/DC50%1/2
ref $ret_values eq "ARRAY" and @$ret_values
ref $ret_values eq "ARRAY" @$ret_values
Truth table66%2/3
A and B
A= ref $ret_values eq "ARRAY"B= @$ret_values
ABresult
0X0
100
111
318 {
319 10 return { 'me.' . $column => $ret_values };
320 }
321 }
322 140 ▶ last if ( ref($ret_values) eq 'ARRAY' and @$ret_values );
Branch: 1 branch
BranchTrueFalseTotal
if ref $ret_values eq "ARRAY" and @$ret_values 0 140 140
Condition33%1/3
ref $ret_values eq "ARRAY" and @$ret_values
!ll&&!rl&&r
14000
MC/DC0%0/2
ref $ret_values eq "ARRAY" and @$ret_values
ref $ret_values eq "ARRAY" @$ret_values
Truth table33%1/3
A and B
A= ref $ret_values eq "ARRAY"B= @$ret_values
ABresult
0X0
100
111
323 }
324 }
325  
326 30 return undef;
327 }
328  
329 =head2 attributes
330  
331 Goes through all of the columns, determines if they are to be ignored
332 if not then it returns them in a large array containing the accessor
333 and data type of each column.
334  
335 =cut
336  
337 sub attributes
338 73 ▶{
Subroutine attributes: called CC=6 SCAR=17.9
Pod documented
339 73 my $self = shift;
340 73 my $relation = shift;
341 73 ▶ my $burnt = shift || {};
Condition100%2/2
shift() || {}
l!l
4033
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
342  
343 73 $burnt->{ ref($self) } = 1;
344  
345 73 my ( @dimensions, $attrs, $ignore_hash, $info, $source, $rel_info );
346  
347 73 $ignore_hash = $self->get_ignore_columns();
348  
349 73 $source = $self->_get_result_source();
350  
351 73 foreach my $dimension ( $source->columns() )
352 {
353 267 ▶ if ( not $ignore_hash->{$dimension} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_hash->{$dimension}) 249 18 267
354 {
355 249 ▶ if ( $source->has_relationship($dimension) )
Branch: 1 branch
BranchTrueFalseTotal
if ($source->has_relationship($dimension)) { } 40 209 249
356 {
357 40 $rel_info = $source->relationship_info($dimension);
358 40 ▶ if ( not exists( $burnt->{ $rel_info->{'source'} } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$burnt{$rel_info->{"source"}}) 40 0 40
359 {
360 $attrs =
361 $self->result_source->schema->resultset(
362 40 $rel_info->{'source'} )
363 ->attributes( $dimension, $burnt );
364 40 push( @dimensions, @$attrs );
365 }
366 }
367 else
368 {
369 209 $info = $source->column_info($dimension);
370 209 $dimension = $source->from() . '.' . $dimension;
371 push(
372 @dimensions,
373 {
374 'name' => $dimension,
375 209 'type' => $info->{'data_type'}
376 }
377 );
378 }
379 }
380 }
381 73 return \@dimensions;
382 }
383  
384 sub _get_result_source
385 487 ▶{
Subroutine _get_result_source: called CC=2 SCAR=7.2
386 487 my $self = shift;
387 487 my $source;
388 487 ▶ if ( $self->can('result_source') )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('result_source')) { } 487 0 487
389 {
390 487 $source = $self->result_source;
391 }
392 else
393 {
394 0 $source = $self;
395 }
396 487 return $source;
397 }
398  
399 sub _columns_as_hash
400 216 ▶{
Subroutine _columns_as_hash: called CC=1 SCAR=0.0
401 216 my $self = shift;
402 216 my $source = $self->_get_result_source();
403 216 return map( { $_ => 1 } $source->columns() );
404 }
405 4 1;
↑ Summary Snowflake/Fact.pm »