NAME

Persistence::Meta::XML - Persistence meta object xml injection

SYNOPSIS

use Persistence::Meta::XML;
my $meta = Persistence::Meta::XML->new(persistence_dir => 'meta/');
my $entity_manager = $meta->inject('my_persistence.xml');
#or
# $meta->inject('my_persistence.xml');
# my $entity_manager = Persistence::Entity::Manager->manager('manager_name');

DESCRIPTION

Loads xml files that containt meta persistence definition.

persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence name="test"  connection_name="test" >
    <entities>
        <entity_file  file="emp.xml"  />
        <entity_file  file="dept.xml" />
    </entities>
    <mapping_rules>
        <orm_file file="Employee.xml" />
        <orm_file file="Department.xml" />
    </mapping_rules>
</persistence>

emp.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity name="emp" alias="e">
    <primary_key>empno</primary_key>
    <columns>
        <column name="empno" />
        <column name="ename" unique="1" />
        <column name="sal" />
        <column name="job" />
        <column name="deptno" />
    </columns>
    <subquery_columns>
        <subquery_column name="dname" entity="dept" />
    </subquery_columns>
</entity>

dept.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity name="dept" alias="d">
    <primary_key>deptno</primary_key>
    <columns>
        <column name="deptno" />
        <column name="dname"  unique="1" />
        <column name="loc" />
    </columns>
    <to_many_relationships>
        <relationship target_entity="emp" order_by="deptno, empno">
            <join_column>deptno</join_column>
        </relationship>
    </to_many_relationships>
</entity>

Employee.xml
<?xml version="1.0" encoding="UTF-8"?>
<orm entity="emp"  class="Employee" >
    <column name="empno"  attribute="id" />
    <column name="ename"  attribute="name" />
    <column name="job"    attribute="job" />
    <column name="dname"  attribute="dept_name" />
    <to_one_relationship  name="dept" attribute="dept" fetch_method="EAGER" cascade="ALL"/>
</orm>

Department.xml
<?xml version="1.0" encoding="UTF-8"?>
<orm entity="dept"  class="Department" >
    <column name="deptno" attribute="id" />
    <column name="dname" attribute="name" />
    <column name="loc" attribute="location" />
    <one_to_many_relationship  name="emp" attribute="employees" fetch_method="EAGER" cascade="ALL"/>
</orm>



package Employee;
use Abstract::Meta::Class ':all';

has '$.id';
has '$.name';
has '$.job';
has '$.dept_name';
has '$.dept' => (associated_class => 'Department');

package Department;
use Abstract::Meta::Class ':all';

has '$.id';
has '$.name';
has '$.location';
has '@.employees' => (associated_class => 'Employee');

my $meta = Persistence::Meta::XML->new(persistence_dir => $dir);
my $entity_manager = $meta->inject('persistence.xml');

my ($dept) = $entity_manager->find(dept => 'Department', name => 'dept3');

my $enp = Employee->new(id => 88, name => 'emp88');
$enp->set_dept(Department->new(id => 99, name => 'd99'));
$entity_manager->insert($enp);

EXPORT

None

ATTRIBUTES

entities
_entities_subquery_columns
_entities_to_many_relationship
_entities_to_one_relationship
cache_dir

Containts cache directory.

use_cache

Flag that indicates if use cache.

persistence_dir

Contains directory of xml files that contain persistence object definition.

METHODS

initialise
persistence_xml_handler

Retunds xml handlers that will transform the persistence xml into objects. Persistence node is mapped to the Persistence::Entity::Manager;

<!ELEMENT persistence (entities+,mapping_rules*)> <!ATTLIST persistence name #REQUIRED> <!ATTLIST persistence connection_name #REQUIRED> <!ELEMENT entities (entity_file+)> <!ELEMENT entity_file (filter_condition_value+ .dml_filter_value) > <!ATTLIST entity_file file id order_index> <!ELEMENT mapping_rules (orm_file+)> <!ATTLIST mapping_rules file>

<?xml version='1.0' encoding='UTF-8'?> <persistence name="test" connection_name="test" > <entities> <entity_file file="emp.xml" /> <entity_file file="dept.xml" /> </entities> <mapping_rules> <orm_file file="Employee" /> </mapping_rules> </persistence>

add_xml_persistence_handlers

Adds persistence xml handlers/ Takes Simple::SAX::Serializer object as parameter.

load_persistence_object

Loads persistence object. Takes entity manager object, array ref of the entity files, array ref of the ORM files.

