DBI/ColumnGrouper.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 75.0% 15 / 20 Conditioncond 76.4% 13 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 17 / 17 Podpod 100% 11 / 11 totaltotal 86.3% 114 / 132 CC 18 SCAR 12.9
CC
18
Cov
87%
add_group
8.0
primary
4.5
add_column
3.1
CRAP 3.6
« DBI/Test/SQLite.pm ↑ Summary DBI/Iterator.pm »
1 package Class::DBI::ColumnGrouper;
2  
3 =head1 NAME
4  
5 Class::DBI::ColumnGrouper - Columns and Column Groups
6  
7 =head1 SYNOPSIS
8  
9 my $colg = Class::DBI::ColumnGrouper->new;
10 $colg->add_group(People => qw/star director producer/);
11  
12 my @cols = $colg->group_cols($group);
13  
14 my @all = $colg->all_columns;
15 my @pri_col = $colg->primary;
16 my @essential_cols = $colg->essential;
17  
18 =head1 DESCRIPTION
19  
20 Each Class::DBI class maintains a list of its columns as class data.
21 This provides an interface to that. You probably don't want to be dealing
22 with this directly.
23  
24 =head1 METHODS
25  
26 =cut
27  
28 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use Class::DBI::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶sub _unique {
Subroutine _unique: called CC=2 SCAR=6.9
35 1 my %seen;
36 1 ▶ map { $seen{$_}++ ? () : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$seen{$_}++ ? : 1 2 3
37 }
38  
39 12 ▶sub _uniq {
Subroutine _uniq: called CC=1 SCAR=0.0
40 12 my %tmp;
41 12 return grep !$tmp{$_}++, @_;
42 }
43  
44 =head2 new
45  
46 my $colg = Class::DBI::ColumnGrouper->new;
47  
48 A new blank ColumnnGrouper object.
49  
50 =head2 clone
51  
52 my $colg2 = $colg->clone;
53  
54 Clone an existing ColumnGrouper.
55  
56 =cut
57  
58 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
59 4 my $class = shift;
60 4 bless {
61 _groups => {},
62 _cols => {},
63 }, $class;
64 }
65  
66 15 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
67 15 my ($class, $prev) = @_;
68 15 return dclone $prev;
69 }
70  
71 =head2 add_column / find_column
72  
73 $colg->add_column($name);
74 my Class::DBI::Column $col = $colg->find_column($name);
75  
76 Add or return a Column object for the given column name.
77  
78 =cut
79  
80 36 ▶sub add_column {
Subroutine add_column: called CC=3 SCAR=11.4
Pod documented
81 36 my ($self, $col) = @_;
82  
83 # TODO remove this
84 36 ▶ croak "Need a Column, got $col" unless $col->isa("Class::DBI::Column");
Branch: 1 branch
BranchTrueFalseTotal
unless $col->isa("Class::DBI::Column") 0 36 36
85 36 ▶ $self->{_allcol}->{ $col->name_lc } ||= $col;
Condition66%2/3
$self->{"_allcol"}{$col->name_lc} ||= $col
l!l&&r!l&&!r
9270
MC/DC0%0/2
$self->{"_allcol"}{$col->name_lc} ||= $col
$self->{"_allcol"}{$col->name_lc} $col
Truth table66%2/3
A ||= B
A= $self->{"_allcol"}{$col->name_lc}B= $col
ABresult
1X1
011
000
86 }
87  
88 8 ▶sub find_column {
Subroutine find_column: called CC=3 SCAR=11.1
Pod documented
89 8 my ($self, $name) = @_;
90 8 ▶ return $name if ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 8 8
91 8 ▶ return unless $self->{_allcol}->{ lc $name };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_allcol"}{lc $name} 1 7 8
92 }
93  
94 =head2 add_group
95  
96 $colg->add_group(People => qw/star director producer/);
97  
98 This adds a list of columns as a column group.
99  
100 =cut
101  
102 19 ▶sub add_group {
Subroutine add_group: called CC=8 SCAR=20.8
Pod documented
103 19 my ($self, $group, @names) = @_;
104 19 ▶ $self->add_group(Primary => $names[0])
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "All" || $group eq "Essential" and not $self->group_cols("Primary") 4 15 19
Condition100%6/6
$group eq "All" || $group eq "Essential" and not $self->group_cols("Primary")
!ll&&!rl&&r
1144
$group eq "All" or $group eq "Essential"
l!l&&r!l&&!r
7111
MC/DC100%3/3
$group eq "All" || $group eq "Essential" and not $self->group_cols("Primary")
$group eq "All" $group eq "Essential" not $self->group_cols("Primary")
Truth table80%4/5
A or B and not(C)
A= $group eq "All"B= $group eq "Essential"C= not $self->group_cols("Primary")
ABCresult
00X0
1X00
0100
1X11
0111
105 if ($group eq "All" or $group eq "Essential")
106 and not $self->group_cols('Primary');
107 19 ▶ $self->add_group(Essential => @names)
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "All" and not $self->essential 0 19 19
Condition66%2/3
$group eq "All" and not $self->essential
!ll&&!rl&&r
1270
MC/DC0%0/2
$group eq "All" and not $self->essential
$group eq "All" not $self->essential
Truth table66%2/3
A and not(B)
A= $group eq "All"B= not $self->essential
ABresult
0X0
100
111
108 if $group eq "All"
109 and !$self->essential;
110 19 ▶ @names = _unique($self->primary, @names) if $group eq "Essential";
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "Essential" 1 18 19
111  
112 19 my @cols = map $self->add_column($_), @names;
113 19 $_->add_group($group) foreach @cols;
114 19 $self->{_groups}->{$group} = \@cols;
115 19 return $self;
116 }
117  
118 =head2 group_cols / groups_for
119  
120 my @colg = $cols->group_cols($group);
121 my @groups = $cols->groups_for(@cols);
122  
123 This returns a list of all columns which are in the given group, or the
124 groups a given column is in.
125  
126 =cut
127  
128 44 ▶sub group_cols {
Subroutine group_cols: called CC=3 SCAR=11.0
Pod documented
129 44 my ($self, $group) = @_;
130 44 ▶ return $self->all_columns if $group eq "All";
Branch: 1 branch
BranchTrueFalseTotal
if $group eq "All" 0 44 44
131 44 ▶ @{ $self->{_groups}->{$group} || [] };
Condition100%2/2
$self->{'_groups'}{$group} || []
l!l
2915
MC/DC100%1/1
$self->{'_groups'}{$group} || []
$self->{'_groups'}{$group}
Truth table100%2/2
A
A= $self->{'_groups'}{$group}
Aresult
11
00
132 }
133  
134 2 ▶sub groups_for {
Subroutine groups_for: called CC=1 SCAR=0.0
Pod documented
135 2 my ($self, @cols) = @_;
136 2 return _uniq(map $_->groups, @cols);
137 }
138  
139 =head2 columns_in
140  
141 my @cols = $colg->columns_in(@groups);
142  
143 This returns a list of all columns which are in the given groups.
144  
145 =cut
146  
147 11 ▶sub columns_in {
Subroutine columns_in: called CC=1 SCAR=0.0
Pod documented
148 11 my ($self, @groups) = @_;
149 11 return _uniq(map $self->group_cols($_), @groups);
150 }
151  
152 =head2 all_columns
153  
154 my @all = $colg->all_columns;
155  
156 This returns a list of all the real columns.
157  
158 =head2 primary
159  
160 my $pri_col = $colg->primary;
161  
162 This returns a list of the columns in the Primary group.
163  
164 =head2 essential
165  
166 my @essential_cols = $colg->essential;
167  
168 This returns a list of the columns in the Essential group.
169  
170 =cut
171  
172 10 ▶sub all_columns {
Subroutine all_columns: called CC=1 SCAR=0.0
Pod documented
173 10 my $self = shift;
174 10 return grep $_->in_database, values %{ $self->{_allcol} };
175 }
176  
177 24 ▶sub primary {
Subroutine primary: called CC=3 SCAR=15.0
Pod documented
178 24 my @cols = shift->group_cols('Primary');
179 24 ▶ if (!wantarray && @cols > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray and @cols > 1) 0 24 24
Condition33%1/3
not wantarray and @cols > 1
!ll&&!rl&&r
2400
MC/DC0%0/2
not wantarray and @cols > 1
not wantarray @cols > 1
Truth table33%1/3
not(A) and B
A= not wantarrayB= @cols > 1
ABresult
0X0
100
111
180 0 local ($Carp::CarpLevel) = 1;
181 0 confess(
182 "Multiple columns in Primary group (@cols) but primary called in scalar context"
183 );
184 0 return $cols[0];
185 }
186 24 return @cols;
187 }
188  
189 11 ▶sub essential {
Subroutine essential: called CC=2 SCAR=6.9
Pod documented
190 11 my $self = shift;
191 11 my @cols = $self->columns_in('Essential');
192 11 ▶ @cols = $self->primary unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 9 2 11
193 11 return @cols;
194 }
195  
196 4 1;
« DBI/Test/SQLite.pm ↑ Summary DBI/Iterator.pm »