annotate Lib/IMPL/SQL/Schema/Table.pm @ 170:b88b7fe60aa3

refactoring
author sourcer
date Tue, 24 May 2011 01:11:16 +0400
parents 6148f89bb7bf
children 4d0e1962161c
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: 33
diff changeset
1 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
2 package IMPL::SQL::Schema::Table;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
3
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
4 use IMPL::lang qw(:declare :constants is);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
5
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
6 use parent qw(
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
7 IMPL::Object
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
8 IMPL::Object::Disposable
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
9 );
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
10
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
11 require IMPL::SQL::Schema::Column;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
12 require IMPL::SQL::Schema::Constraint;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
13 require IMPL::SQL::Schema::Constraint::PrimaryKey;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
14 require IMPL::SQL::Schema::Constraint::ForeignKey;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
15
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
16 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
17
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
18 BEGIN {
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
19 public _direct property name => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
20 public _direct property schema => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
21 public _direct property columns => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
22 public _direct property constraints => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
23 public _direct property columnsByName => 0;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
24 public _direct property primaryKey => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
25 public _direct property tag => PROP_ALL;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
26 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
27
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29 my ($this,%args) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
30
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
31 $this->{$name} = $args{'name'} or die new IMPL::InvalidArgumentException('a table name is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
32 $this->{$schema} = $args{'schema'} or die new IMPL::InvalidArgumentException('a parent schema is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
33
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
34 if ($args{columns}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
35 die new IMPL::InvalidOperationException('A columns property should be a reference to an array') unless ref $args{columns} eq 'ARRAY';
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
36
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
37 $this->InsertColumn($_) foreach @{$args{columns}};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
38 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
39
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
40 if ($args{constraints}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
41 die new IMPL::InvalidOperationException('A constraints property should be a reference to an array') unless ref $args{constraints} eq 'ARRAY';
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
42
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
43 $this->AddConstraint($_) foreach @{$args{constraints}};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
44 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
45 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
46
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
47 sub InsertColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48 my ($this,$column,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
50 $index = ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0) if not defined $index;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
51
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
52 die new IMPL::InvalidArgumentException("The index is out of range") if ($index < 0 || $index > ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
53
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
54 if (UNIVERSAL::isa($column,'IMPL::SQL::Schema::Column')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
55
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
56 } elsif (UNIVERSAL::isa($column,'HASH')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57 $column = new IMPL::SQL::Schema::Column(%{$column});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
58 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
59 die new IMPL::InvalidArgumentException("The invalid column parameter");
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
60 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
61
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
62 if (exists $this->{$columnsByName}->{$column->name}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
63 die new IMPL::InvalidOperationException("The column already exists",$column->name);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
64 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
65 $this->{$columnsByName}->{$column->name} = $column;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
66 splice @{$this->{$columns}},$index,0,$column;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
67 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
68
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
69 return $column;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
70 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
71
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
72 sub RemoveColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
73 my ($this,$NameOrColumn,$Force) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
74
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
75 my $ColName;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
76 if (UNIVERSAL::isa($NameOrColumn,'IMPL::SQL::Schema::Column')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
77 $ColName = $NameOrColumn->name;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
78 } elsif (not ref $NameOrColumn) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
79 $ColName = $NameOrColumn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
80 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
81
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
82 if (exists $this->{$columnsByName}->{$ColName}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
83 my $index = 0;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
84 foreach my $column(@{$this->{$columns}}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
85 last if $column->name eq $ColName;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
86 $index++;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
87 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
88
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
89 my $column = $this->{$columns}[$index];
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
90 if (my @constraints = $this->GetColumnConstraints($column)){
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
91 $Force or die new IMPL::InvalidOperationException('Can\'t remove column which is used in the constraints',@constraints);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
92 $this->RemoveConstraint($_) foreach @constraints;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
93 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
94
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
95 my $removed = splice @{$this->{$columns}},$index,1;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
96 delete $this->{$columnsByName}->{$ColName};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
97 return $removed;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
98 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
99 die new IMPL::InvalidOperationException("The column not found",$NameOrColumn->name);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
100 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
101 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
102
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
103 sub GetColumn {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
104 my ($this,$name) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
105
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
106 return $this->{$columnsByName}->{$name};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
107 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
108
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
109 sub GetColumnAt {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
110 my ($this,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
111
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
112 die new IMPL::InvalidArgumentException("The index is out of range")
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
113 if $index < 0 || $index >= ($this->{$columns} ? scalar(@{$this->{$columns}}) : 0);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
114
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
115 return $this->{$columns}[$index];
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
116 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
117
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
118 sub ColumnsCount {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
119 my ($this) = @_;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
120
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
121 return scalar(@{$this->{$columns}});
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
122 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
123
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
124 sub AddConstraint {
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
125 my $this = shift;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
126 if (@_ == 1) {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
127 my ($Constraint) = @_;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
128
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
129 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint);
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
130
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
131 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
132
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
133 if (exists $this->{$constraints}->{$Constraint->name}) {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
134 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name);
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
135 } else {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
136 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
137 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
138 $this->{$primaryKey} = $Constraint;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
139 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
140
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
141 $this->{$constraints}->{$Constraint->name} = $Constraint;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
142 }
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
143 } elsif( @_ == 2) {
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
144 my ($type,$params) = @_;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
145
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
146 $type = IMPL::SQL::Schema::Constraint->ResolveAlias($type) or
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
147 die new IMPL::Exception("Can't resolve a constraint alias",$_[0]);
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
148
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
149 $params->{table} = $this;
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
150
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
151 $this->AddConstraint($type->new(%$params));
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
152 } else {
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
153 die new IMPL::Exception("Wrong arguments number",scalar(@_));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
154 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
155 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
156
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
157 sub RemoveConstraint {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
158 my ($this,$Constraint,$Force) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
159
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
160 my $cn = UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint') ? $Constraint->name : $Constraint;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
161 $Constraint = $this->{$constraints}->{$cn} or die new IMPL::InvalidOperationException('The specified constraint doesn\'t exists',$cn);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
162
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
163 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
164 not scalar keys %{$this->{$primaryKey}->ConnectedFK} or die new IMPL::InvalidOperationException('Can\'t remove Primary Key unless some foreign keys referenses it');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
165
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
166 delete $this->{$primaryKey};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
167 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
168 $Constraint->Dispose;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
169 delete $this->{$constraints}->{$cn};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
170 return $cn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
171 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
172
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
173 sub GetConstraint {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
174 my ($this,$name) = @_;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
175
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
176 return $this->{$constraints}{$name};
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
177 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
178
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
179 sub GetConstraints {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
180 my ($this) = @_;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
181
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
182 return wantarray ? values %{$this->{$constraints}} : [values %{$this->{$constraints}}];
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
183 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
184
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
185 sub GetColumnConstraints {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
186 my ($this,@Columns) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
187
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
188 my @cn = map { UNIVERSAL::isa($_ ,'IMPL::SQL::Schema::Column') ? $_ ->name : $_ } @Columns;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
189 exists $this->{$columnsByName}->{$_} or die new IMPL::InvalidOperationException('The specified column isn\'t found',$_) foreach @cn;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
190
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
191 return grep {$_->HasColumn(@cn)} values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
192 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
193
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
194 sub SetPrimaryKey {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
195 my ($this,@ColumnList) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
196
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
197 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::PrimaryKey(name => $this->{$name}.'_PK', table => $this, columns => \@ColumnList));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
198 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
199
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
200 sub LinkTo {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
201 my ($this,$table,@ColumnList) = @_;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
202 $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
203 my $constraintName = $this->{$name}.'_'.$table->name.'_FK_'.join('_',map {ref $_ ? $_->name : $_} @ColumnList);
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
204 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(name => $constraintName, table => $this, columns => \@ColumnList, referencedTable => $table, referencedColumns => $table->primaryKey->columns->as_list));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
205 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
206
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
207 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
208 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
209
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
210 $_->Dispose() foreach values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
211
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
212 undef %{$this};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
213 $this->SUPER::Dispose();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
214 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
215
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
216 sub SameValue {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
217 my ($this,$other) = @_;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
218
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
219 return 0 unless is $other, typeof $this;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
220
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
221 return 0 unless $this->name eq $other->name;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
222 return 0 unless $this->ColumnsCount eq $other->ColumnsCount;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
223
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
224 for (my $i = 0; $i < $this->ColumsCount; $i ++) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
225 return 0 unless $this->($i)->SameValue($other->GetColumnAt($i));
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
226 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
227
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
228 my %thisConstraints = map { $_->name, $_ } $this->GetConstraints();
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
229 my %otherConstraints = map { $_->name, $_ } $other->GetConstraints();
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
230
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
231 foreach my $name ( keys %thisConstraints ) {
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
232 return 0 unless $otherConstraints{$name};
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
233 return 0 unless $thisConstraints{$name}->SameValue(delete $otherConstraints{$name});
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
234 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
235
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
236 return 0 if %otherConstraints;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
237
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
238 return 1;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
239 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
240
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
241 1;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
242
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
243