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