| Fey documentation | Contained in the Fey distribution. |
Fey::Table::Alias - Represents an alias for a table
version 0.40
my $alias = $user_table->alias(); my $alias = $user_table->alias( alias_name => 'User2' );
This class represents an alias for a table. Table aliases allow you to join the same table more than once in a query, which makes certain types of queries simpler to express.
This class provides the following methods:
This method constructs a new Fey::Table::Alias object. It takes the
following parameters:
This is the Fey::Table object which we are aliasing.
This should be a valid table name for your DBMS. If not provided, a unique name is automatically created.
Returns the Fey::Table object for which this object is an alias.
Returns the name for this alias.
These methods work like the corresponding methods in
Fey::Table. The name() method returns the real table name.
These methods work like the corresponding methods in
Fey::Table. However, the columns they return will return the alias
object when $column->table() is called.
Always returns true.
Returns the appropriate SQL snippet for the alias.
Returns a unique string identifying the alias.
This class does the Fey::Role::TableLike 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::Table::Alias; BEGIN { $Fey::Table::Alias::VERSION = '0.40'; } use strict; use warnings; use namespace::autoclean; use Fey::Exceptions qw(param_error); use Fey::Table; use Fey::Types qw( Column HashRef Str Table ); use Moose; use MooseX::Params::Validate qw( pos_validated_list ); use MooseX::SemiAffordanceAccessor; use MooseX::StrictConstructor; with 'Fey::Role::TableLike'; has 'id' => ( is => 'ro', lazy_build => 1, init_arg => undef, ); has 'table' => ( is => 'ro', isa => Table, handles => [ 'schema', 'name' ], ); has 'alias_name' => ( is => 'ro', isa => Str, lazy_build => 1, ); has '_columns' => ( traits => ['Hash'], is => 'bare', isa => HashRef[Column], default => sub { {} }, handles => { _get_column => 'get', _set_column => 'set', _has_column => 'exists', }, init_arg => undef, ); 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 column { my $self = shift; my ($name) = pos_validated_list( \@_, { isa => 'Str' } ); return $self->_get_column($name) if $self->_has_column($name); my $col = $self->table()->column($name) or return; my $clone = $col->_clone(); $clone->_set_table($self); $self->_set_column( $name => $clone ); return $clone; } sub columns { my $self = shift; my @cols = @_ ? @_ : map { $_->name() } $self->table()->columns(); return map { $self->column($_) } @cols; } # Making this an attribute would be a hassle since we'd need to reset # it whenever the associated table's keys changed. sub primary_key { return [ $_[0]->columns( map { $_->name() } @{ $_[0]->table()->primary_key() } ) ]; } sub is_alias {1} sub sql_with_alias { return ( $_[1]->quote_identifier( $_[0]->table()->name() ) . ' AS ' . $_[1]->quote_identifier( $_[0]->alias_name() ) ); } sub _build_id { $_[0]->alias_name() } __PACKAGE__->meta()->make_immutable(); 1; # ABSTRACT: Represents an alias for a table
__END__