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