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 use IMPL::Code::Loader();
|
|
8
|
168
|
9 # required for use with typeof operator
|
|
10 use IMPL::SQL::Schema::Constraint::PrimaryKey();
|
|
11 use IMPL::SQL::Schema::Constraint::Index();
|
|
12 use IMPL::SQL::Schema::Constraint::Unique();
|
|
13 use IMPL::SQL::Schema::Constraint::ForeignKey();
|
49
|
14
|
164
|
15 ###################################################
|
|
16
|
165
|
17 package IMPL::SQL::Schema::Traits::Table;
|
|
18 use base qw(IMPL::Object::Fields);
|
163
|
19
|
164
|
20 use fields qw(
|
|
21 name
|
|
22 columns
|
|
23 constraints
|
|
24 options
|
|
25 );
|
|
26
|
|
27 sub CTOR {
|
|
28 my ($this,$table,$columns,$constraints,$options) = @_;
|
|
29
|
165
|
30 $this->{name} = $table or die new IMPL::InvalidArgumentException(table => "A table name is required");
|
|
31 $this->{columns} = $columns if defined $columns;
|
|
32 $this->{constraints} = $constraints if defined $constraints;
|
|
33 $this->{options} = $options if defined $options;
|
164
|
34 }
|
|
35
|
|
36 ###################################################
|
|
37
|
165
|
38 package IMPL::SQL::Schema::Traits::Column;
|
|
39 use base qw(IMPL::Object::Fields);
|
164
|
40
|
|
41 use fields qw(
|
|
42 name
|
|
43 type
|
|
44 isNullable
|
|
45 defaultValue
|
|
46 tag
|
|
47 );
|
|
48
|
|
49 sub CTOR {
|
|
50 my ($this, $name, $type, %args) = @_;
|
|
51
|
|
52 $this->{name} = $name or die new IMPL::InvalidArgumentException("name");
|
|
53 $this->{type} = $type or die new IMPL::InvalidArgumentException("type");
|
|
54 $this->{isNullable} = $args{isNullable} if exists $args{isNullable};
|
|
55 $this->{defaultValue} = $args{defaultValue} if exists $args{defaultValue};
|
|
56 $this->{tag} = $args{tag} if exists $args{tag};
|
|
57 }
|
|
58
|
|
59 ##################################################
|
|
60
|
165
|
61 package IMPL::SQL::Schema::Traits::Constraint;
|
|
62 use base qw(IMPL::Object::Fields);
|
167
|
63
|
164
|
64 use fields qw(
|
|
65 name
|
|
66 columns
|
|
67 );
|
|
68
|
|
69 sub CTOR {
|
167
|
70 my ($this, $name, $columns) = @_;
|
164
|
71
|
|
72 $this->{name} = $name;
|
168
|
73 $this->{columns} = $columns; # list of columnNames
|
|
74 }
|
|
75
|
|
76 sub constraintClass {
|
|
77 die new IMPL::NotImplementedException();
|
164
|
78 }
|
|
79
|
|
80 ##################################################
|
|
81
|
165
|
82 package IMPL::SQL::Schema::Traits::PrimaryKey;
|
164
|
83
|
165
|
84 use base qw(IMPL::SQL::Schema::Traits::Constraint);
|
164
|
85
|
|
86 __PACKAGE__->PassThroughArgs;
|
|
87
|
168
|
88 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::PrimaryKey };
|
|
89
|
164
|
90 ##################################################
|
|
91
|
165
|
92 package IMPL::SQL::Schema::Traits::Index;
|
164
|
93
|
165
|
94 use base qw(IMPL::SQL::Schema::Traits::Constraint);
|
164
|
95
|
|
96 __PACKAGE__->PassThroughArgs;
|
|
97
|
168
|
98 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::Index };
|
|
99
|
164
|
100 ##################################################
|
|
101
|
165
|
102 package IMPL::SQL::Schema::Traits::Unique;
|
164
|
103
|
165
|
104 use base qw(IMPL::SQL::Schema::Traits::Constraint);
|
164
|
105
|
|
106 __PACKAGE__->PassThroughArgs;
|
163
|
107
|
168
|
108 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::Unique };
|
|
109
|
164
|
110 ##################################################
|
|
111
|
165
|
112 package IMPL::SQL::Schema::Traits::ForeignKey;
|
164
|
113
|
165
|
114 use base qw(IMPL::SQL::Schema::Traits::Constraint);
|
164
|
115 use fields qw(
|
|
116 foreignTable
|
|
117 foreignColumns
|
|
118 );
|
|
119
|
168
|
120 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::ForeignKey };
|
|
121
|
164
|
122 our %CTOR = (
|
167
|
123 'IMPL::SQL::Schema::Traits::Constraint' => sub { @_[0..1] }
|
164
|
124 );
|
|
125
|
|
126 sub CTOR {
|
167
|
127 my ($this,$foreignTable,$foreignColumns) = @_[0,3,4];
|
164
|
128
|
|
129 $this->{foreignTable} = $foreignTable;
|
|
130 $this->{foreignColunms} = $foreignColumns;
|
|
131 }
|
|
132
|
|
133
|
|
134 ##################################################
|
|
135
|
165
|
136 package IMPL::SQL::Schema::Traits::CreateTable;
|
164
|
137
|
165
|
138 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
164
|
139 use IMPL::Class::Property;
|
|
140 use IMPL::lang;
|
|
141
|
|
142 BEGIN {
|
|
143 public property table => prop_get | owner_set;
|
|
144 }
|
|
145
|
|
146 sub CTOR {
|
|
147 my ($this,$table) = @_;
|
|
148
|
165
|
149 die new IMPL::InvalidArgumentException("table", "An object of IMPL::SQL::Schema::Traits::Table type is required")
|
|
150 unless is $table, typeof IMPL::SQL::Schema::Traits::Table;
|
164
|
151
|
|
152 $this->table($table);
|
|
153 }
|
|
154
|
|
155 sub apply {
|
|
156 my ($this,$schema) = @_;
|
|
157
|
|
158 return 0 if ( $schema->GetTable( $this->table->{name} ) );
|
|
159
|
|
160 $schema->AddTable($this->table);
|
|
161 return 1;
|
|
162 }
|
|
163
|
|
164 ##################################################
|
|
165
|
165
|
166 package IMPL::SQL::Schema::Traits::DropTable;
|
|
167 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
164
|
168 use IMPL::Class::Property;
|
|
169
|
|
170 BEGIN {
|
|
171 public property tableName => prop_get | owner_set;
|
|
172 }
|
|
173
|
|
174 sub CTOR {
|
|
175 my ($this,$tableName) = @_;
|
|
176
|
|
177 $this->tableName($tableName) or die new IMPL::InvalidArgumentException("tableName is required");
|
|
178 }
|
|
179
|
|
180 sub apply {
|
|
181 my ($this,$schema) = @_;
|
|
182
|
|
183 return 0 if $schema->GetTable( $this->tableName );
|
|
184
|
|
185 $schema->RemoveTable($this->tableName);
|
|
186
|
|
187 return 1;
|
|
188 }
|
|
189
|
|
190 ##################################################
|
|
191
|
165
|
192 package IMPL::SQL::Schema::Traits::RenameTable;
|
|
193 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
163
|
194 use IMPL::Class::Property;
|
49
|
195
|
|
196 BEGIN {
|
164
|
197 public property tableName => prop_get | owner_set;
|
|
198 public property tableNewName => prop_get | owner_set;
|
|
199 }
|
|
200
|
|
201 sub CTOR {
|
|
202 my ($this, $oldName, $newName) = @_;
|
|
203
|
|
204 $this->tableName($oldName) or die new IMPL::InvalidArgumentException("A table name is required");
|
|
205 $this->tableNewName($newName) or die new IMPL::InvalidArgumentException("A new table name is required");
|
|
206 }
|
|
207
|
|
208 sub apply {
|
|
209 my ($this,$schema) = @_;
|
|
210
|
|
211 return 0 if not $schema->GetTable($this->tableName) or $schema->GetTable($this->tableNewName);
|
|
212
|
|
213 $this->RenameTable($this->tableName, $this->tableNewName);
|
|
214
|
|
215 return 1;
|
|
216 }
|
|
217
|
|
218 #################################################
|
|
219
|
165
|
220 package IMPL::SQL::Schema::Traits::AlterTableAddColumn;
|
|
221 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
164
|
222 use IMPL::Class::Property;
|
|
223 use IMPL::lang;
|
|
224
|
|
225 BEGIN {
|
|
226 public property tableName => prop_get | owner_set;
|
|
227 public property column => prop_get | owner_set;
|
49
|
228 }
|
|
229
|
164
|
230 sub CTOR {
|
|
231 my ($this,$tableName,$column) = @_;
|
|
232
|
|
233 $this->tableName($tableName) or die new IMPL::InvalidArgumentException("A table name is required");
|
|
234
|
165
|
235 die new IMPL::InvalidArgumentException("A column should be a IMPL::SQL::Schema::Traits::Column object")
|
|
236 unless is $column, typeof IMPL::SQL::Schema::Traits::Column;
|
164
|
237
|
|
238 $this->column($column);
|
|
239 }
|
|
240
|
|
241 sub apply {
|
|
242 my ($this,$schema) = @_;
|
|
243
|
|
244 my $table = $schema->GetTable($this->tableName) or return 0;
|
|
245
|
|
246 return 0 if $table->GetColumn( $this->column->{name} );
|
|
247
|
|
248 $table->AddColumn($this->column);
|
|
249
|
|
250 return 1;
|
|
251 }
|
|
252
|
|
253 #################################################
|
|
254
|
165
|
255 package IMPL::SQL::Schema::Traits::AlterTableDropColumn;
|
|
256 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
164
|
257 use IMPL::Class::Property;
|
|
258
|
|
259 BEGIN {
|
|
260 public property tableName => prop_get | owner_set;
|
|
261 public property columnName => prop_get | owner_set;
|
|
262 }
|
|
263
|
|
264 sub CTOR {
|
|
265 my ($this,$table,$column) = @_;
|
|
266
|
|
267 $this->tableName($table) or die new IMPL::InvalidArgumentException(tableName => "A table name should be specified");
|
|
268 $this->columnName($column) or die new IMPL::InvalidArgumentException(columnName => "A column name should be specified");
|
|
269 }
|
|
270
|
|
271 sub apply {
|
|
272 my ($this,$schema) = @_;
|
|
273
|
|
274 local $@;
|
|
275
|
|
276 return eval {
|
|
277 $schema->GetTable($this->tableName)->RemoveColumn($this->columnName);
|
|
278 return 1;
|
|
279 } || 0;
|
49
|
280 }
|
|
281
|
164
|
282 #################################################
|
163
|
283
|
165
|
284 package IMPL::SQL::Schema::Traits::AlterTableChangeColumn;
|
|
285 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
164
|
286 use IMPL::Class::Property;
|
49
|
287
|
164
|
288 BEGIN {
|
|
289 public property tableName => prop_get | owner_set;
|
|
290 public property columnName => prop_get | owner_set;
|
168
|
291 public property columnType => prop_all;
|
|
292 public property defaultValue => prop_all;
|
|
293 public property isNullable => prop_all;
|
|
294 public property options => prop_all; # hash diff format, (keys have a prefix '+' - add or update value, '-' remove value)
|
164
|
295 }
|
49
|
296
|
164
|
297 sub CTOR {
|
|
298 my ($this, $table,$column,%args) = @_;
|
|
299
|
|
300 $this->tableName($table) or die new IMPL::InvalidArgumentException(tableName => "A table name is required");
|
|
301 $this->columnName($column) or die new IMPL::InvalidArgumentException(columnName => "A column name is required");
|
|
302
|
|
303 $this->$_($args{$_})
|
|
304 for (grep exists $args{$_}, qw(columnType defaultValue isNullable options));
|
|
305 }
|
49
|
306
|
164
|
307 sub apply {
|
|
308 my ($this,$schema) = @_;
|
|
309
|
|
310 local $@;
|
|
311
|
|
312 return eval {
|
|
313 my $column = $schema->GetTable($this->tableName)->GetColumn($this->columnName);
|
168
|
314 $column->SetType($this->columnType) if defined $this->columnType;
|
|
315 $column->SetNullable($this->isNullable) if defined $this->isNullable;
|
|
316 $column->SetDefaultValue($this->defaultValue) if defined $this->defaultValue;
|
|
317 $column->SetOptions($this->options) if defined $this->options;
|
164
|
318
|
|
319 return 1;
|
|
320 } || 0;
|
|
321 }
|
49
|
322
|
164
|
323 #################################################
|
163
|
324
|
165
|
325 package IMPL::SQL::Schema::Traits::AlterTableAddConstraint;
|
|
326 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
164
|
327 use IMPL::Class::Property;
|
|
328 use IMPL::lang;
|
49
|
329
|
164
|
330 BEGIN {
|
|
331 public property tableName => prop_get | owner_set;
|
|
332 public property constraint => prop_get | owner_set;
|
|
333 }
|
49
|
334
|
164
|
335 sub CTOR {
|
|
336 my ($this,$table,$constraint) = @_;
|
|
337
|
|
338 $this->tableName($table) or die new IMPL::InvalidArgumentException( tableName => "A table name is required");
|
|
339
|
165
|
340 die new IMPL::InvalidArgumentException(constaraint => "A valid IMPL::SQL::Schema::Traits::Constarint is required")
|
|
341 unless is $constraint, typeof IMPL::SQL::Schema::Traits::Constraint;
|
164
|
342
|
|
343 $this->constraint($constraint);
|
|
344 }
|
49
|
345
|
164
|
346 sub apply {
|
|
347 my ($this,$schema) = @_;
|
|
348
|
|
349 local $@;
|
|
350
|
|
351 return eval {
|
168
|
352 $schema->GetTable($this->tableName)->AddConstraint($this->constraint->constraintClass, $this->constraint);
|
164
|
353 return 1;
|
|
354 } || 0;
|
|
355
|
|
356 }
|
49
|
357
|
164
|
358 #################################################
|
49
|
359
|
165
|
360 package IMPL::SQL::Schema::Traits::AlterTableDropConstraint;
|
|
361 use parent qw(-norequire IMPL::SQL::Schema::Traits);
|
164
|
362 use IMPL::Class::Property;
|
|
363
|
|
364 BEGIN {
|
|
365 public property tableName => prop_get | owner_set;
|
|
366 public property constraintName => prop_get | owner_set;
|
|
367 }
|
163
|
368
|
164
|
369 sub CTOR {
|
|
370 my ($this,$table,$constraint) = @_;
|
|
371
|
|
372 die new IMPL::InvalidArgumentException( tableName => "A table name is required" ) unless $table;
|
|
373 die new IMPL::InvalidArgumentException( constraintName => "A constraint name is required" ) unless $constraint;
|
|
374 }
|
49
|
375
|
164
|
376 sub apply {
|
|
377 my ($this,$schema) = @_;
|
|
378
|
|
379 my $table = $schema->GetTable($this->tableName) or return 0;
|
|
380
|
|
381 return 0 unless $table->GetConstraint($this->constraintName);
|
|
382
|
|
383 $table->RemoveConstraint($this->constraintName);
|
|
384 return 1;
|
|
385 }
|
163
|
386
|
49
|
387
|
|
388 1;
|
163
|
389
|
|
390 __END__
|
|
391
|
|
392 =pod
|
|
393
|
|
394 =head1 NAME
|
|
395
|
|
396 C<IMPL::SQL::Traits> - Операции над объектками SQL схемы.
|
|
397
|
|
398 =head1 DESCRIPTION
|
|
399
|
|
400 Изменения схемы могу быть представлены в виде последовательности примитивных операций.
|
164
|
401 Правила выполнения последовательности примитывных действий могут варьироваться
|
165
|
402 в зависимости от процессора, который их выполняет. Например C<IMPL::SQL::Schema::Traits::Processor>.
|
163
|
403
|
164
|
404 Данные, которые содержаться в примитивных операциях не могут существовать независимо от схемы.
|
|
405
|
|
406 =head1 OPEARATIONS
|
|
407
|
|
408 =head2 General
|
|
409
|
|
410 Методы обще для всех примитивных операций.
|
|
411
|
|
412 =over
|
|
413
|
|
414 =item C<apply($schema)>
|
|
415
|
|
416 Пытается приминить операцию к указанной схеме.
|
|
417
|
|
418 Возвращаемое значение:
|
|
419
|
|
420 =over
|
|
421
|
|
422 =item C<true>
|
|
423
|
|
424 Операция успешно применена к схеме.
|
|
425
|
|
426 =item C<false>
|
|
427
|
|
428 Операция не может быть применена к схеме.
|
|
429
|
|
430 =back
|
|
431
|
|
432 =back
|
|
433
|
|
434 =head2 Primitive operations
|
|
435
|
|
436 =over
|
|
437
|
165
|
438 =item C<IMPL::SQL::Schema::Traits::CreateTable>
|
164
|
439
|
|
440 Создает таблицу
|
|
441
|
|
442 =over
|
|
443
|
|
444 =item C<CTOR($table)>
|
|
445
|
|
446 =item C<[get]table>
|
|
447
|
165
|
448 C<IMPL::SQL::Schema::Traits::Table> - описание создаваемой таблицы
|
164
|
449
|
|
450 =back
|
|
451
|
165
|
452 =item C<IMPL::SQL::Schema::Traits::DropTable>
|
164
|
453
|
|
454 Удалает таблицу по имени
|
|
455
|
|
456 =over
|
|
457
|
|
458 =item C<CTOR($tableName)>
|
|
459
|
|
460 =item C<[get]tableName>
|
|
461
|
|
462 Имя удаляемой таблицы
|
|
463
|
|
464 =back
|
|
465
|
165
|
466 =item C<IMPL::SQL::Schema::Traits::RenameTable>
|
164
|
467
|
|
468 =over
|
|
469
|
|
470 =item C<CTOR($tableName,$tableNewName)>
|
|
471
|
|
472 =item C<[get]tableName>
|
|
473
|
|
474 Имя таблицы, которую требуется переименовать
|
|
475
|
|
476 =item C<[get]tableNewName>
|
|
477
|
|
478 Новое имя таблицы
|
|
479
|
|
480 =back
|
|
481
|
165
|
482 =item C<IMPL::SQL::Schema::Traits::AlterTableAddColumn>
|
164
|
483
|
|
484 Добавляет столбец в таблицу
|
|
485
|
|
486 =over
|
|
487
|
|
488 =item C<CTOR($tableName,$column)>
|
|
489
|
|
490 =item C<[get]tableName>
|
|
491
|
|
492 Имя таблицы в которую нужно добавить столбец
|
|
493
|
|
494 =item C<[get]column>
|
|
495
|
165
|
496 C<IMPL::SQL::Schema::Traits::Column> - описание столбца который нужно добавить
|
164
|
497
|
|
498 =back
|
|
499
|
165
|
500 =item C<IMPL::SQL::Schema::Traits::AlterTableDropColumn>
|
164
|
501
|
|
502 Удаляет столбец из таблицы
|
|
503
|
|
504 =over
|
|
505
|
|
506 =item C<CTOR($tableName,$columnName)>
|
|
507
|
|
508 =item C<[get]tableName>
|
|
509
|
|
510 Имя таблицы в которой нужно удалить столбец
|
|
511
|
|
512 =item C<[get]columnName>
|
|
513
|
|
514 Имя столбца для удаления
|
|
515
|
|
516 =back
|
|
517
|
165
|
518 =item C<IMPL::SQL::Schema::Traits::AlterTableChangeColumn>
|
164
|
519
|
|
520 Меняет описание столбца
|
|
521
|
|
522 =over
|
|
523
|
|
524 =item C<CTOR($tableName,$columnName,%args)>
|
|
525
|
|
526 C<%args> - хеш, ключами которого являются оставшиеся свойства создаваемого объекта.
|
|
527
|
|
528 =item C<[get]tableName>
|
|
529
|
|
530 Имя таблицы в которой находится столбец.
|
|
531
|
|
532 =item C<[get]columnName>
|
|
533
|
|
534 Имя столбца для изменения
|
|
535
|
|
536 =item C<[get]columnType>
|
|
537
|
|
538 Новый тип столбца. Не задан, если тип не меняется
|
|
539
|
|
540 =item C<[get]defaultValue>
|
|
541
|
|
542 Значение по умолчанию. Не задано, если не меняется
|
|
543
|
|
544 =item C<[get]isNullable>
|
|
545
|
|
546 Может ли столбец содержать C<NULL>. Не задано, если не меняется.
|
|
547
|
|
548 =item C<[get]options>
|
|
549
|
|
550 Хеш опций, не задан, если опции не меняются
|
|
551
|
|
552 =back
|
|
553
|
165
|
554 =item C<IMPL::SQL::Schema::Traits::AlterTableAddConstraint>
|
164
|
555
|
|
556 Базовый класс для операций по добавлению ограничений
|
|
557
|
|
558 =over
|
|
559
|
|
560 =item C<CTOR($tableName,$constraint)>
|
|
561
|
|
562 =item C<[get]tableName>
|
|
563
|
|
564 Имя таблицы в которую добавляется ограничение.
|
|
565
|
|
566 =item C<[get]constraint>
|
|
567
|
165
|
568 C<IMPL::SQL::Schema::Traits::Constraint> - описние ограничения, которое нужно добавить.
|
164
|
569
|
|
570 =back
|
|
571
|
165
|
572 =item C<IMPL::SQL::Schema::Traits::AlterTableDropConstraint>
|
164
|
573
|
|
574 Удаляет ограничение на таблицу
|
|
575
|
|
576 =over
|
|
577
|
|
578 =item C<CTOR($tableName,$constraintName)>
|
|
579
|
|
580 =item C<[get]tableName>
|
|
581
|
|
582 Имя таблицы в которой требуется удалить ограничение.
|
|
583
|
|
584 =item C<[get]constraintName>
|
|
585
|
|
586 Имя ограничения для удаления.
|
|
587
|
|
588 =back
|
|
589
|
|
590 =back
|
163
|
591
|
|
592 =cut |