| Fey documentation | Contained in the Fey distribution. |
Fey::Column::Alias - Represents an alias for a column
version 0.40
my $alias = $user_id_col->alias();
This class represents an alias for a column. Column aliases allow you to use the same column in different ways multiple times in a query, which makes certain types of queries simpler to express.
This method constructs a new Fey::Column::Alias object. It takes
the following parameters:
This is the Fey::Column object which we are aliasing.
This should be a valid column name for your DBMS. If not provided, a unique name is automatically created.
This returns the name of the column for which this object is an alias.
Returns the name for this alias.
Returns the specified attribute for the column, just like the
Fey::Column methods of the same name.
Returns the Fey::Table object to which the column alias belongs, if
any.
Always returns false.
Returns the appropriate SQL snippet for the alias.
Returns a unique identifier for the column. This method throws an exception if the alias does not belong to a table.
This class does the Fey::Role::ColumnLike and Fey::Role::Named roles.
See Fey for details on how to report bugs.
Dave Rolsky <autarch@urth.org>
This software is Copyright (c) 2011 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
| Fey documentation | Contained in the Fey distribution. |
package Fey::Column::Alias; BEGIN { $Fey::Column::Alias::VERSION = '0.40'; } use strict; use warnings; use namespace::autoclean; use Fey::Column; use Fey::Exceptions qw( object_state_error ); use Fey::Table; use Fey::Table::Alias; use Fey::Types qw( Column Str ); use Moose; use MooseX::SemiAffordanceAccessor; use MooseX::StrictConstructor; with 'Fey::Role::ColumnLike'; has 'id' => ( is => 'ro', lazy_build => 1, init_arg => undef, clearer => '_clear_id', ); has 'column' => ( is => 'ro', isa => Column, handles => [ qw( name type generic_type length precision is_auto_increment is_nullable table ) ], ); has 'alias_name' => ( is => 'ro', isa => Str, lazy_build => 1, ); with 'Fey::Role::Named'; { my %Numbers; sub _build_alias_name { my $self = shift; my $name = $self->name(); $Numbers{$name} ||= 0; return $name . ++$Numbers{$name}; } } sub is_alias {1} sub sql { $_[1]->quote_identifier( $_[0]->alias_name() ) } sub sql_with_alias { my $sql = $_[1]->quote_identifier( undef, $_[0]->_containing_table_name_or_alias(), $_[0]->name(), ); $sql .= ' AS '; $sql .= $_[1]->quote_identifier( $_[0]->alias_name() ); return $sql; } sub sql_or_alias { goto &sql } sub _build_id { my $self = shift; my $table = $self->table(); object_state_error 'The id attribute cannot be determined for a column object which has no table.' unless $table; return $table->id() . '.' . $self->alias_name(); } __PACKAGE__->meta()->make_immutable(); 1; # ABSTRACT: Represents an alias for a column
__END__