annotate Lib/IMPL/SQL/Schema/MySQL/Processor.pm @ 295:9b25e1f50144

sync
author cin
date Fri, 01 Mar 2013 03:15:30 +0400
parents 2f06250bab5f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
1 package IMPL::SQL::Schema::MySQL::Processor;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
2 use strict;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
3
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
4 use mro;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
5 use IMPL::Const qw(:prop);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
6 use IMPL::declare {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
7 require => {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
8 MySQLFormatter => 'IMPL::SQL::Schema::MySQL::Formatter',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
9 AlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
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
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
16
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
17 },
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
18 base => [
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
19 'IMPL::SQL::Schema::Processor' => sub { $_[0] }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
20 ],
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
21 props => [
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
22 formatter => PROP_RO,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
23 sqlBatch => PROP_RO
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
24 ]
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
25 };
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
26 use IMPL::lang qw(is);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
27
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
28 sub CTOR {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
29 my ( $this, $schema, %opts ) = @_;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
30
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
31 $this->formatter( $opts{formatter} || MySQLFormatter );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
32 $this->sqlBatch([]);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
33 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
34
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
35 sub AddSqlBatch {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
36 my $this = shift;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
37
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
38 push @{$this->sqlBatch}, @_;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
39 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
40
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
41 sub ApplyOperation {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
42 my ($this, $op, $iteration ) = @_;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
43
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
44 my @formatterParams;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
45
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
46 if ( is( $op, AlterTableDropConstraint ) ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
47 my $constraint = $this
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
48 ->dbSchema
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
49 ->GetTable($op->tableName)
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
50 ->GetConstraint($op->constraintName);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
51
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
52 push @formatterParams, ref $constraint;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
53 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
54 push @formatterParams, $this->dbSchema;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
55 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
56
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
57 if ( is( $op, CreateTable ) ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
58 my @constraints;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
59 my @fk;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
60 my $table = $op->table;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
61
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
62 # отделяем создание внешних ключей от таблиц
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
63
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
64 foreach my $c (@{$table->{constraints} || []}) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
65 if ( is($c,ForeignKey)) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
66 push @fk,$c;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
67 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
68 push @constraints, $c;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
69 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
70 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
71
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
72 if (@fk) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
73 $op = CreateTable->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
74 Table->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
75 $table->{name},
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
76 $table->{columns},
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
77 \@constraints,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
78 $table->{options}
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
79 )
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
80 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
81
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
82 $this->AddPendingOperations(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
83 map AlterTableAddConstraint->new($table->{name},$_), @fk
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
84 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
85 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
86 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
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
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
100 $this->next::method($op,$iteration);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
101
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
102 $this->AddSqlBatch(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
103 $this->formatter->Format($op,@formatterParams)
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
104 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
105 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
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
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
150 1;