Fey::Table::Alias - Represents an alias for a table


Fey documentation Contained in the Fey distribution.

Index


Code Index:

NAME

Top

Fey::Table::Alias - Represents an alias for a table

VERSION

Top

version 0.40

SYNOPSIS

Top

  my $alias = $user_table->alias();

  my $alias = $user_table->alias( alias_name => 'User2' );

DESCRIPTION

Top

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.

METHODS

Top

This class provides the following methods:

Fey::Table::Alias->new()

This method constructs a new Fey::Table::Alias object. It takes the following parameters:

* table - required

This is the Fey::Table object which we are aliasing.

* alias_name - optional

This should be a valid table name for your DBMS. If not provided, a unique name is automatically created.

$alias->table()

Returns the Fey::Table object for which this object is an alias.

$alias->alias_name()

Returns the name for this alias.

$alias->name()

$alias->schema()

These methods work like the corresponding methods in Fey::Table. The name() method returns the real table name.

$alias->column($name)

$alias->columns()

$alias->columns(@names)

$alias->primary_key()

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.

$alias->is_alias()

Always returns true.

$alias->sql_with_alias()

Returns the appropriate SQL snippet for the alias.

$alias->id()

Returns a unique string identifying the alias.

ROLES

Top

This class does the Fey::Role::TableLike and Fey::Role::Named roles.

BUGS

Top

See Fey for details on how to report bugs.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


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__