Fey/Column/Alias.pm

filter: Statementstmt 100% 56 / 56 Branchbran 100% 2 / 2 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 100% 81 / 81 CC 3 SCAR 2.0
CC
3
Cov
100%
_build_alias_name
2.0
_build_id
2.0
BEGIN
1.0
CRAP 1.2
« Fey/Literal/Function.pm ↑ Summary Fey/Role/IsLiteral.pm »
1 package Fey::Column::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.42';
8  
9 25 ▶use Fey::Exceptions qw( object_state_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::Table::Alias;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Fey::Types qw( Column Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 25 ▶use Moose 0.90;
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.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 25 with 'Fey::Role::ColumnLike';
19  
20 25 has 'id' => (
21 is => 'ro',
22 lazy_build => 1,
23 init_arg => undef,
24 clearer => '_clear_id',
25 );
26  
27 25 has 'column' => (
28 is => 'ro',
29 isa => Column,
30 handles => [
31 qw( name type generic_type length precision
32 is_auto_increment is_nullable table )
33 ],
34 );
35  
36 25 has 'alias_name' => (
37 is => 'ro',
38 isa => Str,
39 lazy_build => 1,
40 );
41  
42 25 with 'Fey::Role::Named';
43  
44 {
45 25 my %Numbers;
46  
47 sub _build_alias_name {
48 2 ▶ my $self = shift;
Subroutine _build_alias_name: called CC=2 SCAR=6.9
49  
50 2 my $name = $self->name();
51 2 ▶ $Numbers{$name} ||= 0;
Condition100%2/2
$Numbers{$name} ||= 0
l!l
11
MC/DC100%1/1
$Numbers{$name} ||= 0
$Numbers{$name}
Truth table100%2/2
A
A= $Numbers{$name}
Aresult
11
00
52  
53 2 return $name . ++$Numbers{$name};
54 }
55 }
56  
57 1 ▶sub is_alias {1}
Subroutine is_alias: called CC=1 SCAR=0.0
Pod documented
58  
59 5 ▶sub sql { $_[1]->quote_identifier( $_[0]->alias_name() ) }
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
60  
61 sub sql_with_alias {
62 1 ▶ my $sql = $_[1]->quote_identifier(
Subroutine sql_with_alias: called CC=1 SCAR=0.0
Pod documented
63 undef,
64 $_[0]->_containing_table_name_or_alias(),
65 $_[0]->name(),
66 );
67  
68 1 $sql .= ' AS ';
69 1 $sql .= $_[1]->quote_identifier( $_[0]->alias_name() );
70  
71 1 return $sql;
72 }
73  
74 4 ▶sub sql_or_alias { goto &sql }
Subroutine sql_or_alias: called CC=1 SCAR=0.0
Pod documented
75  
76 sub _build_id {
77 2 ▶ my $self = shift;
Subroutine _build_id: called CC=2 SCAR=6.9
78  
79 2 my $table = $self->table();
80  
81 2 ▶ object_state_error
Branch: 1 branch
BranchTrueFalseTotal
unless $table 1 1 2
82 'The id attribute cannot be determined for a column object which has no table.'
83 unless $table;
84  
85 1 return $table->id() . '.' . $self->alias_name();
86 }
87  
88 25 __PACKAGE__->meta()->make_immutable();
89  
90 # This is here to avoid a circular use issue.
91 25 require Fey::Column;
92  
93 25 1;
94  
95 # ABSTRACT: Represents an alias for a column
96  
97 __END__
98  
99 =pod
100  
101 =head1 NAME
102  
103 Fey::Column::Alias - Represents an alias for a column
104  
105 =head1 VERSION
106  
107 version 0.42
108  
109 =head1 SYNOPSIS
110  
111 my $alias = $user_id_col->alias();
112  
113 =head1 DESCRIPTION
114  
115 This class represents an alias for a column. Column aliases allow you
116 to use the same column in different ways multiple times in a query,
117 which makes certain types of queries simpler to express.
118  
119 =head1 METHODS
120  
121 =head2 Fey::Column::Alias->new()
122  
123 This method constructs a new C<Fey::Column::Alias> object. It takes
124 the following parameters:
125  
126 =over 4
127  
128 =item * column - required
129  
130 This is the C<Fey::Column> object which we are aliasing.
131  
132 =item * alias_name - optional
133  
134 This should be a valid column name for your DBMS. If not provided, a
135 unique name is automatically created.
136  
137 =back
138  
139 =head2 $alias->name()
140  
141 This returns the name of the column for which this object is an alias.
142  
143 =head2 $alias->alias_name()
144  
145 Returns the name for this alias.
146  
147 =head2 $alias->type()
148  
149 =head2 $alias->generic_type()
150  
151 =head2 $alias->length()
152  
153 =head2 $alias->precision()
154  
155 =head2 $alias->is_auto_increment()
156  
157 =head2 $alias->is_nullable()
158  
159 =head2 $alias->default()
160  
161 Returns the specified attribute for the column, just like the
162 C<Fey::Column> methods of the same name.
163  
164 =head2 $alias->table()
165  
166 Returns the C<Fey::Table> object to which the column alias belongs, if
167 any.
168  
169 =head2 $alias->is_alias()
170  
171 Always returns false.
172  
173 =head2 $alias->sql()
174  
175 =head2 $alias->sql_with_alias()
176  
177 =head2 $alias->sql_or_alias()
178  
179 Returns the appropriate SQL snippet for the alias.
180  
181 =head2 $alias->id()
182  
183 Returns a unique identifier for the column. This method throws an
184 exception if the alias does not belong to a table.
185  
186 =head1 ROLES
187  
188 This class does the L<Fey::Role::ColumnLike> and L<Fey::Role::Named>
189 roles.
190  
191 =head1 BUGS
192  
193 See L<Fey> for details on how to report bugs.
194  
195 =head1 AUTHOR
196  
197 Dave Rolsky <autarch@urth.org>
198  
199 =head1 COPYRIGHT AND LICENSE
200  
201 This software is Copyright (c) 2011 - 2015 by Dave Rolsky.
202  
203 This is free software, licensed under:
204  
205 The Artistic License 2.0 (GPL Compatible)
206  
207 =cut
« Fey/Literal/Function.pm ↑ Summary Fey/Role/IsLiteral.pm »