| SQL-Entity documentation | Contained in the SQL-Entity distribution. |
SQL::Entity::Column - Entity column abstraction.
use SQL::Entity::Column ':all';
my $column = SQL::Entity::Column->new(name => 'name');
or
my $column = sql_column(name => 'name');
Represents entities column, that maps to the table column, or sql expression.
None by default.
sql_column by tag 'all'
Column alias
Table association
Entity association
Column expression: f.e. col1 || col2
Flag is column can be use in where caluse
Returns column as condition operand.
The SQL::Entity::Column module is free software. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
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__