271
|
1 package IMPL::SQL::Schema::MySQL::Processor;
|
|
2 use strict;
|
|
3
|
|
4 use mro;
|
|
5 use IMPL::Const qw(:prop);
|
|
6 use IMPL::declare {
|
|
7 require => {
|
|
8 MySQLFormatter => 'IMPL::SQL::Schema::MySQL::Formatter',
|
|
9 AlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint',
|
|
10 AlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint',
|
|
11 CreateTable => '-IMPL::SQL::Schema::Traits::CreateTable',
|
|
12 Table => '-IMPL::SQL::Schema::Traits::Table',
|
|
13 ForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey',
|
|
14
|
|
15 },
|
|
16 base => [
|
|
17 'IMPL::SQL::Schema::Processor' => sub { $_[0] }
|
|
18 ],
|
|
19 props => [
|
|
20 formatter => PROP_RO,
|
|
21 sqlBatch => PROP_RO
|
|
22 ]
|
|
23 };
|
|
24 use IMPL::lang qw(is);
|
|
25
|
|
26 sub CTOR {
|
|
27 my ( $this, $schema, %opts ) = @_;
|
|
28
|
|
29 $this->formatter( $opts{formatter} || MySQLFormatter );
|
|
30 $this->sqlBatch([]);
|
|
31 }
|
|
32
|
|
33 sub AddSqlBatch {
|
|
34 my $this = shift;
|
|
35
|
|
36 push @{$this->sqlBatch}, @_;
|
|
37 }
|
|
38
|
|
39 sub ApplyOperation {
|
|
40 my ($this, $op, $iteration ) = @_;
|
|
41
|
|
42 my @formatterParams;
|
|
43
|
|
44 if ( is( $op, AlterTableDropConstraint ) ) {
|
|
45 my $constraint = $this
|
|
46 ->dbSchema
|
|
47 ->GetTable($op->tableName)
|
|
48 ->GetConstraint($op->constraintName);
|
|
49
|
|
50 push @formatterParams, ref $constraint;
|
|
51 } else {
|
|
52 push @formatterParams, $this->dbSchema;
|
|
53 }
|
|
54
|
|
55 if ( is( $op, CreateTable ) ) {
|
|
56 my @constraints;
|
|
57 my @fk;
|
|
58 my $table = $op->table;
|
|
59
|
|
60 # отделяем создание внешних ключей от таблиц
|
|
61
|
|
62 foreach my $c (@{$table->{constraints} || []}) {
|
|
63 if ( is($c,ForeignKey)) {
|
|
64 push @fk,$c;
|
|
65 } else {
|
|
66 push @constraints, $c;
|
|
67 }
|
|
68 }
|
|
69
|
|
70 if (@fk) {
|
|
71 $op = CreateTable->new(
|
|
72 Table->new(
|
|
73 $table->{name},
|
|
74 $table->{columns},
|
|
75 \@constraints,
|
|
76 $table->{options}
|
|
77 )
|
|
78 );
|
|
79
|
|
80 $this->AddPendingOperations(
|
|
81 map AlterTableAddConstraint->new($table->{name},$_), @fk
|
|
82 );
|
|
83 }
|
|
84 }
|
|
85
|
|
86 $this->next::method($op,$iteration);
|
|
87
|
|
88 $this->AddSqlBatch(
|
|
89 $this->formatter->Format($op,@formatterParams)
|
|
90 );
|
|
91 }
|
|
92
|
|
93 1;
|