annotate Lib/IMPL/ORM/Object.pm @ 390:de1f875e8875

added reverse matching lookup to TypeKeyedCollection (find closest descendant)
author cin
date Wed, 12 Feb 2014 18:02:03 +0400
parents 4ddb27ff4a0b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
1 package IMPL::ORM::Object;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
2 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
3 use warnings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
4
166
4267a2ac3d46 Added Class::Template,
wizard
parents: 49
diff changeset
5 use parent qw(IMPL::Object);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
6 use IMPL::Class::Property;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
7
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
8 require IMPL::ORM::Entity;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
9 require IMPL::ORM::Schema::Entity;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
10 require IMPL::ORM::Schema::Field;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
11 require IMPL::ORM::Schema::Relation::HasMany;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
12 require IMPL::ORM::Schema::Relation::HasOne;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
13 require IMPL::ORM::Schema::Relation::Subclass;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
14
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
15 BEGIN {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
16 private _direct property _entities => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
17 public property objectType => prop_all, {type => 'String'};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
18
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
19 sub _PropertyImplementor {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
20 'IMPL::ORM::PropertyImplementor'
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
21 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
22 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
23
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
24 my %schemaCache;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
25
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
26 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
27 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
28
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
29 while ( my ($class,$schema) = $this->ormGetSchema ) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
30 $this->{$_entities}{$class} = new IMPL::ORM::Entity($class,$schema);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
31 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
32 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
33
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
34 sub ormStore {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
35 my ($this,$class,$prop,$value) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
36
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
37 die IMPL::InvalidOperationException("Cannot find entity for the specified class",$class) unless $this->{$_entities}{$class};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
38
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
39 $this->{$_entities}{$class}->Store($prop,$value);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
40 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
41
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
42 sub ormGet {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
43 my ($this,$class,$prop,$value) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
44
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
45 return $this->{$_entities}{$class} ? $this->{$_entities}{$class}->Get($prop,$value) : undef;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
46 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
47
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
48 sub entityName {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
49 (my $self = ref $_[0] || $_[0]) =~ s/^.*?(\w+)$/$1/;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
50 return $self;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
51 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
52
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
53 sub ormGetSchema {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
54 my ($self,$dataSchema,$surrogate) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
55
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
56 my $schema = $surrogate || IMPL::ORM::Schema::Entity->new($self->entityName);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
57
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
58 # для текущего класса, проходим по всем свойствам
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
59 foreach my $ormProp (
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
60 $self->get_meta(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
61 'IMPL::Class::PropertyInfo',
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
62 sub {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
63 UNIVERSAL::isa($_->Implementor, 'IMPL::ORM::PropertyImplementor' )
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
64 },
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
65 0
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
66 )
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
67 ){
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
68 if ($ormProp->Mutators & prop_list) {
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
69 # отношение 1 ко многим
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
70 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);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
71 $schema->appendChild( new IMPL::ORM::Schema::Relation::HasMany($ormProp->Name, $type->entityName) );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
72 } elsif (my $type = $dataSchema->isValueType($ormProp->Type)) {
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
73 # поле
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
74 $schema->appendChild( new IMPL::ORM::Schema::Field($ormProp->Name,$ormProp->Type) );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
75 } elsif (my $entity = $dataSchema->resolveType($ormProp->Type)) {
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
76 # отношение ссылка
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
77 $schema->appendChild( new IMPL::ORM::Schema::Relation::HasOne($ormProp->Name,$entity->entityName));
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
78 } else {
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
79 # хз что. Скорее всего не удалось квалифицировать тип свойства не как ссылочный и как поле.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
80 die new IMPL::Exception('Uexpected error due building schema for a class', $ormProp->Class, $ormProp->Name,$ormProp->Type);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
81 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
82 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
83
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
84 # Формируем отношения наследования
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
85 {
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
86 # локализуем прагму
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
87 no strict 'refs';
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
88
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
89 my $class = ref $self || $self;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
90
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
91 # по всем классам
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
92 foreach my $super (grep $_->isa(__PACKAGE__), @{"${class}::ISA"}) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
93 my $type = $dataSchema->resolveType($super) or die new IMPL::InvalidOperationException("Failed to resolve a super class due building schema for a class", $class, $super);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
94 $schema->appendChild(new IMPL::ORM::Schema::Relation::Subclass($type));
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
95 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
96 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
97
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
98 return $schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
99 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
100
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
101 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
102
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
103 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
104
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
105 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
106
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
107 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
108
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
109 Базовый объект для реляционного отображения,
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
110 содержит в себе реляционные записи представляющие данный объект.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
111
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
112 Каждый класс отображается в определенную сущность. Сущности хранят
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
113 состояние объектов в том виде в котором удобно записывать в реляционную базу.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
114
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
115 =cut