annotate Lib/IMPL/SQL/Schema/MySQL/Processor.pm @ 275:6253872024a4

*refactoring IMPL::Class
author cin
date Thu, 31 Jan 2013 02:18:31 +0400
parents 56364d0c4b4f
children 2f06250bab5f
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',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
11 CreateTable => '-IMPL::SQL::Schema::Traits::CreateTable',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
12 Table => '-IMPL::SQL::Schema::Traits::Table',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
13 ForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
14
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
15 },
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
16 base => [
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
17 'IMPL::SQL::Schema::Processor' => sub { $_[0] }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
18 ],
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
19 props => [
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
20 formatter => PROP_RO,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
21 sqlBatch => PROP_RO
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
22 ]
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
23 };
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
24 use IMPL::lang qw(is);
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 sub CTOR {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
27 my ( $this, $schema, %opts ) = @_;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
28
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
29 $this->formatter( $opts{formatter} || MySQLFormatter );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
30 $this->sqlBatch([]);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
31 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
32
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
33 sub AddSqlBatch {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
34 my $this = shift;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
35
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
36 push @{$this->sqlBatch}, @_;
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
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
39 sub ApplyOperation {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
40 my ($this, $op, $iteration ) = @_;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
41
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
42 my @formatterParams;
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 if ( is( $op, AlterTableDropConstraint ) ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
45 my $constraint = $this
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
46 ->dbSchema
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
47 ->GetTable($op->tableName)
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
48 ->GetConstraint($op->constraintName);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
49
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
50 push @formatterParams, ref $constraint;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
51 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
52 push @formatterParams, $this->dbSchema;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
53 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
54
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
55 if ( is( $op, CreateTable ) ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
56 my @constraints;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
57 my @fk;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
58 my $table = $op->table;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
59
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
60 # отделяем создание внешних ключей от таблиц
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 foreach my $c (@{$table->{constraints} || []}) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
63 if ( is($c,ForeignKey)) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
64 push @fk,$c;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
65 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
66 push @constraints, $c;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
67 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
68 }
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 if (@fk) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
71 $op = CreateTable->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
72 Table->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
73 $table->{name},
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
74 $table->{columns},
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
75 \@constraints,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
76 $table->{options}
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
77 )
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
78 );
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 $this->AddPendingOperations(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
81 map AlterTableAddConstraint->new($table->{name},$_), @fk
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
82 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
83 }
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 $this->next::method($op,$iteration);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
87
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
88 $this->AddSqlBatch(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
89 $this->formatter->Format($op,@formatterParams)
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
90 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
91 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
92
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
93 1;