Persistence::Entity - Entity persistence abstraction layer.
SQL::Entity::Table | +----SQL::Entity | +----Persistence::Entity
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);
This class represents database entity.
sql_relationship sql_column sql_index sql_cond sql_and sql_or by ':all' tag
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 });
Hash that contains pair of column and its value generator.
Hash ref that contains filter values, that values will be used as condition values
Hash ref that contains columns values that will be added to all dml operations.
Validates triggers types
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.
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)
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
Returns true if refreshis required
Inserts entities' row that is passed in as fields to insert.
$entity->insert(col1 => 'val1', col2 => 'val2');
Adds autogenerated values. Takes hash ref to fields values
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);
Returns join columns values for passed in relation
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'});
Merges entities' row, takes fields to merge as named parameteres, $entity->merge(col1 => 'val1', col2 => 'val2', the_rowid => '0xAAFF');
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 entities' row that meets passed in condition values Takes condition values. $entity->delete(the_rowid => 'xx');
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.
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.
Returns true if passed in dataset contains primary key values.
Retrieves primary key values.
Executes passed in sql statements with all callback defined by decorators (triggers)
Executes query
SQL::Entity
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.
Adrian Witas, adrian@webapp.strefa.pl
To install Persistence::Entity, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Persistence::Entity
CPAN shell
perl -MCPAN -e shell install Persistence::Entity
For more information on module installation, please visit the detailed CPAN module installation guide.