NAME
Persistence::Entity - Entity persistence abstraction layer.
CLASS HIERARCHY
SQL::Entity::Table
|
+----SQL::Entity
|
+----Persistence::Entity
SYNOPSIS
use Persistence::Entity ':all';
my $membership_entity = Persistence::Entity->new(
name => 'wsus_user_service',
alias => 'us',
primary_key => ['user_id', 'service_id'],
columns => [
sql_column(name => 'user_id'),
sql_column(name => 'service_id'),
sql_column(name => 'agreement_flag')
],
);
my $user_entity = Persistence::Entity->new(
name => 'wsus_user',
alias => 'ur',
primary_key => ['id'],
columns => [
sql_column(name => 'id'),
sql_column(name => 'username'),
sql_column(name => 'password'),
sql_column(name => 'email'),
],
to_many_relationships => [sql_relationship(target_entity => $membership_entity, join_columns => ['user_id'])]
);
$entity_manager->add_entities($membership_entity, $user_entity);
DESCRIPTION
This class represents database entity.
EXPORT
sql_relationship
sql_column
sql_index
sql_cond
sql_and
sql_or by ':all' tag
ATTRIBUTES
- trigger
-
Defines tigger that will execute on one of the following event before_insert after_insert before_update after_update before_delete after_delete, on_fetch Takes event name as first parameter, and callback as secound parameter.
$entity->trigger(before_insert => sub { my ($self) = @_; #do stuff });
- entity_manager
- value_generators
-
Hash that contains pair of column and its value generator.
- filter_condition_values
-
Hash ref that contains filter values, that values will be used as condition values
- dml_filter_values
-
Hash ref that contains columns values that will be added to all dml operations.
METHODS
- run_event
- validate_trigger
-
Validates triggers types
- find
-
Returns list of objects or resultsets for passed in entity that meets passed in condition Takes entity name, class name to which resultset will be casted, (if class name is undef then hash ref will be return instead), list of names parameters that will be used as condition or condition object. If class name has the ORM mapping, then name parameters must be objects' attributs . Condition object always should use entity column.
my $entity = $entity_manager->entity('emp'); my ($emp) = $entity->find('Employee', ename => 'adrian'); or my @emp = $entity->find('Employee', sql_cond('ename', 'LIKE', 'a%')); #array of Employee objects.
- search
-
Returns list of objects or resultsets for passed in entity that meets passed in condition Takes entity name, class name to which resultset will be casted, (if class name is undef then hash ref will be return instead), list of names parameters that will be used as condition or condition object. If class name has the ORM mapping, then name parameters must be objects' attributs . Condition object always should use entity column.
my $entity = $entity_manager->entity('emp'); my ($emp) = $entity->find('Employee', ename => 'adrian'); or my @emp = $entity->find('Employee', sql_cond('ename', 'LIKE', 'a%')); #array of Employee objects.
- lock
-
Returns and locks list and of objects or resultsets for passed in entity that meets passed in condition Takes entity name, class name to which resultset will be casted, (if class name is undef then hash ref will be return instead), list of names parameters that will be used as condition or condition object. If class name has the ORM mapping, then name parameters must be objects' attributs . Condition object always should use entity column. Locking is forced by SELECT ... FOR UPDATE clause
my $entity = $entity_manager->entity('emp'); my ($emp) = $entity->lock('Employee', ename => 'adrian'); or my @emp = $entity->lock('Employee', sql_cond('ename', 'LIKE', 'a%')); #array of Employee objects. or my @emp = $entity->lock(undef, sql_cond('ename', 'LIKE', 'a%')); #array of resultset (hash ref)
- relationship_query
-
Return rows for relationship. Takes relationship_name, class_name, target_class_name, condition arguments as parameters.
# $user_entity->add_to_many_relationships(sql_relationship(target_entity => $membership_entity, join_columns => ['user_id']));
my $entity_manager = Persistence::Entity::Manager->new(connection_name => 'my_connection'); $entity_manager->add_entities($membership_entity, $user_entity);
my @membership = $user_entity->relationship_query('wsus_user_service', undef => undef, username => 'test'); # returns array of hash refs.
or
my @membership = $user_entity->relationship_query('wsus_user_service', 'User' => 'ServiceMembership', username => 'test'); # returns array of ServiceMembership objects
- is_refresh_required
-
Returns true if refreshis required
- insert
-
Inserts entities' row that is passed in as fields to insert.
$entity->insert(col1 => 'val1', col2 => 'val2');
- _autogenerated_values
-
Adds autogenerated values. Takes hash ref to fields values
- relationship_insert
-
Inserts relation entities' rows that are passed in Takes relation name, dataset that represents current entity row, array ref where item can be either object or hash ref that represents row to be asssociated .
$user_entity->relationship_insert('wsus_user_service', {username => 'test'} , {service_id => 1}, {service_id => 9});
$user_entity->relationship_insert('wsus_user_service', $user, $membership1, $membership1);
- _join_columns_values
-
Returns join columns values for passed in relation
- update
-
Updates entities' row that is passed in as fields to update. Takes fields values as hash ref, condition values as hash reference.
$entity->update({col1 => 'val1', col2 => 'val2'}, {the_rowid => 'xx'});
- merge
-
Merges entities' row, takes fields to merge as named parameteres, $entity->merge(col1 => 'val1', col2 => 'val2', the_rowid => '0xAAFF');
- relationship_merge
-
Merges relation entities' rows that are passed in. Takes relation name, dataset that represents current entity row, array ref where item can be either object or hash ref that represent asssociated row to merge.
$user_entity->relationship_merge('wsus_user_service', {username => 'test'} , {service_id => 1, agreement_flag => 1}, {service_id => 5, agreement_flag => 1});
- delete
-
Delete entities' row that meets passed in condition values Takes condition values. $entity->delete(the_rowid => 'xx');
- relationship_delete
-
Inserts relation entities' row that is passed in as fields to insert. Takes relation name, dataset that represents current entity row, array ref where item can be either associated object or hash ref that represent asssociated row.
$user_entity->relationship_insert('wsus_user_service', {username => 'test'} , {service_id => 1}, {service_id => 9});
$user_entity->relationship_insert('wsus_user_service', $user, $membership1, $membership1);
- primary_key_values
-
Returns primary key values. Takes fields values that will be used as condition to retrive primary key values in case they are not contain primary key values.
- has_primary_key_values
-
Returns true if passed in dataset contains primary key values.
- retrive_primary_key_values
-
Retrieves primary key values.
- _execute_statement
-
Executes passed in sql statements with all callback defined by decorators (triggers)
- _execute_query
-
Executes query
SEE ALSO
COPYRIGHT AND LICENSE
The Persistence::Entity 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.
AUTHOR
Adrian Witas, adrian@webapp.strefa.pl