annotate Lib/IMPL/SQL/Schema/Table.pm @ 271:56364d0c4b4f

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