Fey/Table/Alias.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 100% 6 / 6 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 94.7% 18 / 19 Podpod 100% 6 / 6 totaltotal 98.0% 98 / 100 SCAR 16.1
CC
5
Cov
99%
column
3.0
_build_alias_name
2.0
columns
2.0
CRAP 5.0
« Fey/Literal.pm ↑ Summary Fey/Column.pm »
1 package Fey::Table::Alias;
2  
3 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 our $VERSION = '0.43';
8  
9 25 ▶use Fey::Exceptions qw(param_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Fey::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Fey::Types qw( Column HashRef Str Table );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 25 ▶use Moose 2.1200;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use MooseX::Params::Validate 0.21 qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 25 ▶use MooseX::SemiAffordanceAccessor 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 25 ▶use MooseX::StrictConstructor 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 25 with 'Fey::Role::TableLike';
19  
20 25 has 'id' => (
21 is => 'ro',
22 lazy_build => 1,
23 init_arg => undef,
24 );
25  
26 25 has 'table' => (
27 is => 'ro',
28 isa => Table,
29 handles => [ 'schema', 'name' ],
30 );
31  
32 25 has 'alias_name' => (
33 is => 'ro',
34 isa => Str,
35 lazy_build => 1,
36 );
37  
38 has '_columns' => (
39 traits => ['Hash'],
40 is => 'bare',
41 isa => HashRef [Column],
42 7 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 25 handles => {
44 _get_column => 'get',
45 _set_column => 'set',
46 _has_column => 'exists',
47 },
48 init_arg => undef,
49 );
50  
51 25 with 'Fey::Role::Named';
52  
53 {
54 25 my %Numbers;
55  
56 sub _build_alias_name {
57 3 ▶ my $self = shift;
Subroutine _build_alias_name: called CC=2 SCAR=6.9
58  
59 3 my $name = $self->name();
60 3 ▶ $Numbers{$name} ||= 0;
Condition100%2/2
$Numbers{$name} ||= 0
l!l
12
MC/DC100%1/1
$Numbers{$name} ||= 0
$Numbers{$name}
Truth table100%2/2
A
A= $Numbers{$name}
Aresult
11
00
61  
62 3 return $name . ++$Numbers{$name};
63 }
64 }
65  
66 25 sub column {
67 8 ▶ my $self = shift;
Subroutine column: called CC=3 SCAR=11.0
Pod documented
68 8 my ($name) = pos_validated_list( \@_, { isa => 'Str' } );
69  
70 8 ▶ return $self->_get_column($name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_column($name) 3 5 8
71 if $self->_has_column($name);
72  
73 5 ▶ my $col = $self->table()->column($name)
Branch: 1 branch
BranchTrueFalseTotal
my $col = $self->table->column($name) or return 1 4 5
74 or return;
75  
76 4 my $clone = $col->_clone();
77 4 $clone->_set_table($self);
78  
79 4 $self->_set_column( $name => $clone );
80  
81 4 return $clone;
82 }
83  
84 sub columns {
85 4 ▶ my $self = shift;
Subroutine columns: called CC=2 SCAR=6.9
Pod documented
86  
87 4 ▶ my @cols = @_ ? @_ : map { $_->name() } $self->table()->columns();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 1 4
88  
89 4 return map { $self->column($_) } @cols;
90 }
91  
92 # Making this an attribute would be a hassle since we'd need to reset
93 # it whenever the associated table's keys changed.
94 sub primary_key {
95 return [
96 $_[0]->columns(
97 1 ▶ map { $_->name() } @{ $_[0]->table()->primary_key() }
Subroutine primary_key: called CC=1 SCAR=0.0
Pod documented
98 )
99 ];
100 }
101  
102 2 ▶sub is_alias {1}
Subroutine is_alias: called CC=1 SCAR=0.0
Pod documented
103  
104 sub sql_for_select_clause {
105 1 ▶ return $_[1]->quote_identifier( $_[0]->alias_name() ) . '.*';
Subroutine sql_for_select_clause: called CC=1 SCAR=0.0
Pod documented
106 }
107  
108 sub sql_with_alias {
109 0 ▶ return ( $_[1]->quote_identifier( $_[0]->table()->name() ) . ' AS '
Subroutine sql_with_alias: not called CC=1 SCAR=6.9
Pod documented
110 . $_[1]->quote_identifier( $_[0]->alias_name() ) );
111 }
112  
113 1 ▶sub _build_id { $_[0]->alias_name() }
Subroutine _build_id: called CC=1 SCAR=0.0
114  
115 25 __PACKAGE__->meta()->make_immutable();
116  
117 25 1;
118  
119 # ABSTRACT: Represents an alias for a table
120  
121 __END__
122  
123 =pod
124  
125 =head1 NAME
126  
127 Fey::Table::Alias - Represents an alias for a table
128  
129 =head1 VERSION
130  
131 version 0.43
132  
133 =head1 SYNOPSIS
134  
135 my $alias = $user_table->alias();
136  
137 my $alias = $user_table->alias( alias_name => 'User2' );
138  
139 =head1 DESCRIPTION
140  
141 This class represents an alias for a table. Table aliases allow you to
142 join the same table more than once in a query, which makes certain
143 types of queries simpler to express.
144  
145 =head1 METHODS
146  
147 This class provides the following methods:
148  
149 =head2 Fey::Table::Alias->new()
150  
151 This method constructs a new C<Fey::Table::Alias> object. It takes the
152 following parameters:
153  
154 =over 4
155  
156 =item * table - required
157  
158 This is the C<Fey::Table> object which we are aliasing.
159  
160 =item * alias_name - optional
161  
162 This should be a valid table name for your DBMS. If not provided, a
163 unique name is automatically created.
164  
165 =back
166  
167 =head2 $alias->table()
168  
169 Returns the C<Fey::Table> object for which this object is an alias.
170  
171 =head2 $alias->alias_name()
172  
173 Returns the name for this alias.
174  
175 =head2 $alias->name()
176  
177 =head2 $alias->schema()
178  
179 These methods work like the corresponding methods in
180 C<Fey::Table>. The C<name()> method returns the real table name.
181  
182 =head2 $alias->column($name)
183  
184 =head2 $alias->columns()
185  
186 =head2 $alias->columns(@names)
187  
188 =head2 $alias->primary_key()
189  
190 These methods work like the corresponding methods in
191 C<Fey::Table>. However, the columns they return will return the alias
192 object when C<< $column->table() >> is called.
193  
194 =head2 $alias->is_alias()
195  
196 Always returns true.
197  
198 =head2 $alias->sql_with_alias()
199  
200 =head2 $table->sql_for_select_clause()
201  
202 Returns the appropriate SQL snippet for the alias.
203  
204 =head2 $alias->id()
205  
206 Returns a unique string identifying the alias.
207  
208 =head1 ROLES
209  
210 This class does the L<Fey::Role::TableLike> and L<Fey::Role::Named>
211 roles.
212  
213 =head1 BUGS
214  
215 See L<Fey> for details on how to report bugs.
216  
217 =head1 AUTHOR
218  
219 Dave Rolsky <autarch@urth.org>
220  
221 =head1 COPYRIGHT AND LICENSE
222  
223 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
224  
225 This is free software, licensed under:
226  
227 The Artistic License 2.0 (GPL Compatible)
228  
229 =cut
« Fey/Literal.pm ↑ Summary Fey/Column.pm »