Alzabo
view release on metacpan or search on metacpan
lib/Alzabo/Runtime/Table.pm view on Meta::CPAN
my $self = shift;
$self->{prefetch} = $self->_canonize_prefetch(@_);
}
sub _canonize_prefetch
{
my $self = shift;
validate_pos( @_, ( { isa => 'Alzabo::Column' } ) x @_ );
foreach my $c (@_)
{
params_exception "Column " . $c->name . " doesn't exist in $self->{name}"
unless $self->has_column( $c->name );
}
return [ map { $_->name } grep { ! $_->is_primary_key } @_ ];
}
sub prefetch
{
my $self = shift;
return ref $self->{prefetch} ? @{ $self->{prefetch} } : ();
}
sub add_group
{
my $self = shift;
validate_pos( @_, ( { isa => 'Alzabo::Column' } ) x @_ );
my @names = map { $_->name } @_;
foreach my $col (@_)
{
params_exception "Column " . $col->name . " doesn't exist in $self->{name}"
unless $self->has_column( $col->name );
next if $col->is_primary_key;
$self->{groups}{ $col->name } = \@names;
}
}
sub group_by_column
{
my $self = shift;
my $col = shift;
return exists $self->{groups}{$col} ? @{ $self->{groups}{$col} } : $col;
}
my $alias_num = '000000000';
sub alias
{
my $self = shift;
my $clone;
%$clone = %$self;
bless $clone, ref $self;
$clone->{alias_name} = $self->name . ++$alias_num;
$clone->{real_table} = $self;
$clone->{columns} = Tie::IxHash->new( map { $_->name => $_ } $self->columns );
# Force clone of primary key columns right away.
$clone->column($_) foreach map { $_->name } $self->primary_key;
return $clone;
}
#
# Since its unlikely that a user will end up needing clones of more
# than 1-2 columns each time an alias is used, we only make copies as
# needed.
#
sub column
{
my $self = shift;
# I'm an alias, make an alias column
if ( $self->{alias_name} )
{
my $name = shift;
my $col = $self->SUPER::column($name);
# not previously cloned
unless ( $col->table eq $self )
{
# replace our copy of this column with a clone
$col = $col->alias_clone( table => $self );
my $index = $self->{columns}->Indices($name);
$self->{columns}->Replace( $index, $col, $name );
Scalar::Util::weaken( $col->{table} );
delete $self->{pk_array} if $col->is_primary_key;
}
return $col;
}
else
{
return $self->SUPER::column(@_);
}
}
sub alias_name
{
# intentionally don't call $_[0]->name for a noticeable
# performance boost
return $_[0]->{alias_name} || $_[0]->{name};
}
sub real_table
{
return $_[0]->{real_table} || $_[0];
}
( run in 1.206 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )