Mercurial > pub > Impl
annotate Lib/IMPL/SQL/Schema/MySQL/Processor.pm @ 286:d357b5d85d25
*TTView refactoring
author | sergey |
---|---|
date | Mon, 18 Feb 2013 14:46:06 +0400 |
parents | 2f06250bab5f |
children |
rev | line source |
---|---|
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', | |
283
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
11 DropTable => '-IMPL::SQL::Schema::Traits::DropTable', |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
12 PrimitiveDropTable => '-IMPL::SQL::Schema::MySQL::Processor::PrimitiveDropTable', |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
13 CreateTable => '-IMPL::SQL::Schema::Traits::CreateTable', |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
14 Table => '-IMPL::SQL::Schema::Traits::Table', |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
15 ForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey', |
271 | 16 |
17 }, | |
18 base => [ | |
19 'IMPL::SQL::Schema::Processor' => sub { $_[0] } | |
20 ], | |
21 props => [ | |
22 formatter => PROP_RO, | |
23 sqlBatch => PROP_RO | |
24 ] | |
25 }; | |
26 use IMPL::lang qw(is); | |
27 | |
28 sub CTOR { | |
29 my ( $this, $schema, %opts ) = @_; | |
30 | |
31 $this->formatter( $opts{formatter} || MySQLFormatter ); | |
32 $this->sqlBatch([]); | |
33 } | |
34 | |
35 sub AddSqlBatch { | |
36 my $this = shift; | |
37 | |
38 push @{$this->sqlBatch}, @_; | |
39 } | |
40 | |
41 sub ApplyOperation { | |
42 my ($this, $op, $iteration ) = @_; | |
43 | |
44 my @formatterParams; | |
45 | |
46 if ( is( $op, AlterTableDropConstraint ) ) { | |
47 my $constraint = $this | |
48 ->dbSchema | |
49 ->GetTable($op->tableName) | |
50 ->GetConstraint($op->constraintName); | |
51 | |
52 push @formatterParams, ref $constraint; | |
53 } else { | |
54 push @formatterParams, $this->dbSchema; | |
55 } | |
56 | |
57 if ( is( $op, CreateTable ) ) { | |
58 my @constraints; | |
59 my @fk; | |
60 my $table = $op->table; | |
61 | |
62 # отделяем создание внешних ключей от таблиц | |
63 | |
64 foreach my $c (@{$table->{constraints} || []}) { | |
65 if ( is($c,ForeignKey)) { | |
66 push @fk,$c; | |
67 } else { | |
68 push @constraints, $c; | |
69 } | |
70 } | |
71 | |
72 if (@fk) { | |
73 $op = CreateTable->new( | |
74 Table->new( | |
75 $table->{name}, | |
76 $table->{columns}, | |
77 \@constraints, | |
78 $table->{options} | |
79 ) | |
80 ); | |
81 | |
82 $this->AddPendingOperations( | |
83 map AlterTableAddConstraint->new($table->{name},$_), @fk | |
84 ); | |
85 } | |
86 } | |
87 | |
283
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
88 if (is($op, DropTable)) { |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
89 my $table = $this->dbSchema->GetTable($op->tableName); |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
90 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
91 if(my $pk = $table->primaryKey) { |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
92 $this->ApplyOperation($_,$iteration) |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
93 foreach |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
94 map |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
95 AlterTableDropConstraint->new($_->table->name,$_->name), |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
96 values %{$pk->connectedFK || {}}; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
97 } |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
98 } |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
99 |
271 | 100 $this->next::method($op,$iteration); |
101 | |
102 $this->AddSqlBatch( | |
103 $this->formatter->Format($op,@formatterParams) | |
104 ); | |
105 } | |
106 | |
283
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
107 package IMPL::SQL::Schema::MySQL::Processor::PrimitiveDropTable; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
108 use IMPL::Const qw(:prop); |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
109 use IMPL::declare { |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
110 require => { |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
111 ArgException => '-IMPL::InvalidArgumentException' |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
112 }, |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
113 base => [ |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
114 'IMPL::Object' => undef |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
115 ], |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
116 props => [ |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
117 tableName => PROP_RO, |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
118 ] |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
119 }; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
120 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
121 sub CTOR { |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
122 my ($this,$tableName) = @_; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
123 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
124 $this->tableName($tableName) or die ArgException->new("tableName is required"); |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
125 } |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
126 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
127 sub CanApply { |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
128 my ($this,$schema) = @_; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
129 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
130 my $table = $schema->GetTable( $this->tableName ) |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
131 or return 0; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
132 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
133 my $pk = $table->primaryKey |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
134 or return 1; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
135 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
136 my $canDrop = keys(%{$pk->connectedFK || {}}) ? 0 : 1; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
137 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
138 warn "Can drop ", $this->tableName |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
139 if $canDrop; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
140 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
141 return $canDrop; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
142 } |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
143 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
144 sub Apply { |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
145 my ($this,$schema) = @_; |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
146 |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
147 $schema->RemoveTable($this->tableName); |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
148 } |
2f06250bab5f
*IMPL::SQL::MySQL fixed issues with foreign keys and drop table
sergey
parents:
271
diff
changeset
|
149 |
271 | 150 1; |