| SQL-Entity documentation | Contained in the SQL-Entity distribution. |
SQL::Entity::Relationship - Entities Relationship abstraction layer.
use SQL::Entity::Relationship ':all';
use SQL::Entity::Column ':all';
use SQL::Entity::Table;
use SQL::Entity::Condition ':all';
my $dept = SQL::Entity::Table->new(
name => 'dept',
alias => 'd',
columns => [
sql_column(name => 'deptno'),
sql_column(name => 'dname')
],
);
my $emp = SQL::Entity->new(
name => 'emp',
primary_key => ['empno'],
unique_expression => 'rowid',
columns => [
sql_column(name => 'ename'),
sql_column(name => 'empno'),
sql_column(name => 'deptno')
],
);
$emp->add_to_one_relationships(sql_relationship(
target_entity => $dept,
condition => sql_cond($dept->column('deptno'), '=', $entity->column('deptno'))
));
Represents relationship between entities.
sql_relationship by all tag.
Name of the relationship
Return join condition.
Return SQL condition fragment.
Returns join columns values.
Returns condition for join columns.
Returns order by sql fragment.
Associated the other end.
Creates a new relation object.
The SQL::Entity::Relationship 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::Relationship; use warnings; use strict; use Carp 'confess'; use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION); use SQL::Entity::Condition; $VERSION = '0.01'; use base 'Exporter'; @EXPORT_OK = qw(sql_relationship); %EXPORT_TAGS = (all => \@EXPORT_OK); use Abstract::Meta::Class ':all';
has '$.name';
has '$.target_entity' => (associated_class => 'SQL::Entity');
has '$.condition' => (associated_class => 'SQL::Entity::Condition');
has '@.join_columns';
has '$.order_by';
sub initialise { my ($self) = @_; $self->name($self->target_entity->id) unless $self->name; }
sub join_condition { my ($self, $entity, $bind_variables, $entity_condition) = @_; my $join_condition = $self->join_columns_condition($entity); my $condition = $self->condition; $condition = $condition && $join_condition ? $join_condition->and($condition) : $condition || $join_condition; $condition = $entity_condition ? $condition->and($entity_condition) : $condition; $condition; }
sub join_condition_as_string { my ($self, $entity, $bind_variables, $entity_condition) = @_; my $condition = $self->join_condition($entity, $bind_variables, $entity_condition); my $target_entity = $self->target_entity; my %query_columns = $entity->query_columns; $condition->as_string(\%query_columns, $bind_variables, $entity); }
sub join_columns_values { my ($self, $entity) = @_; my $target_entity = $self->target_entity; if($entity->to_one_relationship($self->name)) { $target_entity = $entity; $entity = $self->target_entity; } my @join_columns = $self->join_columns or return; my @primary_key = $entity->primary_key or confess "primary key must be defined for entity " . $entity->name; my @result; for my $i (0 .. $#primary_key) { my $column = $entity->column($primary_key[$i]) or confess "unknown primary key column: " . $primary_key[$i] . " on " . $entity->name; push @result, $column; my $join_column = $target_entity->column($join_columns[$i]) or confess "unknown foreign key column: " . $join_columns[$i] . " on " . $target_entity->name; push @result, $join_column; } @result; }
sub join_columns_condition { my ($self, $entity) = @_; my @condition = $self->join_columns_values($entity); SQL::Entity::Condition->struct_to_condition(@condition); }
sub order_by_clause { my ($self) = @_; my $order_by = $self->order_by; $order_by ? " ORDER BY ${order_by}" : ""; }
sub associate_the_other_end { my ($self, $entity) = @_; my $target_entity = $self->target_entity; $target_entity->add_to_one_relationships(sql_relationship( name => ($entity->id || $entity->name), target_entity => $entity, condition => $self->condition, ($self->join_columns ? (join_columns => [$self->join_columns ]) : ()) )); }
sub sql_relationship { __PACKAGE__->new(@_); } 1; __END__