line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Fey::Table::Alias; |
2
|
|
|
|
|
|
|
|
3
|
26
|
|
|
26
|
|
127
|
use strict; |
|
26
|
|
|
|
|
44
|
|
|
26
|
|
|
|
|
1083
|
|
4
|
26
|
|
|
26
|
|
122
|
use warnings; |
|
26
|
|
|
|
|
81
|
|
|
26
|
|
|
|
|
744
|
|
5
|
26
|
|
|
26
|
|
121
|
use namespace::autoclean; |
|
26
|
|
|
|
|
39
|
|
|
26
|
|
|
|
|
207
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.42'; |
8
|
|
|
|
|
|
|
|
9
|
26
|
|
|
26
|
|
2452
|
use Fey::Exceptions qw(param_error); |
|
26
|
|
|
|
|
157
|
|
|
26
|
|
|
|
|
1511
|
|
10
|
26
|
|
|
26
|
|
138
|
use Fey::Table; |
|
26
|
|
|
|
|
36
|
|
|
26
|
|
|
|
|
484
|
|
11
|
26
|
|
|
26
|
|
392
|
use Fey::Types qw( Column HashRef Str Table ); |
|
26
|
|
|
|
|
37
|
|
|
26
|
|
|
|
|
193
|
|
12
|
|
|
|
|
|
|
|
13
|
26
|
|
|
26
|
|
283358
|
use Moose 0.90; |
|
26
|
|
|
|
|
723
|
|
|
26
|
|
|
|
|
190
|
|
14
|
26
|
|
|
26
|
|
142858
|
use MooseX::Params::Validate 0.08 qw( pos_validated_list ); |
|
26
|
|
|
|
|
66797
|
|
|
26
|
|
|
|
|
188
|
|
15
|
26
|
|
|
26
|
|
5684
|
use MooseX::SemiAffordanceAccessor 0.03; |
|
26
|
|
|
|
|
11892
|
|
|
26
|
|
|
|
|
185
|
|
16
|
26
|
|
|
26
|
|
81278
|
use MooseX::StrictConstructor 0.07; |
|
26
|
|
|
|
|
14242
|
|
|
26
|
|
|
|
|
165
|
|
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
|
|
4
|
my $self = shift; |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
14
|
my $name = $self->name(); |
60
|
3
|
|
100
|
|
|
14
|
$Numbers{$name} ||= 0; |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
74
|
return $name . ++$Numbers{$name}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub column { |
67
|
16
|
|
|
16
|
1
|
134
|
my $self = shift; |
68
|
16
|
|
|
|
|
96
|
my ($name) = pos_validated_list( \@_, { isa => 'Str' } ); |
69
|
|
|
|
|
|
|
|
70
|
16
|
100
|
|
|
|
3513
|
return $self->_get_column($name) |
71
|
|
|
|
|
|
|
if $self->_has_column($name); |
72
|
|
|
|
|
|
|
|
73
|
10
|
100
|
|
|
|
219
|
my $col = $self->table()->column($name) |
74
|
|
|
|
|
|
|
or return; |
75
|
|
|
|
|
|
|
|
76
|
9
|
|
|
|
|
1275
|
my $clone = $col->_clone(); |
77
|
9
|
|
|
|
|
40
|
$clone->_set_table($self); |
78
|
|
|
|
|
|
|
|
79
|
9
|
|
|
|
|
325
|
$self->_set_column( $name => $clone ); |
80
|
|
|
|
|
|
|
|
81
|
9
|
|
|
|
|
83
|
return $clone; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub columns { |
85
|
9
|
|
|
9
|
1
|
19
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
9
|
100
|
|
|
|
87
|
my @cols = @_ ? @_ : map { $_->name() } $self->table()->columns(); |
|
2
|
|
|
|
|
159
|
|
88
|
|
|
|
|
|
|
|
89
|
9
|
|
|
|
|
25
|
return map { $self->column($_) } @cols; |
|
10
|
|
|
|
|
34
|
|
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
|
|
|
|
|
32
|
|
98
|
|
|
|
|
|
|
) |
99
|
|
|
|
|
|
|
]; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
14
|
|
|
14
|
1
|
208
|
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
|
138
|
return ( $_[1]->quote_identifier( $_[0]->table()->name() ) . ' AS ' |
110
|
|
|
|
|
|
|
. $_[1]->quote_identifier( $_[0]->alias_name() ) ); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
5
|
|
|
5
|
|
138
|
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.42 |
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 |