line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Fey::Table::Alias; |
2
|
|
|
|
|
|
|
|
3
|
26
|
|
|
26
|
|
137
|
use strict; |
|
26
|
|
|
|
|
47
|
|
|
26
|
|
|
|
|
1166
|
|
4
|
26
|
|
|
26
|
|
134
|
use warnings; |
|
26
|
|
|
|
|
44
|
|
|
26
|
|
|
|
|
837
|
|
5
|
26
|
|
|
26
|
|
129
|
use namespace::autoclean; |
|
26
|
|
|
|
|
42
|
|
|
26
|
|
|
|
|
289
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.43'; |
8
|
|
|
|
|
|
|
|
9
|
26
|
|
|
26
|
|
3007
|
use Fey::Exceptions qw(param_error); |
|
26
|
|
|
|
|
53
|
|
|
26
|
|
|
|
|
1715
|
|
10
|
26
|
|
|
26
|
|
133
|
use Fey::Table; |
|
26
|
|
|
|
|
53
|
|
|
26
|
|
|
|
|
559
|
|
11
|
26
|
|
|
26
|
|
628
|
use Fey::Types qw( Column HashRef Str Table ); |
|
26
|
|
|
|
|
50
|
|
|
26
|
|
|
|
|
229
|
|
12
|
|
|
|
|
|
|
|
13
|
26
|
|
|
26
|
|
329242
|
use Moose 2.1200; |
|
26
|
|
|
|
|
877
|
|
|
26
|
|
|
|
|
236
|
|
14
|
26
|
|
|
26
|
|
165084
|
use MooseX::Params::Validate 0.21 qw( pos_validated_list ); |
|
26
|
|
|
|
|
65235
|
|
|
26
|
|
|
|
|
207
|
|
15
|
26
|
|
|
26
|
|
6063
|
use MooseX::SemiAffordanceAccessor 0.03; |
|
26
|
|
|
|
|
10465
|
|
|
26
|
|
|
|
|
191
|
|
16
|
26
|
|
|
26
|
|
93840
|
use MooseX::StrictConstructor 0.13; |
|
26
|
|
|
|
|
14114
|
|
|
26
|
|
|
|
|
185
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
with 'Fey::Role::TableLike'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'id' => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
lazy_build => 1, |
23
|
|
|
|
|
|
|
init_arg => undef, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has 'table' => ( |
27
|
|
|
|
|
|
|
is => 'ro', |
28
|
|
|
|
|
|
|
isa => Table, |
29
|
|
|
|
|
|
|
handles => [ 'schema', 'name' ], |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
default => sub { {} }, |
43
|
|
|
|
|
|
|
handles => { |
44
|
|
|
|
|
|
|
_get_column => 'get', |
45
|
|
|
|
|
|
|
_set_column => 'set', |
46
|
|
|
|
|
|
|
_has_column => 'exists', |
47
|
|
|
|
|
|
|
}, |
48
|
|
|
|
|
|
|
init_arg => undef, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
with 'Fey::Role::Named'; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
{ |
54
|
|
|
|
|
|
|
my %Numbers; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _build_alias_name { |
57
|
3
|
|
|
3
|
|
5
|
my $self = shift; |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
18
|
my $name = $self->name(); |
60
|
3
|
|
100
|
|
|
23
|
$Numbers{$name} ||= 0; |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
69
|
return $name . ++$Numbers{$name}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub column { |
67
|
16
|
|
|
16
|
1
|
181
|
my $self = shift; |
68
|
16
|
|
|
|
|
87
|
my ($name) = pos_validated_list( \@_, { isa => 'Str' } ); |
69
|
|
|
|
|
|
|
|
70
|
16
|
100
|
|
|
|
3848
|
return $self->_get_column($name) |
71
|
|
|
|
|
|
|
if $self->_has_column($name); |
72
|
|
|
|
|
|
|
|
73
|
10
|
100
|
|
|
|
260
|
my $col = $self->table()->column($name) |
74
|
|
|
|
|
|
|
or return; |
75
|
|
|
|
|
|
|
|
76
|
9
|
|
|
|
|
1263
|
my $clone = $col->_clone(); |
77
|
9
|
|
|
|
|
36
|
$clone->_set_table($self); |
78
|
|
|
|
|
|
|
|
79
|
9
|
|
|
|
|
344
|
$self->_set_column( $name => $clone ); |
80
|
|
|
|
|
|
|
|
81
|
9
|
|
|
|
|
81
|
return $clone; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub columns { |
85
|
9
|
|
|
9
|
1
|
17
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
9
|
100
|
|
|
|
80
|
my @cols = @_ ? @_ : map { $_->name() } $self->table()->columns(); |
|
2
|
|
|
|
|
148
|
|
88
|
|
|
|
|
|
|
|
89
|
9
|
|
|
|
|
19
|
return map { $self->column($_) } @cols; |
|
10
|
|
|
|
|
33
|
|
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
|
1
|
|
|
|
|
24
|
$_[0]->columns( |
97
|
1
|
|
|
1
|
1
|
3
|
map { $_->name() } @{ $_[0]->table()->primary_key() } |
|
1
|
|
|
|
|
35
|
|
98
|
|
|
|
|
|
|
) |
99
|
|
|
|
|
|
|
]; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
14
|
|
|
14
|
1
|
231
|
sub is_alias {1} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub sql_for_select_clause { |
105
|
1
|
|
|
1
|
1
|
27
|
return $_[1]->quote_identifier( $_[0]->alias_name() ) . '.*'; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub sql_with_alias { |
109
|
6
|
|
|
6
|
1
|
160
|
return ( $_[1]->quote_identifier( $_[0]->table()->name() ) . ' AS ' |
110
|
|
|
|
|
|
|
. $_[1]->quote_identifier( $_[0]->alias_name() ) ); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
5
|
|
|
5
|
|
111
|
sub _build_id { $_[0]->alias_name() } |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__PACKAGE__->meta()->make_immutable(); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
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 |