annotate Lib/IMPL/ORM/Schema.pm @ 165:76515373dac0

Added Class::Template, Rewritten SQL::Schema 'use parent' directive instead of 'use base'
author wizard
date Sat, 23 Apr 2011 23:06:48 +0400
parents 2d1c3f713280
children 4267a2ac3d46
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::Schema;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
5 use base qw(IMPL::DOM::Document);
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 require IMPL::ORM::Schema::Entity;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
8 require IMPL::ORM::Schema::ValueType;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
9
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
10 our %CTOR = (
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
11 'IMPL::DOM::Document' => sub { nodeName => 'ORMSchema' }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
12 );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
13
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
14 BEGIN {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
15 public property mapValueTypes => prop_get | owner_set;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
16 public property mapReferenceTypes => prop_get | owner_set;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
17 public property mapPending => prop_get | owner_set;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
18 public property prefix => prop_get | owner_set;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
19 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
20
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
21 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
22 my ($this ) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
23 $this->mapValueTypes({});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
24 $this->mapReferenceTypes({});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
25 $this->mapPending({});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
26 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
27
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
28 # return an entity for the specified typename
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
29 # makes forward declaration if nesessary
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
30 sub resolveType {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
31 my ($this,$typeName) = @_;
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 $this = ref $this ? $this : $this->instance;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
34
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
35 if (my $entity = $this->mapReferenceTypes->{$typeName}) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
36 return $entity;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
37 } elsif (UNIVERSAL::isa($typeName,'IMPL::ORM::Object')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
38 return $this->declareReferenceType($typeName);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
39 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
40 return undef;
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 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
43
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
44 sub declareReferenceType {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
45 my ($this,$typeName) = @_;
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 my $entity = new IMPL::ORM::Schema::Entity($typeName->entityName);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
48
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
49 $this->mapPending->{$typeName} = $entity;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
50
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
51 $this->appendChild($entity);
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 return $this->mapReferenceTypes->{$typeName} = $entity;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
54 }
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 sub _addReferenceType {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
57 my ($this,$className) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
58
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
59 if ( my $entity = delete $this->mapPending->{$className} ) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
60 $className->ormGetSchema($this,$entity);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
61 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
62 return $this->appendChild( $this->mapReferenceTypes->{$className} = $className->ormGetSchema($this) );
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
63 }
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 }
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 # returns valuetype name
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
68 sub isValueType {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
69 my ($this,$typeName) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
70
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
71 $this = ref $this ? $this : $this->instance;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
72
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
73 return $this->mapValueTypes->{$typeName};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
74 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
75
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
76 my %instances;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
77 sub instance {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
78 my ($class) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
79
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
80 return ($instances{$class} || ($instances{$class} = $class->new));
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 sub ValueTypes {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
84 my ($this,%classes) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
85
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
86 $this = ref $this ? $this : $this->instance;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
87
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
88 while ( my ($typeName,$typeReflected) = each %classes ) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
89 $this->mapValueTypes->{$typeName} = $typeReflected;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
90 $this->appendChild(IMPL::ORM::Schema::ValueType->new($typeName,$typeReflected));
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
91 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
92 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
93
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
94 sub Classes {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
95 my ($this,@classNames) = @_;
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 $this = ref $this ? $this : $this->instance;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
98
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
99 $this->_addReferenceType($this->prefix . $_) foreach @classNames;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
102 sub usePrefix {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
103 my ($this,$prefix) = @_;
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 $prefix .= '::' if $prefix and $prefix !~ /::$/;
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 (ref $this ? $this : $this->instance)->prefix($prefix);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
108 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
109
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
110 sub CompleteSchema {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
111 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
112
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
113 $this = ref $this ? $this : $this->instance;
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 $_->ormGetSchema($this,delete $this->mapPending->{$_}) foreach (keys %{$this->mapPending});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
116 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
117
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
118 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
119
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
120 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
121
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
122 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
123
79
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
124 =head1 NAME
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
125
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
126 C<IMPL::ORM::Schema> Схема отображения классов в реляционную структуру.
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
127
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
128 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
129
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
130 Схема данных, представляет собой DOM документ, элементами которой
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
131 являются сущности.
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
132
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
133 Каждый узел - это описание сущности.
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
134
79
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
135 =begin code xml
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
136
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
137 <Schema>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
138 <Entity entityName="My_Data_Foo">
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
139 <Field fieldName="Doo" fieldType="String"/>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
140 <HasMany name="Boxes" target="My_Data_Box"/>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
141 </Entity>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
142 <Entity entityName="My_Data_Bar">
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
143 <Subclass base="My_Data_Foo"/>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
144 <Field fieldName="Timestamp" fieldType="Integer"/>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
145 </Entity>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
146 <Entity entityName="My_Data_Box">
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
147 <Field fieldName="Capacity" fieldType="Integer"/>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
148 </Entity>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
149 </Schema>
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
150
79
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
151 =end code xml
2d1c3f713280 ORM concept in development
wizard
parents: 49
diff changeset
152
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 44
diff changeset
153 =cut