comparison Lib/IMPL/ORM/Schema/TransformToSQL.pm @ 44:32d2350fccf9

ORM *Minor fixes *Working tarnsform to sql *Fixes to the sql traits
author Sergey
date Mon, 11 Jan 2010 01:42:00 +0300
parents d660fb38b7cc
children 16ada169ca75
comparison
equal deleted inserted replaced
43:009aa9ca2e48 44:32d2350fccf9
17 ORMSchema => \&ORMSchemaTransform, 17 ORMSchema => \&ORMSchemaTransform,
18 Entity => \&EntityTransform, 18 Entity => \&EntityTransform,
19 Field => \&FieldTransform, 19 Field => \&FieldTransform,
20 HasOne => \&HasOneTransform, 20 HasOne => \&HasOneTransform,
21 HasMany => \&HasManyTransform, 21 HasMany => \&HasManyTransform,
22 Subclass => \&SubclassTransform 22 Subclass => \&SubclassTransform,
23 ValueType => sub {}
23 } 24 }
24 ); 25 );
25 26
26 sub CTOR { 27 sub CTOR {
27 my ($this,$refTypeMap) = @_; 28 my ($this,$refTypeMap) = @_;
37 my @constraints; 38 my @constraints;
38 39
39 my %ctx = (Schema => $schema); 40 my %ctx = (Schema => $schema);
40 41
41 # all tables 42 # all tables
42 foreach my $entity ($node->ReferenceTypes) { 43 foreach my $entity ($node->selectNodes('Entity')) {
43 $schema->AddTable($this->Transform($entity,\%ctx)); 44 $schema->AddTable($this->Transform($entity,\%ctx));
44 push @constraints, $entity->selectNodes(sub {$_->isa('IMPL::ORM::Schema::Relation')}); 45 push @constraints, $entity->selectNodes(sub {$_->isa('IMPL::ORM::Schema::Relation')});
45 } 46 }
46 47
47 # establish relations 48 # establish relations
78 my $sqlSchema = $ctx->{Schema}; 79 my $sqlSchema = $ctx->{Schema};
79 my $table = $sqlSchema->Tables->{$relation->parentNode->entityName}; 80 my $table = $sqlSchema->Tables->{$relation->parentNode->entityName};
80 my $tableForeign = $sqlSchema->Tables->{$relation->target}; 81 my $tableForeign = $sqlSchema->Tables->{$relation->target};
81 my $prefix = $relation->name; 82 my $prefix = $relation->name;
82 83
83 my @fkColumns = map 84 my @fkColumns = @{$tableForeign->PrimaryKey->Columns};
85
86 if (@fkColumns > 1) {
87 @fkColumns = map
84 $table->InsertColumn({ 88 $table->InsertColumn({
85 Name => $prefix . $_->Name, 89 Name => $prefix . $_->Name,
86 Type => $_->Type, 90 Type => $_->Type,
87 CanBeNull => 1 91 CanBeNull => 1
88 }), 92 }), @fkColumns;
89 @{$tableForeign->PrimaryKey->Columns}; 93 } else {
90 94 @fkColumns = $table->InsertColumn({
95 Name => $prefix,
96 Type => $fkColumns[0]->Type,
97 CanBeNull => 1
98 });
99 }
100
91 $table->LinkTo($tableForeign,@fkColumns); 101 $table->LinkTo($tableForeign,@fkColumns);
92 } 102 }
93 103
94 sub HasManyTransform { 104 sub HasManyTransform {
95 my ($this,$relation,$ctx) = @_; 105 my ($this,$relation,$ctx) = @_;
97 #similar to HasOne 107 #similar to HasOne
98 108
99 my $sqlSchema = $ctx->{Schema}; 109 my $sqlSchema = $ctx->{Schema};
100 my $table = $sqlSchema->Tables->{$relation->parentNode->entityName}; 110 my $table = $sqlSchema->Tables->{$relation->parentNode->entityName};
101 my $tableForeign = $sqlSchema->Tables->{$relation->target}; 111 my $tableForeign = $sqlSchema->Tables->{$relation->target};
102 my $prefix = $table->Name . '_' . $relation->name; 112 my $prefix = $relation->name;
103 113
104 my @fkColumns = map 114 my @fkColumns = @{$table->PrimaryKey->Columns};
105 $tableForeign->InsertColumn({ 115
116 if (@fkColumns > 1 ) {
117 @fkColumns = map $tableForeign->InsertColumn({
106 Name => $prefix . $_->Name, 118 Name => $prefix . $_->Name,
107 Type => $_->Type, 119 Type => $_->Type,
108 CanBeNull => 1 120 CanBeNull => 1
109 }), 121 }), @fkColumns;
110 @{$table->PrimaryKey->Columns}; 122 } else {
123 @fkColumns = $tableForeign->InsertColumn({
124 Name => $prefix,
125 Type => $fkColumns[0]->Type,
126 CanBeNull => 1
127 });
128 }
111 129
112 $tableForeign->LinkTo($table,@fkColumns); 130 $tableForeign->LinkTo($table,@fkColumns);
113 } 131 }
114 132
115 sub SubclassTransform { 133 sub SubclassTransform {
116 # actually this rlations has only ligical implementation 134 # actually this rlations has only logical implementation
117 } 135 }
118 136
119 sub MapType { 137 sub MapType {
120 my ($this,$typeName) = @_; 138 my ($this,$typeName) = @_;
121 139