Mercurial > pub > Impl
annotate Lib/IMPL/SQL/Schema/Traits.pm @ 282:68d905f8dc43
*IMPL::SQL fixed warnings
author | cin |
---|---|
date | Tue, 12 Feb 2013 01:24:36 +0400 |
parents | 47db27ed5b43 |
children | 2f06250bab5f |
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 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
543 my $table = $schema->GetTable($this->tableName); |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
544 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
545 my $constraint = $table->GetConstraint($this->constraintName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
546 or return 0; |
194 | 547 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
548 # есть ли внешние ключи на данную таблицу |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
549 return ( |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
550 is($constraint,PK) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
551 && values( %{$constraint->connectedFK || {}} ) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
552 ? 0 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
553 : 1 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
554 ); |
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 sub Apply { |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
558 my ($this,$schema) = @_; |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
559 |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
560 my $table = $schema->GetTable($this->tableName) |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
561 or die IMPL::InvalidOperationException->new("The specified table doesn't exists", $this->tableName); |
194 | 562 |
563 $table->RemoveConstraint($this->constraintName); | |
164 | 564 } |
163 | 565 |
49 | 566 |
567 1; | |
163 | 568 |
569 __END__ | |
570 | |
571 =pod | |
572 | |
573 =head1 NAME | |
574 | |
180 | 575 C<IMPL::SQL::Traits> - Операции над объектками SQL схемы. |
163 | 576 |
577 =head1 DESCRIPTION | |
578 | |
180 | 579 Изменения схемы могу быть представлены в виде последовательности примитивных операций. |
580 Правила выполнения последовательности примитывных действий могут варьироваться | |
581 в зависимости от процессора, который их выполняет. Например C<IMPL::SQL::Schema::Traits::Processor>. | |
163 | 582 |
180 | 583 Данные, которые содержаться в примитивных операциях не могут существовать независимо от схемы. |
164 | 584 |
269 | 585 =head1 OPERATIONS |
164 | 586 |
587 =head2 General | |
588 | |
180 | 589 Методы обще для всех примитивных операций. |
164 | 590 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
591 =head3 C<CanApply($schema)> |
164 | 592 |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
593 Определяет возможность применения операции к указанной схеме. |
164 | 594 |
180 | 595 Возвращаемое значение: |
164 | 596 |
597 =over | |
598 | |
599 =item C<true> | |
600 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
601 Операция приминима к схеме. |
164 | 602 |
603 =item C<false> | |
604 | |
180 | 605 Операция не может быть применена к схеме. |
164 | 606 |
607 =back | |
608 | |
271
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
609 =head3 C<Apply($schema)> |
56364d0c4b4f
+IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
269
diff
changeset
|
610 |
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 |
164 | 613 =head2 Primitive operations |
614 | |
269 | 615 =head3 C<IMPL::SQL::Schema::Traits::CreateTable> |
164 | 616 |
180 | 617 Создает таблицу |
164 | 618 |
269 | 619 =head4 C<CTOR($table)> |
164 | 620 |
269 | 621 =head4 C<[get]table> |
164 | 622 |
180 | 623 C<IMPL::SQL::Schema::Traits::Table> - описание создаваемой таблицы |
164 | 624 |
269 | 625 =head3 C<IMPL::SQL::Schema::Traits::DropTable> |
164 | 626 |
180 | 627 Удалает таблицу по имени |
164 | 628 |
269 | 629 =head4 C<CTOR($tableName)> |
164 | 630 |
269 | 631 =head4 C<[get]tableName> |
164 | 632 |
180 | 633 Имя удаляемой таблицы |
164 | 634 |
269 | 635 =head3 C<IMPL::SQL::Schema::Traits::RenameTable> |
164 | 636 |
269 | 637 =head4 C<CTOR($tableName,$tableNewName)> |
164 | 638 |
269 | 639 =head4 C<[get]tableName> |
164 | 640 |
180 | 641 Имя таблицы, которую требуется переименовать |
164 | 642 |
269 | 643 =head4 C<[get]tableNewName> |
164 | 644 |
180 | 645 Новое имя таблицы |
164 | 646 |
269 | 647 =head3 C<IMPL::SQL::Schema::Traits::AlterTableAddColumn> |
164 | 648 |
180 | 649 Добавляет столбец в таблицу |
164 | 650 |
269 | 651 =head4 C<CTOR($tableName,$column,$position)> |
164 | 652 |
269 | 653 =head4 C<[get]tableName> |
164 | 654 |
180 | 655 Имя таблицы в которую нужно добавить столбец |
164 | 656 |
269 | 657 =head4 C<[get]column> |
164 | 658 |
269 | 659 C<IMPL::SQL::Schema::Traits::Column> - описание столбца который нужно добавить |
164 | 660 |
269 | 661 =head4 C<[get]position> |
164 | 662 |
269 | 663 Позиция на которую нужно вставить столбец |
664 | |
665 =head3 C<IMPL::SQL::Schema::Traits::AlterTableDropColumn> | |
164 | 666 |
180 | 667 Удаляет столбец из таблицы |
164 | 668 |
269 | 669 =head4 C<CTOR($tableName,$columnName)> |
164 | 670 |
269 | 671 =head4 C<[get]tableName> |
164 | 672 |
180 | 673 Имя таблицы в которой нужно удалить столбец |
164 | 674 |
269 | 675 =head4 C<[get]columnName> |
164 | 676 |
180 | 677 Имя столбца для удаления |
164 | 678 |
269 | 679 =head3 C<IMPL::SQL::Schema::Traits::AlterTableChangeColumn> |
164 | 680 |
180 | 681 Меняет описание столбца |
164 | 682 |
269 | 683 =head4 C<CTOR($tableName,$columnName,%args)> |
164 | 684 |
180 | 685 C<%args> - хеш, ключами которого являются оставшиеся свойства создаваемого объекта. |
164 | 686 |
269 | 687 =head4 C<[get]tableName> |
164 | 688 |
180 | 689 Имя таблицы в которой находится столбец. |
164 | 690 |
269 | 691 =head4 C<[get]columnName> |
164 | 692 |
180 | 693 Имя столбца для изменения |
164 | 694 |
269 | 695 =head4 C<[get]columnType> |
164 | 696 |
180 | 697 Новый тип столбца. Не задан, если тип не меняется |
164 | 698 |
269 | 699 =head4 C<[get]defaultValue> |
164 | 700 |
180 | 701 Значение по умолчанию. Не задано, если не меняется |
164 | 702 |
269 | 703 =head4 C<[get]isNullable> |
164 | 704 |
180 | 705 Может ли столбец содержать C<NULL>. Не задано, если не меняется. |
164 | 706 |
269 | 707 =head4 C<[get]options> |
164 | 708 |
269 | 709 Хеш опций, не задан, если опции не меняются. Данный хеш содержит разничу между |
710 старыми и новыми значениями свойства C<tag> столбца. | |
164 | 711 |
269 | 712 |
713 =head3 C<IMPL::SQL::Schema::Traits::AlterTableAddConstraint> | |
164 | 714 |
180 | 715 Базовый класс для операций по добавлению ограничений |
164 | 716 |
269 | 717 =head4 C<CTOR($tableName,$constraint)> |
164 | 718 |
269 | 719 =head4 C<[get]tableName> |
164 | 720 |
180 | 721 Имя таблицы в которую добавляется ограничение. |
164 | 722 |
269 | 723 =head4 C<[get]constraint> |
164 | 724 |
180 | 725 C<IMPL::SQL::Schema::Traits::Constraint> - описние ограничения, которое нужно добавить. |
164 | 726 |
269 | 727 =head3 C<IMPL::SQL::Schema::Traits::AlterTableDropConstraint> |
164 | 728 |
180 | 729 Удаляет ограничение на таблицу |
164 | 730 |
269 | 731 =head4 C<CTOR($tableName,$constraintName)> |
164 | 732 |
269 | 733 =head4 C<[get]tableName> |
164 | 734 |
180 | 735 Имя таблицы в которой требуется удалить ограничение. |
164 | 736 |
269 | 737 =head4 C<[get]constraintName> |
164 | 738 |
180 | 739 Имя ограничения для удаления. |
164 | 740 |
180 | 741 =cut |