annotate Lib/IMPL/ORM/Schema.pm @ 38:d660fb38b7cc

small fixes ORM shema to SQL schema transformation
author Sergey
date Mon, 23 Nov 2009 17:57:07 +0300
parents d59526f6310e
children 009aa9ca2e48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
1 package IMPL::ORM::Schema;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
2 use strict;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
3 use warnings;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
4
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
5 use base qw(IMPL::DOM::Document);
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
6 use IMPL::Class::Property;
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
7 require IMPL::ORM::Schema::Entity;
38
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
8 require IMPL::ORM::Schema::ValueType;
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
9
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
10 our %CTOR = (
38
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
11 'IMPL::DOM::Document' => sub { nodeName => 'ORMSchema' }
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
12 );
28
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
13
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
14 BEGIN {
38
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
15 private property mapValueTypes => prop_all;
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
16 private property mapReferenceTypes => prop_all;
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
17 private property mapPending => prop_all;
31
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
18 public property prefix => prop_get | owner_set;
28
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
19 }
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
20
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
21 sub CTOR {
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
22 my ($this ) = @_;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
23 $this->mapValueTypes({});
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
24 $this->mapReferenceTypes({});
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
25 $this->mapPending({});
28
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
26 }
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
27
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
28 # return an entity for the specified typename
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
29 # makes forward declaration if nesessary
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
30 sub resolveType {
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
31 my ($this,$typeName) = @_;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
32
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
33 $this = ref $this ? $this : $this->instance;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
34
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
35 if (my $entity = $this->mapReferenceTypes->{$typeName}) {
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
36 return $entity;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
37 } elsif (UNIVERSAL::isa($typeName,'IMPL::ORM::Object')) {
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
38 return $this->declareReferenceType($typeName);
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
39 } else {
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
40 return undef;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
41 }
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
42 }
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
43
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
44 sub declareReferenceType {
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
45 my ($this,$typeName) = @_;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
46
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
47 my $entity = new IMPL::ORM::Schema::Entity($typeName);
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
48
38
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
49 $this->appendChild($entity);
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
50
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
51 $this->mapPending->{$typeName} = $entity;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
52
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
53 return $this->mapReferenceTypes->{$typeName} = $entity;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
54 }
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
55
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
56 sub _addReferenceType {
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
57 my ($this,$className) = @_;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
58
38
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
59 $this->mapReferenceTypes->{$className} = $className->ormGetSchema($this,delete $this->mapPending->{$className} || $this->appendChild(new IMPL::ORM::Schema::Entity($className)));
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
60 }
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
61
28
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
62 # returns valuetype name
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
63 sub isValueType {
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
64 my ($this,$typeName) = @_;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
65
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
66 $this = ref $this ? $this : $this->instance;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
67
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
68 return $this->mapValueTypes->{$typeName};
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
69 }
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
70
38
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
71 sub ReferenceTypes {
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
72 my ($this) = @_;
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
73
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
74 values %{$this->mapReferenceTypes};
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
75 }
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
76
28
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
77 my %instances;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
78 sub instance {
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
79 my ($class) = @_;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
80
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
81 return ($instances{$class} || ($instances{$class} = $class->new));
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
82 }
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
83
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
84 sub ValueTypes {
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
85 my ($this,%classes) = @_;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
86
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
87 $this = ref $this ? $this : $this->instance;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
88
38
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
89 $this->mapValueTypes->{$_} = $this->appendChild(
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
90 IMPL::ORM::Schema::ValueType->new(
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
91 name => $_,
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
92 mapper => $classes{$_}
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
93 )
d660fb38b7cc small fixes
Sergey
parents: 31
diff changeset
94 ) foreach keys %classes;
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
95 }
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
96
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
97 sub Classes {
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
98 my ($this,@classNames) = @_;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
99
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
100 $this = ref $this ? $this : $this->instance;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
101
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
102 $this->_addReferenceType($this->prefix . $_) foreach @classNames;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
103 }
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
104
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
105 sub usePrefix {
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
106 my ($this,$prefix) = @_;
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
107
31
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
108 $prefix .= '::' if $prefix and $prefix !~ /::$/;
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
109
30
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
110 (ref $this ? $this : $this->instance)->prefix($prefix);
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
111 }
dd4d72600c69 ORM in works
Sergey
parents: 28
diff changeset
112
31
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
113 sub CompleteSchema {
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
114 my ($this) = @_;
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
115
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
116 $this = ref $this ? $this : $this->instance;
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
117
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
118 $this->mapReferenceTypes->{$_} = $_->ormGetSchema($this,delete $this->mapPending->{$_})
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
119 foreach (keys %{$this->mapPending});
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
120 }
d59526f6310e Small fixes to Test framework (correct handlinf of the compilation errors in the test units)
Sergey
parents: 30
diff changeset
121
28
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
122 1;
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
123
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
124 __END__
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
125
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
126 =pod
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
127
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
128 =head1 DESCRIPTION
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
129
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
130 Схема данных, представляет собой DOM документ, элементами которой
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
131 являются сущности.
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
132
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
133 Каждый узел - это описание сущности.
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
134
6d33f75c6e1f ORM in works
Sergey
parents:
diff changeset
135 =cut