orm_xml_handler
<!ELEMENT orm (column+, to_one_relationship*, one_to_many_relationship*, many_to_many_relationship*)
<!ATTRLIST orm class entity>
<!ELEMENT column>
<!ATTRLIST column name attribute>
<!ELEMENT to_one_relationship>
<!ATTRLIST to_one_relationship name attribute #REQUIRED>
<!ATTRLIST to_one_relationship fetch_method (LAZY|EAGER) "LAZY">
<!ATTRLIST to_one_relationship cascade (NONE|ALL|ON_INSERT|ON_UPDATE|ON_DELETE) "NONE">

<orm entity="emp"  class="Employee" >
    <column name="empno" attribute="id" />
    <column name="ename" attribute="name" />
    <column name="job" attribute="job" />
    <to_one_relationship name="dept" attribute="depts" fetch_method="LAZY" cascade="ALL">
</orm>

many_to_many 'project' => (
    attribute        => has('%.projects' => (associated_class => 'Project'), index_by => 'name'),
    join_entity_name => 'emp_project',
    fetch_method     => LAZY,
    cascade          => ALL,
);
orm_xml_handler

Retunds xml handlers that will transform the orm xml into Persistence::ORM object

add_orm_xml_handlers

Adds orm xml handler to Simple::SAX::Serializer object.

create_orm_mapping

Creates orm mappings. Takes

_add_one_to_many_relationship
_add_to_many_to_many_relationship
_add_to_one_relationship
_add_relationship_parameters
entity_xml_handler

Retunds xml handlers that will transform the enity xml into Persistence::Entity

<!ELEMENT entity (primary_key*, indexes?, columns?, subquery_columns?, filter_condition_value+ .dml_filter_value+, to_one_relationships? to_many_relationships?, value_generators*)> <!ATTLIST entity id name alias unique_expression query_from schema order_index> <!ELEMENT primary_key (#PCDATA)> <!ELEMENT indexes (index+)> <!ELEMENT index (index_columns+)> <!ATTLIST index name hint> <!ELEMENT index_columns (#PCDATA)> <!ELEMENT columns (column+) > <!ELEMENT subquery_columns (subquery_column+)> <!ELEMENT subquery_column> <!ATTLIST subquery_column entity name> <!ELEMENT column> <!ATTLIST column id name unique expression case_sensitive queryable insertable updatable> <!ELEMENT filter_condition_values (#PCDATA)> <!ATTLIST filter_condition_values name #REQUIRED> <!ELEMENT dml_filter_values (#PCDATA)> <!ATTLIST dml_filter_values name #REQUIRED> <!ELEMENT to_one_relationships (relationship+)> <!ELEMENT to_many_relationships (relationship+)> <!ELEMENT relationship (join_columns*, condition?)> <!ATTLIST relationship name target_entity order_by> <!ELEMENT join_columns (#PCDATA)> <!ELEMENT condition (condition+) > <!ATTLIST condition operand1 operator operand2 relation> <!ELEMENT value_generators (#PCDATA)>

For instnace. <?xml version="1.0" encoding="UTF-8"?> <entity name="emp" alias="e"> <primary_key>empno</primary_key> <indexes> <index name="emp_idx_empno" hint="INDEX_ASC(e emp_idx_empno)"> <index_column>ename</index_column> </index> <index name="emp_idx_ename"> <index_column>empno</index_column> </index> </indexes> <columns> <column name="empno" /> <column name="ename" /> </columns> <subquery_columns> <subquery_column name="dname" entity_id="dept" /> </subquery_columns> <to_one_relationships> <relationship target_entity="dept"> <join_column>deptno</join_column> </relationship> </to_one_relationships> </entity>

add_entity_xml_handlers

Adds entity xml handler to the Simple::SAX::Serializer object.

_initialise_subquery_columns

Initialise subquery columns

_initialise_to_one_relationship

Initialise to one relationships

_initialise_to_many_relationship

Initialise to manye relationships

_initialise_relationships

Initialises relationshsips Takes relationship type as parameters. Allowed value: 'to_one_relationships', 'to_many_relationships'

_relationship

Returns the relationship object. Takes hash_ref, that will be transformed to the new object parameters.

_parse_condition

Parses condition object to replacase ant occurence of <entity>.<column> to column object.

has_column
entity_column

Returns entity column

SEE ALSO

Simple::SAX::Handler

COPYRIGHT AND LICENSE

The Persistence::Meta::Xml 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