SQL::Entity::Column - Entity column abstraction.


SQL-Entity documentation Contained in the SQL-Entity distribution.

Index


Code Index:

NAME

Top

SQL::Entity::Column - Entity column abstraction.

SYNOPSIS

Top

    use SQL::Entity::Column ':all';

    my $column = SQL::Entity::Column->new(name  => 'name');
    or 
    my $column = sql_column(name  => 'name');

DESCRIPTION

Top

Represents entities column, that maps to the table column, or sql expression.

EXPORT

None by default.

sql_column by tag 'all'

ATTRIBUTES

id

Column alias

name
table

Table association

entity

Entity association

expression

Column expression: f.e. col1 || col2

case_sensitive
queryable

Flag is column can be use in where caluse

insertable
update_allowed
unique

METHODS

initialise
as_string
subquery_to_string
as_operand

Returns column as condition operand.

sql_column

COPYRIGHT

Top

SEE ALSO

Top

SQL::Entity SQL::Entity::Table SQL::Entity::Condition

AUTHOR

Top

Adrian Witas, adrian@webapp.strefa.pl


SQL-Entity documentation Contained in the SQL-Entity distribution.
package SQL::Entity::Column;

use strict;
use warnings;
use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION);

$VERSION = '0.02';

use Abstract::Meta::Class ':all';
use base 'Exporter';

@EXPORT_OK = qw(sql_column);
%EXPORT_TAGS = (all => \@EXPORT_OK);

has '$.id';


has '$.name';


has '$.table' => (associated_class => 'SQL::Entity::Table');

has '$.entity' => (associated_class => 'SQL::Entity');


has '$.expression';


has '$.case_sensitive' => (default => 1);


has '$.queryable' => (default => 1);


has '$.insertable' => (default => 1);


has '$.updatable' => (default => 1);


has '$.unique';


sub initialise {
    my ($self) = @_;
    $self->set_id($self->name) unless $self->id;
    if ($self->expression) {
        $self->insertable(0);
        $self->updatable(0);
    }
}


sub as_string {
    my ($self, $source, $join_methods) = @_;
    my $table = $self->table;
    return $self->subquery_to_string($source, $join_methods)
      if ($table && $source && $table ne $source);
    my $table_alias = $table ?  $table->alias . "." : '';
    my $id = $self->id;
    my $expression = $self->expression;
    my $name = $self->name;
    my $result = ($expression ? "($expression)" : $table_alias . $name);
    $result . ($id && $id ne ($name || '') ? " AS $id": '');
}


sub subquery_to_string {
    my ($self, $source, $join_methods) = @_;
    return () unless $self->entity;
    my $table = $self->table;
    my $id = $self->id;
    my $table_alias = $table ?  $table->alias . "." : '';
    my $table_id = $table->id;
    my $relationship = $self->entity->to_one_relationship($table_id);
    my $join_method = $join_methods->{$table_id};
    unless ($join_method) {
        #enforce subquery
        my ($sql, $bind_variable) = $table->query([$id], $relationship->join_condition($self->entity), undef, {$source->id => 'SUBQUERY'});
        
        return "($sql) AS $id";
    } else {
        $self->as_string;
    }
}


sub as_operand {
    my $self = shift;
    my $table = $self->table;
    my $table_alias = $table ?  $table->alias . "." : '';
    my $case_sensitive = $self->case_sensitive;
    (! $case_sensitive ? 'UPPER(' : '')
      . $table_alias . ($self->name || $self->expression)
      . (! $case_sensitive ? ')' : '');
}

sub sql_column {
    __PACKAGE__->new(@_);
}


1;

__END__