annotate Lib/IMPL/SQL/Schema/Table.pm @ 278:4ddb27ff4a0b

core refactoring
author cin
date Mon, 04 Feb 2013 02:10:37 +0400
parents 56364d0c4b4f
children 77df11605d3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
278
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
1 package IMPL::SQL::Schema::Table;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
2 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
3
278
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
4 use IMPL::lang qw(is);
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
5 use IMPL::Const qw(:prop);
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
6 use IMPL::declare {
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
7 base => [
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
8 'IMPL::Object' => undef,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
9 'IMPL::Object::Disposable' => undef
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
10 ],
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
11 props => [
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
12 name => PROP_RO | PROP_DIRECT,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
13 schema => PROP_RO | PROP_DIRECT,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
14 columns => PROP_RO | PROP_DIRECT,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
15 constraints => PROP_RO | PROP_DIRECT,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
16 columnsByName => PROP_RO | PROP_DIRECT,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
17 primaryKey => PROP_RO | PROP_DIRECT,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
18 tag => PROP_RW | PROP_DIRECT,
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
19 ]
4ddb27ff4a0b core refactoring
cin
parents: 271
diff changeset
20 };
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
21
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
22 require IMPL::SQL::Schema::Column;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
23 require IMPL::SQL::Schema::Constraint;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
24 require IMPL::SQL::Schema::Constraint::PrimaryKey;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
25 require IMPL::SQL::Schema::Constraint::ForeignKey;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
26
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
27 sub CTOR {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28 my ($this,%args) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
30 $this->{$name} = $args{'name'} or die new IMPL::InvalidArgumentException('a table name is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
31 $this->{$schema} = $args{'schema'} or die new IMPL::InvalidArgumentException('a parent schema is required');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
32
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
33 if ($args{columns}) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
34 die new IMPL::InvalidOperationException('A columns property should be a reference to an array') unless ref $args{columns} eq 'ARRAY';
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
35
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
36 $this->InsertColumn($_) foreach @{$args{columns}};
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
37 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
38 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
39
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
40 sub InsertColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
41 my ($this,$column,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
42
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
43 $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
44
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
45 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
46
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
47 if (UNIVERSAL::isa($column,'IMPL::SQL::Schema::Column')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49 } elsif (UNIVERSAL::isa($column,'HASH')) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
50 $column = new IMPL::SQL::Schema::Column(%{$column});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
51 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
52 die new IMPL::InvalidArgumentException("The invalid column parameter");
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
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
55 if (exists $this->{$columnsByName}->{$column->name}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
56 die new IMPL::InvalidOperationException("The column already exists",$column->name);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
58 $this->{$columnsByName}->{$column->name} = $column;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
59 splice @{$this->{$columns}},$index,0,$column;
49
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
62 return $column;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
63 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
64
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
65 sub RemoveColumn {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
66 my ($this,$NameOrColumn,$Force) = @_;
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 my $ColName;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
69 if (UNIVERSAL::isa($NameOrColumn,'IMPL::SQL::Schema::Column')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
70 $ColName = $NameOrColumn->name;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
71 } elsif (not ref $NameOrColumn) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
72 $ColName = $NameOrColumn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
73 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
74
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
75 if (exists $this->{$columnsByName}->{$ColName}) {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
76 my $index = 0;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
77 foreach my $column(@{$this->{$columns}}) {
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
78 last if $column->name eq $ColName;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
79 $index++;
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 my $column = $this->{$columns}[$index];
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
83 if (my @constraints = $this->GetColumnConstraints($column)){
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
84 $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
85 $this->RemoveConstraint($_) foreach @constraints;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
86 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
87
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
88 my $removed = splice @{$this->{$columns}},$index,1;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
89 delete $this->{$columnsByName}->{$ColName};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
90 return $removed;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
91 } else {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
92 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
93 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
94 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
95
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
96 sub GetColumn {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
97 my ($this,$name) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
98
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
99 return $this->{$columnsByName}->{$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
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
102 sub GetColumnAt {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
103 my ($this,$index) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
104
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
105 die new IMPL::InvalidArgumentException("The index is out of range")
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
106 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
107
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
108 return $this->{$columns}[$index];
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
109 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
110
269
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
111 sub SetColumnPosition {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
112 my ($this,$nameOrColumn,$pos) = @_;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
113
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
114 my $colName;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
115 if (is($nameOrColumn,'IMPL::SQL::Schema::Column')) {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
116 $colName = $nameOrColumn->name;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
117 } elsif (not ref $nameOrColumn) {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
118 $colName = $nameOrColumn;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
119 } else {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
120 die IMPL::InvalidArgumentException->new(column => 'The specified column isn\'t found in the table');
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
121 }
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
122
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
123 die IMPL::InvalidArgumentException->new( 'pos' => 'The specified position is invalid')
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
124 if not defined $pos || $pos < 0 || $pos >= $this->columnsCount;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
125
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
126 my $index = 0;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
127 foreach my $column(@{$this->{$columns}}) {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
128 last if $column->name eq $colName;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
129 $index++;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
130 }
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
131
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
132 if ($pos != $index) {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
133 #position needs to be changed;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
134
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
135 my ($column) = splice @{$this->{$columns}}, $index, 1;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
136 splice @{$this->{$columns}}, $pos, 0, $column;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
137 }
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
138
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
139 return;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
140 }
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
141
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
142 sub columnsCount {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
143 my ($this) = @_;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
144
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
145 return scalar(@{$this->{$columns}});
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
146 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
147
269
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
148 sub ColumnsCount {
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
149 goto &columnsCount;
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
150 }
dacfe7c0311a SQL schema updated (unstable)
sergey
parents: 232
diff changeset
151
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
152 sub AddConstraint {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
153 my $this = shift;
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
154 if (@_ == 1) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
155 my ($Constraint) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
156
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
157 die new IMPL::InvalidArgumentException('The invalid parameter') if not is($Constraint,typeof IMPL::SQL::Schema::Constraint);
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
158
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
159 $Constraint->table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
160
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
161 if (exists $this->{$constraints}->{$Constraint->name}) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
162 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
163 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
164 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
165 not $this->{$primaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
166 $this->{$primaryKey} = $Constraint;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
167 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
168
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
169 $this->{$constraints}->{$Constraint->name} = $Constraint;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
170 }
168
6148f89bb7bf IMPL::SQL::Schema::Traits::Diff alfa version
sourcer
parents: 167
diff changeset
171 } elsif( @_ == 2) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
172 my ($type,$params) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
173
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
174 $type = IMPL::SQL::Schema::Constraint->ResolveAlias($type) or
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
175 die new IMPL::Exception("Can't resolve a constraint alias",$_[0]);
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
176
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 269
diff changeset
177 $params = {%{$params}};
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents: 269
diff changeset
178
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
179 $params->{table} = $this;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
180
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
181 $this->AddConstraint($type->new(%$params));
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
182 } else {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
183 die new IMPL::Exception("Wrong arguments number",scalar(@_));
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
184 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
185 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
186
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
187 sub RemoveConstraint {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
188 my ($this,$Constraint,$Force) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
189
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
190 my $cn = UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint') ? $Constraint->name : $Constraint;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
191 $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
192
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
193 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
194 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
195
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
196 delete $this->{$primaryKey};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
197 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
198 $Constraint->Dispose;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
199 delete $this->{$constraints}->{$cn};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
200 return $cn;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
201 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
202
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
203 sub GetConstraint {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
204 my ($this,$name) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
205
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
206 return $this->{$constraints}{$name};
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
207 }
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
208
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
209 sub GetConstraints {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
210 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
211
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
212 return wantarray ? values %{$this->{$constraints}} : [values %{$this->{$constraints}}];
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
213 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
214
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
215 sub GetColumnConstraints {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
216 my ($this,@Columns) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
217
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
218 my @cn = map { UNIVERSAL::isa($_ ,'IMPL::SQL::Schema::Column') ? $_ ->name : $_ } @Columns;
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
219 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
220
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
221 return grep {$_->HasColumn(@cn)} values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
222 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
223
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
224 sub SetPrimaryKey {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
225 my ($this,@ColumnList) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
226
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
227 $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
228 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
229
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
230 sub LinkTo {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
231 my ($this,$table,@ColumnList) = @_;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
232 $table->primaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
233 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
234 $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
235 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
236
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
237 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
238 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
239
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
240 $_->Dispose() foreach values %{$this->{$constraints}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
241
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
242 undef %{$this};
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
243 $this->SUPER::Dispose();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
244 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
245
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
246 sub SameValue {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
247 my ($this,$other) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
248
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
249 return 0 unless is $other, typeof $this;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
250
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
251 return 0 unless $this->name eq $other->name;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
252 return 0 unless $this->ColumnsCount eq $other->ColumnsCount;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
253
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
254 for (my $i = 0; $i < $this->ColumsCount; $i ++) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
255 return 0 unless $this->($i)->SameValue($other->GetColumnAt($i));
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
256 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
257
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
258 my %thisConstraints = map { $_->name, $_ } $this->GetConstraints();
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
259 my %otherConstraints = map { $_->name, $_ } $other->GetConstraints();
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
260
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
261 foreach my $name ( keys %thisConstraints ) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
262 return 0 unless $otherConstraints{$name};
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
263 return 0 unless $thisConstraints{$name}->SameValue(delete $otherConstraints{$name});
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
264 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
265
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
266 return 0 if %otherConstraints;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
267
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
268 return 1;
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
269 }
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
270
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
271 1;
165
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
272
76515373dac0 Added Class::Template,
wizard
parents: 163
diff changeset
273