annotate Lib/IMPL/ORM/Object.pm @ 28:6d33f75c6e1f

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