annotate Lib/IMPL/SQL/Schema/Diff.pm @ 371:d5c8b955bf8d

refactoring
author cin
date Fri, 13 Dec 2013 16:49:47 +0400
parents 77df11605d3a
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::Diff;
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 use warnings;
315
77df11605d3a code cleanup
cin
parents: 271
diff changeset
4 use IMPL::lang qw(:compare :hash is typeof);
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
5
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
6 use IMPL::SQL::Schema::Traits();
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
7
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
8 use IMPL::require {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
9 Schema => 'IMPL::SQL::Schema',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
10 ForeignKey => '-IMPL::SQL::Schema::Constraint::ForeignKey',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
11 PrimaryKey => '-IMPL::SQL::Schema::Constraint::PrimaryKey',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
12 UniqueConstraint =>'-IMPL::SQL::Schema::Constraint::Unique',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
13 Index => '-IMPL::SQL::Schema::Constraint::Index',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
14 TraitsForeignKey => '-IMPL::SQL::Schema::Traits::ForeignKey',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
15 TraitsPrimaryKey => '-IMPL::SQL::Schema::Traits::PrimaryKey',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
16 TraitsUnique => '-IMPL::SQL::Schema::Traits::Unique',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
17 TraitsIndex => '-IMPL::SQL::Schema::Traits::Index',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
18 TraitsDropTable => '-IMPL::SQL::Schema::Traits::DropTable',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
19 TraitsCreateTable => '-IMPL::SQL::Schema::Traits::CreateTable',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
20 TraitsTable => '-IMPL::SQL::Schema::Traits::Table',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
21 TraitsColumn => '-IMPL::SQL::Schema::Traits::Column',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
22 TraitsAlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
23 TraitsAlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
24 TraitsAlterTableDropColumn => '-IMPL::SQL::Schema::Traits::AlterTableDropColumn',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
25 TraitsAlterTableAddColumn => '-IMPL::SQL::Schema::Traits::AlterTableAddColumn',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
26 TraitsAlterTableChangeColumn => '-IMPL::SQL::Schema::Traits::AlterTableChangeColumn',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
27 Exception => 'IMPL::Exception',
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
28 ArgException => '-IMPL::InvalidArgumentException'
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
29 };
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 sub Diff {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
32 my ($self,$src,$dst) = @_;
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 die ArgException->new( src => "A valid source schema is required") unless is($src,Schema);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
35 die ArgException->new( dst => "A valid desctination schema is requried" ) unless is($src,Schema);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
36
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
37 my %dstTables = map { $_->name, $_ } $dst->GetTables;
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 my @operations;
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 foreach my $srcTable ( $src->GetTables) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
42 my $dstTable = delete $dstTables{$srcTable->name};
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 (not $dstTable) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
45 # if a source table doesn't have a corresponding destination table, it should be deleted
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
46 push @operations, TraitsDropTable->new($srcTable->name);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
47 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
48 # a source table needs to be updated
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
49 push @operations, $self->_DiffTables($srcTable,$dstTable);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
50 }
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 }
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 foreach my $tbl ( values %dstTables ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
55 push @operations, TraitsCreateTable->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
56 TraitsTable->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
57 $tbl->name,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
58 [ map _Column2Traits($_), @{$tbl->columns} ],
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
59 [ map _Constraint2Traits($_), $tbl->GetConstraints()],
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
60 $tbl->{tag}
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
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
65 return \@operations;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
66 }
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 sub _DiffTables {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
69 my ($self,$src,$dst) = @_;
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 my @dropConstraints;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
72 my @createConstraints;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
73
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
74 my %srcConstraints = map { $_->name, $_ } $src->GetConstraints();
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
75 my %dstConstraints = map { $_->name, $_ } $dst->GetConstraints();
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
76
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
77 foreach my $cnSrcName (keys %srcConstraints) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
78 if ( my $cnDst = delete $dstConstraints{$cnSrcName} ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
79 unless ( $srcConstraints{$cnSrcName}->SameValue($cnDst) ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
80 push @dropConstraints,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
81 TraitsAlterTableDropConstraint->new( $src->name, $cnSrcName );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
82 push @createConstraints,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
83 TraitsAlterTableAddConstraint->new( $dst->name, _Constraint2Traits($cnDst) );
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 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
86 push @dropConstraints,TraitsAlterTableDropConstraint->new( $src->name, $cnSrcName );
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 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
89
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
90 foreach my $cnDst (values %dstConstraints) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
91 push @createConstraints,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
92 TraitsAlterTableAddConstraint->new( $dst->name, _Constraint2Traits($cnDst) );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
93 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
94
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
95 my @deleteColumns;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
96 my @addColumns;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
97 my @updateColumns;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
98
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
99 my %dstColumnIndexes = map {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
100 my $col = $dst->GetColumnAt($_);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
101 ($col->name, { column => $col, index => $_ })
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
102 } 0 .. $dst->ColumnsCount-1;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
103
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
104 my @columns;
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 # remove old columns, mark for update changed columns
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
107 for( my $i=0; $i < $src->ColumnsCount; $i++) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
108 my $colSrc = $src->GetColumnAt($i);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
109
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
110 if ( my $infoDst = delete $dstColumnIndexes{$colSrc->name} ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
111 $infoDst->{prevColumn} = $colSrc;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
112 push @columns,$infoDst;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
113 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
114 push @deleteColumns,TraitsAlterTableDropColumn->new($src->name,$colSrc->name);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
115 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
116 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
117
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
118 #insert new columns at specified positions
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
119 foreach ( sort { $a->{index} <=> $b->{index} } values %dstColumnIndexes ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
120 splice(@columns,$_->{index},0,$_);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
121 push @addColumns, TraitsAlterTableAddColumn->new($src->name, _Column2Traits( $_->{column}, position => $_->{index} ));
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
122 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
123
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
124 # remember old indexes
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
125 for(my $i =0; $i< @columns; $i ++) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
126 $columns[$i]->{prevIndex} = $i;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
127 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
128
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
129 # reorder columns
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
130 @columns = sort { $a->{index} <=> $b->{index} } @columns;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
131
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
132 foreach my $info (@columns) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
133 if ($info->{prevColumn} && ( !$info->{column}->SameValue($info->{prevColumn}) or $info->{index}!= $info->{prevIndex} ) ) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
134 my $op = TraitsAlterTableChangeColumn->new($src->name,$info->{column}->name);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
135
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
136 $op->position( $info->{index} ) unless $info->{prevIndex} == $info->{index};
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
137 $op->isNullable( $info->{column}->isNullable ) unless equals($info->{column}->isNullable,$info->{prevColumn}->isNullable);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
138 $op->defaultValue( $info->{column}->defaultValue ) unless equals($info->{column}->defaultValue, $info->{prevColumn}->defaultValue);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
139
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
140 my $diff = hashDiff($info->{prevColumn}->tag,$info->{column}->tag);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
141 $op->options($diff) if %$diff;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
142
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
143 push @updateColumns, $op;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
144 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
145 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
146
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
147 my @result = (@dropConstraints, @deleteColumns, @addColumns, @updateColumns, @createConstraints);
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
148
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
149 return @result;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
150 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
151
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
152 sub _Column2Traits {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
153 my ($column,%options) = @_;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
154
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
155 return TraitsColumn->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
156 $column->name,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
157 $column->type,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
158 isNullable => $column->isNullable,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
159 defaultValue => $column->defaultValue,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
160 tag => $column->tag,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
161 %options
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
162 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
163 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
164
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
165 sub _Constraint2Traits {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
166 my ($constraint) = @_;
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
167
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
168 my $map = {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
169 ForeignKey , TraitsForeignKey,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
170 PrimaryKey , TraitsPrimaryKey,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
171 UniqueConstraint , TraitsUnique,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
172 Index , TraitsIndex
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
173 };
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
174
315
77df11605d3a code cleanup
cin
parents: 271
diff changeset
175 my $class = $map->{typeof($constraint)} or die Exception->new("Can't map the constraint",typeof($constraint));
271
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
176
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
177 if ($class eq TraitsForeignKey) {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
178 return $class->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
179 $constraint->name,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
180 [ map $_->name, $constraint->columns ],
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
181 $constraint->referencedPrimaryKey->table->name,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
182 [ map $_->name, $constraint->referencedPrimaryKey->columns ]
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
183 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
184 } else {
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
185 return $class->new(
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
186 $constraint->name,
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
187 [ map $_->name, $constraint->columns ]
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
188 );
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
189 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
190 }
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
191
56364d0c4b4f +IMPL::SQL::Schema::MySQL: added basic support for MySQL
cin
parents:
diff changeset
192 1;