21
|
1 package IMPL::ORM::Object;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
27
|
5 use base qw(IMPL::Object);
|
|
6 use IMPL::Class::Property;
|
|
7 use IMPL::Class::Property::Direct;
|
|
8
|
|
9 require IMPL::ORM::Entity;
|
28
|
10 require IMPL::ORM::Schema::Entity;
|
|
11 require IMPL::ORM::Schema::Field;
|
|
12 require IMPL::ORM::Schema::Relation::HasMany;
|
|
13 require IMPL::ORM::Schema::Relation::HasOne;
|
|
14 require IMPL::ORM::Schema::Relation::Subclass;
|
27
|
15
|
|
16 BEGIN {
|
|
17 private _direct property _entities => prop_all;
|
28
|
18 public property objectType => prop_all;
|
27
|
19 }
|
|
20
|
|
21 my %schemaCache;
|
|
22
|
|
23 sub CTOR {
|
|
24 my ($this) = @_;
|
|
25
|
|
26 while ( my ($class,$schema) = $this->ormGetSchema ) {
|
|
27 $this->{$_entities}{$class} = new IMPL::ORM::Entity($class,$schema);
|
|
28 }
|
|
29 }
|
|
30
|
|
31 sub ormStore {
|
|
32 my ($this,$class,$prop,$value) = @_;
|
|
33
|
|
34 die IMPL::InvalidOperationException("Cannot find entity for the specified class",$class) unless $this->{$_entities}{$class};
|
|
35
|
|
36 $this->{$_entities}{$class}->Store($prop,$value);
|
|
37 }
|
21
|
38
|
27
|
39 sub ormGet {
|
|
40 my ($this,$class,$prop,$value) = @_;
|
|
41
|
|
42 return $this->{$_entities}{$class} ? $this->{$_entities}{$class}->Get($prop,$value) : undef;
|
|
43 }
|
|
44
|
28
|
45 #sub _PropertyImplementor {
|
|
46 # 'IMPL::ORM::Property'
|
|
47 #}
|
|
48
|
|
49 sub entityName {
|
|
50 (my $self = ref $_[0] || $_[0]) =~ s/::/_/g;
|
|
51 return $self;
|
27
|
52 }
|
|
53
|
|
54 sub ormGetSchema {
|
28
|
55 my ($self,$dataSchema) = @_;
|
27
|
56
|
28
|
57 my $schema = IMPL::ORM::Schema::Entity->new($self->entityName);
|
27
|
58
|
|
59 foreach my $ormProp (
|
|
60 $self->get_meta(
|
|
61 'IMPL::Class::PropertyInfo',
|
|
62 sub {
|
|
63 UNIVERSAL::isa($_->Implementor, 'IMPL::ORM::Property' )
|
|
64 },
|
28
|
65 0
|
27
|
66 )
|
|
67 ){
|
28
|
68 if ($ormProp->Mutators & prop_list) {
|
|
69 my $type = $dataSchema->resolveType($ormProp->Type) or die new IMPL::InvalidOperationException("Failed to resolve a reference type due building schema for a class", $ormProp->Class, $ormProp->Name);
|
|
70 $schema->appendChild( new IMPL::ORM::Schema::Relation::HasMany($ormProp->Name, $type->entityName) );
|
|
71 } elsif (my $type = $dataSchema->isValueType($ormProp->Type,'IMPL::ORM::Object')) {
|
|
72 $schema->appendChild( new IMPL::ORM::Schema::Field($ormProp->Name,$type) );
|
|
73 } elsif (my $entity = $dataSchema->resolveType($ormProp->Type)) {
|
|
74 $schema->appendChild( new IMPL::ORM::Schema::Relation::HasOne($ormProp->Name,$entity->entityName));
|
|
75 } else {
|
|
76 die new IMPL::Exception('Uexpected error due building schema for a class', $ormProp->Class, $ormProp->Name);
|
|
77 }
|
27
|
78 }
|
|
79
|
28
|
80 return $schema;
|
27
|
81 }
|
21
|
82
|
|
83 1;
|
27
|
84
|
|
85 __END__
|
|
86
|
|
87 =pod
|
|
88
|
|
89 =head1 DESCRIPTION
|
|
90
|
|
91 Базовый объект для реляционного отображения,
|
|
92 содержит в себе реляционные записи представляющие данный объект.
|
|
93
|
28
|
94 Каждый класс отображается в определенную сущность. Сущности хранят
|
|
95 состояние объектов в том виде в котором удобно записывать в реляционную базу.
|
|
96
|
27
|
97 =cut |