Mercurial > pub > Impl
annotate Lib/IMPL/SQL/Schema/Traits.pm @ 320:28eba7e0c592
*web application action: added method to access HTTP request header.
author | sergey |
---|---|
date | Thu, 16 May 2013 17:16:41 +0400 |
parents | 2f06250bab5f |
children |
rev | line source |
---|---|
165 | 1 package IMPL::SQL::Schema::Traits; |
49 | 2 use strict; |
163 | 3 use IMPL::_core::version; |
4 use IMPL::Exception(); | |
5 | |
165 | 6 use parent qw(IMPL::Object); |
7 | |
168 | 8 # required for use with typeof operator |
9 use IMPL::SQL::Schema::Constraint::PrimaryKey(); | |
10 use IMPL::SQL::Schema::Constraint::Index(); | |
11 use IMPL::SQL::Schema::Constraint::Unique(); | |
12 use IMPL::SQL::Schema::Constraint::ForeignKey(); | |
49 | 13 |
164 | 14 ################################################### |
15 | |
165 | 16 package IMPL::SQL::Schema::Traits::Table; |
17 use base qw(IMPL::Object::Fields); | |
163 | 18 |
164 | 19 use fields qw( |
194 | 20 name |
21 columns | |
22 constraints | |
23 options | |
164 | 24 ); |
25 | |
26 sub CTOR { | |
194 | 27 my ($this,$table,$columns,$constraints,$options) = @_; |
28 | |
29 $this->{name} = $table or die new IMPL::InvalidArgumentException(table => "A table name is required"); | |
30 $this->{columns} = $columns if defined $columns; | |
31 $this->{constraints} = $constraints if defined $constraints; | |
32 $this->{options} = $options if defined $options; | |
164 | 33 } |
34 | |
35 ################################################### | |
36 | |
165 | 37 package IMPL::SQL::Schema::Traits::Column; |
38 use base qw(IMPL::Object::Fields); | |
164 | 39 |
40 use fields qw( | |
194 | 41 name |
42 type | |
43 isNullable | |
44 defaultValue | |
45 tag | |
164 | 46 ); |
47 | |
48 sub CTOR { | |
194 | 49 my ($this, $name, $type, %args) = @_; |
50 | |
51 $this->{name} = $name or die new IMPL::InvalidArgumentException("name"); | |
52 $this->{type} = $type or die new IMPL::InvalidArgumentException("type"); | |
53 $this->{isNullable} = $args{isNullable} if exists $args{isNullable}; | |
54 $this->{defaultValue} = $args{defaultValue} if exists $args{defaultValue}; | |
55 $this->{tag} = $args{tag} if exists $args{tag}; | |
164 | 56 } |
57 | |
58 ################################################## | |
59 | |
165 | 60 package IMPL::SQL::Schema::Traits::Constraint; |
61 use base qw(IMPL::Object::Fields); | |
167 | 62 |
164 | 63 use fields qw( |
194 | 64 name |
65 columns | |
164 | 66 ); |
67 | |
68 sub CTOR { | |
194 | 69 my ($this, $name, $columns) = @_; |
70 | |
71 $this->{name} = $name; | |
72 $this->{columns} = $columns; # list of columnNames | |
168 | 73 } |
74 | |
75 sub constraintClass { | |
194 | 76 die new IMPL::NotImplementedException(); |
164 | 77 } |
78 | |
79 ################################################## | |
80 | |
165 | 81 package IMPL::SQL::Schema::Traits::PrimaryKey; |
164 | 82 |
165 | 83 use base qw(IMPL::SQL::Schema::Traits::Constraint); |
164 | 84 |
85 __PACKAGE__->PassThroughArgs; | |
86 | |
168 | 87 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::PrimaryKey }; |
88 | |
164 | 89 ################################################## |
90 | |
165 | 91 package IMPL::SQL::Schema::Traits::Index; |
164 | 92 |
165 | 93 use base qw(IMPL::SQL::Schema::Traits::Constraint); |
164 | 94 |
95 __PACKAGE__->PassThroughArgs; | |
96 | |
168 | 97 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::Index }; |
98 | |
164 | 99 ################################################## |
100 | |
165 | 101 package IMPL::SQL::Schema::Traits::Unique; |
164 | 102 |
165 | 103 use base qw(IMPL::SQL::Schema::Traits::Constraint); |
164 | 104 |
105 __PACKAGE__->PassThroughArgs; | |
163 | 106 |
168 | 107 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::Unique }; |
108 | |
164 | 109 ################################################## |
110 | |
165 | 111 package IMPL::SQL::Schema::Traits::ForeignKey; |
164 | 112 |
165 | 113 use base qw(IMPL::SQL::Schema::Traits::Constraint); |
164 | 114 use fields qw( |
194 | 115 foreignTable |
116 foreignColumns | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
117 onUpdate |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
118 onDelete |
164 | 119 ); |
120 | |
168 | 121 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::ForeignKey }; |
122 | |
164 | 123 our %CTOR = ( |
194 | 124 'IMPL::SQL::Schema::Traits::Constraint' => sub { @_[0..1] } |
164 | 125 ); |
126 | |
127 sub CTOR { | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
128 my ($this,$foreignTable,$foreignColumns,%args) = @_[0,3..$#_]; |
194 | 129 |
130 $this->{foreignTable} = $foreignTable; | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
131 $this->{foreignColumns} = $foreignColumns; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
132 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
133 $this->{onDelete} = $args{onDelete} if $args{onDelete}; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
134 $this->{onUpdate} = $args{onUpdate} if $args{onUpdate}; |
164 | 135 } |
136 | |
137 | |
138 ################################################## | |
139 | |
165 | 140 package IMPL::SQL::Schema::Traits::CreateTable; |
164 | 141 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
142 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
143 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
144 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
145 Table => '-IMPL::SQL::Schema::Traits::Table', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
146 ArgException => '-IMPL::InvalidArgumentException', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
147 OpException => '-IMPL::InvalidOperationException' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
148 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
149 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
150 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
151 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
152 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
153 table => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
154 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
155 }; |
164 | 156 use IMPL::lang; |
157 | |
158 sub CTOR { | |
194 | 159 my ($this,$table) = @_; |
160 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
161 die ArgException->new("table", "An object of IMPL::SQL::Schema::Traits::Table type is required") |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
162 unless is($table, Table); |
194 | 163 |
164 $this->table($table); | |
164 | 165 } |
166 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
167 sub CanApply { |
194 | 168 my ($this,$schema) = @_; |
169 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
170 return( $schema->GetTable( $this->table->{name} ) ? 0 : 1 ); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
171 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
172 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
173 sub Apply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
174 my ($this,$schema) = @_; |
194 | 175 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
176 my $args = {%{$this->table}}; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
177 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
178 my $constraints = delete $args->{constraints} || []; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
179 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
180 my $table = $schema->AddTable($args); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
181 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
182 $table->AddConstraint($_->constraintClass, $_) foreach @{$constraints}; |
164 | 183 } |
184 | |
185 ################################################## | |
186 | |
165 | 187 package IMPL::SQL::Schema::Traits::DropTable; |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
188 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
189 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
190 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
191 ArgException => '-IMPL::InvalidArgumentException' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
192 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
193 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
194 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
195 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
196 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
197 tableName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
198 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
199 }; |
164 | 200 |
201 sub CTOR { | |
194 | 202 my ($this,$tableName) = @_; |
203 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
204 $this->tableName($tableName) or die ArgException->new("tableName is required"); |
164 | 205 } |
206 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
207 sub CanApply { |
194 | 208 my ($this,$schema) = @_; |
209 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
210 return $schema->GetTable( $this->tableName ) ? 1 : 0; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
211 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
212 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
213 sub Apply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
214 my ($this,$schema) = @_; |
194 | 215 |
216 $schema->RemoveTable($this->tableName); | |
164 | 217 } |
218 | |
219 ################################################## | |
220 | |
165 | 221 package IMPL::SQL::Schema::Traits::RenameTable; |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
222 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
223 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
224 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
225 ArgException => '-IMPL::InvalidArgumentException' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
226 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
227 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
228 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
229 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
230 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
231 tableName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
232 tableNewName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
233 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
234 }; |
164 | 235 |
236 sub CTOR { | |
194 | 237 my ($this, $oldName, $newName) = @_; |
238 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
239 $this->tableName($oldName) or die ArgException->new("A table name is required"); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
240 $this->tableNewName($newName) or die ArgException->new("A new table name is required"); |
164 | 241 } |
242 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
243 sub CanApply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
244 my ($this, $schema) = @_; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
245 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
246 return ($schema->GetTable($this->tableName) and not $schema->GetTable($this->tableNewName) ? 1 : 0 ); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
247 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
248 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
249 sub Apply { |
194 | 250 my ($this,$schema) = @_; |
251 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
252 $schema->RenameTable($this->tableName, $this->tableNewName); |
194 | 253 |
164 | 254 } |
255 | |
256 ################################################# | |
257 | |
165 | 258 package IMPL::SQL::Schema::Traits::AlterTableAddColumn; |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
259 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
260 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
261 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
262 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
263 Column => '-IMPL::SQL::Schema::Traits::Column', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
264 ArgException => '-IMPL::InvalidArgumentException', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
265 OpException => '-IMPL::InvalidOperationException' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
266 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
267 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
268 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
269 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
270 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
271 tableName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
272 column => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
273 position => PROP_RO |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
274 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
275 }; |
164 | 276 use IMPL::lang; |
277 | |
49 | 278 |
164 | 279 sub CTOR { |
194 | 280 my ($this,$tableName,$column) = @_; |
281 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
282 $this->tableName($tableName) or die ArgException->new("A table name is required"); |
194 | 283 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
284 die ArgException->new("A column should be a IMPL::SQL::Schema::Traits::Column object") |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
285 unless is($column, Column); |
194 | 286 |
287 $this->column($column); | |
164 | 288 } |
289 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
290 sub CanApply { |
194 | 291 my ($this,$schema) = @_; |
292 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
293 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
294 or return 0; |
194 | 295 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
296 return $table->GetColumn( $this->column->{name} ) ? 0 : 1; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
297 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
298 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
299 sub Apply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
300 my ($this,$schema) = @_; |
194 | 301 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
302 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
303 or die OpException->new("The specified table doesn't exists", $this->tableName); |
194 | 304 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
305 if ($this->position) { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
306 $table->AddColumn($this->column); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
307 } else { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
308 $table->InsertColumn($this->column,$this->position); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
309 } |
164 | 310 } |
311 | |
312 ################################################# | |
313 | |
165 | 314 package IMPL::SQL::Schema::Traits::AlterTableDropColumn; |
164 | 315 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
316 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
317 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
318 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
319 FK => '-IMPL::SQL::Schema::Constraint::ForeignKey', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
320 ArgException => '-IMPL::InvalidArgumentException', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
321 OpException => '-IMPL::InvalidOperationException' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
322 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
323 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
324 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
325 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
326 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
327 tableName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
328 columnName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
329 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
330 }; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
331 use IMPL::lang; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
332 |
164 | 333 |
334 sub CTOR { | |
194 | 335 my ($this,$table,$column) = @_; |
336 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
337 $this->tableName($table) or die ArgException->new(tableName => "A table name should be specified"); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
338 $this->columnName($column) or die ArgException->new(columnName => "A column name should be specified"); |
164 | 339 } |
340 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
341 sub CanApply { |
194 | 342 my ($this,$schema) = @_; |
343 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
344 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
345 or return 0; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
346 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
347 $table->GetColumn($this->columnName) or |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
348 return 0; |
194 | 349 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
350 # столбец |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
351 return $table->GetColumnConstraints($this->columnName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
352 ? 0 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
353 : 1 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
354 ; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
355 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
356 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
357 sub Apply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
358 my ($this,$schema) = @_; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
359 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
360 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
361 or die OpException->new("The specified table doesn't exists", $this->tableName); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
362 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
363 $table->RemoveColumn($this->columnName); |
49 | 364 } |
365 | |
164 | 366 ################################################# |
163 | 367 |
165 | 368 package IMPL::SQL::Schema::Traits::AlterTableChangeColumn; |
49 | 369 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
370 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
371 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
372 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
373 Constraint => '-IMPL::SQL::Schema::Traits::Constraint', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
374 ArgException => '-IMPL::InvalidArgumentException', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
375 OpException => '-IMPL::InvalidOperationException' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
376 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
377 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
378 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
379 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
380 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
381 tableName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
382 columnName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
383 columnType => PROP_RW, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
384 defaultValue => PROP_RW, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
385 isNullable => PROP_RW, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
386 position => PROP_RW, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
387 options => PROP_RW # hash diff format, (keys have a prefix '+' - add or update value, '-' remove value) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
388 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
389 }; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
390 use IMPL::lang; |
49 | 391 |
164 | 392 sub CTOR { |
194 | 393 my ($this, $table,$column,%args) = @_; |
394 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
395 $this->tableName($table) or die ArgException->new(tableName => "A table name is required"); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
396 $this->columnName($column) or die ArgException->new(columnName => "A column name is required"); |
194 | 397 |
398 $this->$_($args{$_}) | |
399 for (grep exists $args{$_}, qw(columnType defaultValue isNullable options)); | |
164 | 400 } |
49 | 401 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
402 sub CanApply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
403 my ($this,$schema) = @_; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
404 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
405 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
406 or return 0; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
407 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
408 return $table->GetColumn($this->columnName) ? 1 : 0; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
409 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
410 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
411 sub Apply { |
194 | 412 my ($this,$schema) = @_; |
413 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
414 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
415 or die OpException->new("The specified table doesn't exists", $this->tableName); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
416 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
417 my $column = $table->GetColumn($this->columnName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
418 or die OpException->new("The specified column doesn't exists", $this->tableName, $this->columnName); |
194 | 419 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
420 $column->SetType($this->columnType) if defined $this->columnType; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
421 $column->SetNullable($this->isNullable) if defined $this->isNullable; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
422 $column->SetDefaultValue($this->defaultValue) if defined $this->defaultValue; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
423 $column->SetOptions($this->options) if defined $this->options; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
424 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
425 $table->SetColumnPosition($this->position) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
426 if ($this->position); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
427 |
164 | 428 } |
49 | 429 |
164 | 430 ################################################# |
163 | 431 |
165 | 432 package IMPL::SQL::Schema::Traits::AlterTableAddConstraint; |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
433 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
434 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
435 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
436 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
437 Constraint => '-IMPL::SQL::Schema::Traits::Constraint', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
438 ArgException => '-IMPL::InvalidArgumentException', |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
439 FK => '-IMPL::SQL::Schema::Traits::ForeignKey' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
440 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
441 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
442 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
443 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
444 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
445 tableName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
446 constraint => PROP_RO |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
447 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
448 }; |
164 | 449 use IMPL::lang; |
49 | 450 |
164 | 451 sub CTOR { |
194 | 452 my ($this,$table,$constraint) = @_; |
453 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
454 $this->tableName($table) or die ArgException->new( tableName => "A table name is required"); |
194 | 455 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
456 die ArgException->new(constaraint => "A valid " . Constraint . " is required") |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
457 unless is($constraint, Constraint); |
194 | 458 |
459 $this->constraint($constraint); | |
164 | 460 } |
49 | 461 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
462 sub CanApply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
463 my ($this, $schema) = @_; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
464 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
465 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
466 or return 0; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
467 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
468 my $constraint = $this->constraint; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
469 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
470 my @columns = map $table->GetColumn($_), @{$constraint->{columns} || []}; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
471 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
472 # проверяем, что в таблице есть все столбцы для создания ограничения |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
473 return 0 if grep not($_), @columns; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
474 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
475 if (is($constraint,FK)) { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
476 my $foreignTable = $schema->GetTable($constraint->{foreignTable}) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
477 or return 0; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
478 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
479 my @foreignColumns = map $foreignTable->GetColumn($_), @{$constraint->{foreignColumns}||[]}; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
480 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
481 # внешняя таблица имеет нужные столбцы |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
482 return 0 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
483 if grep not($_), @foreignColumns; |
272 | 484 |
485 # типы столбцов во внешней таблице совпадают с типами столбцов ограничения | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
486 return 0 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
487 if grep not($columns[$_]->type->SameValue($foreignColumns[$_]->type)), (0 .. $#columns); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
488 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
489 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
490 return 1; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
491 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
492 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
493 sub Apply { |
194 | 494 my ($this,$schema) = @_; |
495 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
496 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
497 or die IMPL::InvalidOperationException->new("The specified table doesn't exists", $this->tableName); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
498 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
499 my $constraint = $this->constraint; |
194 | 500 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
501 if (is($constraint,FK)) { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
502 my $args = { %$constraint }; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
503 $args->{referencedTable} = $schema->GetTable(delete $args->{foreignTable}); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
504 $args->{referencedColumns} = delete $args->{foreignColumns}; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
505 $table->AddConstraint($constraint->constraintClass, $args); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
506 } else { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
507 $table->AddConstraint($constraint->constraintClass, $constraint); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
508 } |
194 | 509 |
164 | 510 } |
49 | 511 |
164 | 512 ################################################# |
49 | 513 |
165 | 514 package IMPL::SQL::Schema::Traits::AlterTableDropConstraint; |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
515 use IMPL::Const qw(:prop); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
516 use IMPL::declare { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
517 require => { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
518 PK => '-IMPL::SQL::Schema::Constraint::PrimaryKey' |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
519 }, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
520 base => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
521 '-IMPL::SQL::Schema::Traits' => undef |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
522 ], |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
523 props => [ |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
524 tableName => PROP_RO, |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
525 constraintName => PROP_RO |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
526 ] |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
527 }; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
528 use IMPL::lang qw(is); |
163 | 529 |
164 | 530 sub CTOR { |
194 | 531 my ($this,$table,$constraint) = @_; |
532 | |
533 die new IMPL::InvalidArgumentException( tableName => "A table name is required" ) unless $table; | |
534 die new IMPL::InvalidArgumentException( constraintName => "A constraint name is required" ) unless $constraint; | |
535 | |
536 $this->tableName($table); | |
537 $this->constraintName($constraint); | |
164 | 538 } |
49 | 539 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
540 sub CanApply { |
194 | 541 my ($this,$schema) = @_; |
542 | |
283
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
272
diff
changeset
|
543 my $table = $schema->GetTable($this->tableName) |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
272
diff
changeset
|
544 or return 0; |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
545 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
546 my $constraint = $table->GetConstraint($this->constraintName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
547 or return 0; |
194 | 548 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
549 # есть ли внешние ключи на данную таблицу |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
550 return ( |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
551 is($constraint,PK) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
552 && values( %{$constraint->connectedFK || {}} ) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
553 ? 0 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
554 : 1 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
555 ); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
556 } |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
557 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
558 sub Apply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
559 my ($this,$schema) = @_; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
560 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
561 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
562 or die IMPL::InvalidOperationException->new("The specified table doesn't exists", $this->tableName); |
194 | 563 |
564 $table->RemoveConstraint($this->constraintName); | |
164 | 565 } |
163 | 566 |
49 | 567 |
568 1; | |
163 | 569 |
570 __END__ | |
571 | |
572 =pod | |
573 | |
574 =head1 NAME | |
575 | |
180 | 576 C<IMPL::SQL::Traits> - Операции над объектками SQL схемы. |
163 | 577 |
578 =head1 DESCRIPTION | |
579 | |
180 | 580 Изменения схемы могу быть представлены в виде последовательности примитивных операций. |
581 Правила выполнения последовательности примитывных действий могут варьироваться | |
582 в зависимости от процессора, который их выполняет. Например C<IMPL::SQL::Schema::Traits::Processor>. | |
163 | 583 |
180 | 584 Данные, которые содержаться в примитивных операциях не могут существовать независимо от схемы. |
164 | 585 |
269 | 586 =head1 OPERATIONS |
164 | 587 |
588 =head2 General | |
589 | |
180 | 590 Методы обще для всех примитивных операций. |
164 | 591 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
592 =head3 C<CanApply($schema)> |
164 | 593 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
594 Определяет возможность применения операции к указанной схеме. |
164 | 595 |
180 | 596 Возвращаемое значение: |
164 | 597 |
598 =over | |
599 | |
600 =item C<true> | |
601 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
602 Операция приминима к схеме. |
164 | 603 |
604 =item C<false> | |
605 | |
180 | 606 Операция не может быть применена к схеме. |
164 | 607 |
608 =back | |
609 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
610 =head3 C<Apply($schema)> |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
611 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
612 Применяет операцию к указанной схеме. |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
613 |
164 | 614 =head2 Primitive operations |
615 | |
269 | 616 =head3 C<IMPL::SQL::Schema::Traits::CreateTable> |
164 | 617 |
180 | 618 Создает таблицу |
164 | 619 |
269 | 620 =head4 C<CTOR($table)> |
164 | 621 |
269 | 622 =head4 C<[get]table> |
164 | 623 |
180 | 624 C<IMPL::SQL::Schema::Traits::Table> - описание создаваемой таблицы |
164 | 625 |
269 | 626 =head3 C<IMPL::SQL::Schema::Traits::DropTable> |
164 | 627 |
180 | 628 Удалает таблицу по имени |
164 | 629 |
269 | 630 =head4 C<CTOR($tableName)> |
164 | 631 |
269 | 632 =head4 C<[get]tableName> |
164 | 633 |
180 | 634 Имя удаляемой таблицы |
164 | 635 |
269 | 636 =head3 C<IMPL::SQL::Schema::Traits::RenameTable> |
164 | 637 |
269 | 638 =head4 C<CTOR($tableName,$tableNewName)> |
164 | 639 |
269 | 640 =head4 C<[get]tableName> |
164 | 641 |
180 | 642 Имя таблицы, которую требуется переименовать |
164 | 643 |
269 | 644 =head4 C<[get]tableNewName> |
164 | 645 |
180 | 646 Новое имя таблицы |
164 | 647 |
269 | 648 =head3 C<IMPL::SQL::Schema::Traits::AlterTableAddColumn> |
164 | 649 |
180 | 650 Добавляет столбец в таблицу |
164 | 651 |
269 | 652 =head4 C<CTOR($tableName,$column,$position)> |
164 | 653 |
269 | 654 =head4 C<[get]tableName> |
164 | 655 |
180 | 656 Имя таблицы в которую нужно добавить столбец |
164 | 657 |
269 | 658 =head4 C<[get]column> |
164 | 659 |
269 | 660 C<IMPL::SQL::Schema::Traits::Column> - описание столбца который нужно добавить |
164 | 661 |
269 | 662 =head4 C<[get]position> |
164 | 663 |
269 | 664 Позиция на которую нужно вставить столбец |
665 | |
666 =head3 C<IMPL::SQL::Schema::Traits::AlterTableDropColumn> | |
164 | 667 |
180 | 668 Удаляет столбец из таблицы |
164 | 669 |
269 | 670 =head4 C<CTOR($tableName,$columnName)> |
164 | 671 |
269 | 672 =head4 C<[get]tableName> |
164 | 673 |
180 | 674 Имя таблицы в которой нужно удалить столбец |
164 | 675 |
269 | 676 =head4 C<[get]columnName> |
164 | 677 |
180 | 678 Имя столбца для удаления |
164 | 679 |
269 | 680 =head3 C<IMPL::SQL::Schema::Traits::AlterTableChangeColumn> |
164 | 681 |
180 | 682 Меняет описание столбца |
164 | 683 |
269 | 684 =head4 C<CTOR($tableName,$columnName,%args)> |
164 | 685 |
180 | 686 C<%args> - хеш, ключами которого являются оставшиеся свойства создаваемого объекта. |
164 | 687 |
269 | 688 =head4 C<[get]tableName> |
164 | 689 |
180 | 690 Имя таблицы в которой находится столбец. |
164 | 691 |
269 | 692 =head4 C<[get]columnName> |
164 | 693 |
180 | 694 Имя столбца для изменения |
164 | 695 |
269 | 696 =head4 C<[get]columnType> |
164 | 697 |
180 | 698 Новый тип столбца. Не задан, если тип не меняется |
164 | 699 |
269 | 700 =head4 C<[get]defaultValue> |
164 | 701 |
180 | 702 Значение по умолчанию. Не задано, если не меняется |
164 | 703 |
269 | 704 =head4 C<[get]isNullable> |
164 | 705 |
180 | 706 Может ли столбец содержать C<NULL>. Не задано, если не меняется. |
164 | 707 |
269 | 708 =head4 C<[get]options> |
164 | 709 |
269 | 710 Хеш опций, не задан, если опции не меняются. Данный хеш содержит разничу между |
711 старыми и новыми значениями свойства C<tag> столбца. | |
164 | 712 |
269 | 713 |
714 =head3 C<IMPL::SQL::Schema::Traits::AlterTableAddConstraint> | |
164 | 715 |
180 | 716 Базовый класс для операций по добавлению ограничений |
164 | 717 |
269 | 718 =head4 C<CTOR($tableName,$constraint)> |
164 | 719 |
269 | 720 =head4 C<[get]tableName> |
164 | 721 |
180 | 722 Имя таблицы в которую добавляется ограничение. |
164 | 723 |
269 | 724 =head4 C<[get]constraint> |
164 | 725 |
180 | 726 C<IMPL::SQL::Schema::Traits::Constraint> - описние ограничения, которое нужно добавить. |
164 | 727 |
269 | 728 =head3 C<IMPL::SQL::Schema::Traits::AlterTableDropConstraint> |
164 | 729 |
180 | 730 Удаляет ограничение на таблицу |
164 | 731 |
269 | 732 =head4 C<CTOR($tableName,$constraintName)> |
164 | 733 |
269 | 734 =head4 C<[get]tableName> |
164 | 735 |
180 | 736 Имя таблицы в которой требуется удалить ограничение. |
164 | 737 |
269 | 738 =head4 C<[get]constraintName> |
164 | 739 |
180 | 740 Имя ограничения для удаления. |
164 | 741 |
180 | 742 =cut |