annotate lib/IMPL/SQL/Schema/MySQL/Formatter.pm @ 412:30e8c6a74937 ref20150831

working on di container (role based registrations)
author cin
date Mon, 21 Sep 2015 19:54:10 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::SQL::Schema::MySQL::Formatter;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::lang qw(is);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 Exception => 'IMPL::Exception',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 OpException => '-IMPL::InvalidOperationException',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 ArgException => '-IMPL::InvalidArgumentException',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 PrimaryKey => '-IMPL::SQL::Schema::Constraint::PrimaryKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 UniqueIndex => '-IMPL::SQL::Schema::Constraint::Unique',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 Index => '-IMPL::SQL::Schema::Constraint::Index',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 ForeignKey => '-IMPL::SQL::Schema::Constraint::ForeignKey',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 CharType => '-IMPL::SQL::Schema::MySQL::CharType',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 EnumType => '-IMPL::SQL::Schema::MySQL::EnumType',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 TraitsDropTable => '-IMPL::SQL::Schema::Traits::DropTable',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 TraitsCreateTable => '-IMPL::SQL::Schema::Traits::CreateTable',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 TraitsAlterTableDropConstraint => '-IMPL::SQL::Schema::Traits::AlterTableDropConstraint',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 TraitsAlterTableAddConstraint => '-IMPL::SQL::Schema::Traits::AlterTableAddConstraint',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 TraitsAlterTableDropColumn => '-IMPL::SQL::Schema::Traits::AlterTableDropColumn',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 TraitsAlterTableAddColumn => '-IMPL::SQL::Schema::Traits::AlterTableAddColumn',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 TraitsAlterTableChangeColumn => '-IMPL::SQL::Schema::Traits::AlterTableChangeColumn'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 our %TRAITS_FORMATS = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 TraitsDropTable, 'FormatDropTable',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 TraitsCreateTable, 'FormatCreateTable',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 TraitsAlterTableDropConstraint, 'FormatAlterTableDropConstraint',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 TraitsAlterTableAddConstraint, 'FormatAlterTableAddConstraint',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 TraitsAlterTableDropColumn, 'FormatAlterTableDropColumn',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 TraitsAlterTableAddColumn, 'FormatAlterTableAddColumn',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 TraitsAlterTableChangeColumn, 'FormatAlterTableChangeColumn'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 sub quote {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my $self = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 if (wantarray) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 return map { my $str = $_; $str =~ s/'/''/g; "'$str'"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 return join '', map { my $str = $_; $str =~ s/'/''/g; "'$str'"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 sub quote_names {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 my $self = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 if (wantarray) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 return map { my $str = $_; $str =~ s/`/``/g; "`$str`"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 return join '', map { my $str = $_; $str =~ s/`/``/g; "`$str`"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 sub formatTypeNameInteger {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 return
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 $type->name
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 . ( $type->maxLength ? '(' . $type->maxLength . ')' : '' )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 . ( $type->unsigned ? ' UNSIGNED' : '' )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 . ( $type->zerofill ? ' ZEROFILL' : '' );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 sub formatTypeNameReal {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 return $type->name
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 . ( $type->maxLength
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 ? '(' . $type->maxLength . ', ' . $type->scale . ')'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 : '' )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 . ( $type->unsigned ? ' UNSIGNED' : '' )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 . ( $type->zerofill ? ' ZEROFILL' : '' );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 sub formatTypeNameNumeric {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 $type->maxLength
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 or die ArgException->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 type => 'The length and precission must be specified',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 $type->name
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 return $type->name
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 . ( $type->maxLength
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 ? '(' . $type->maxLength . ', ' . $type->scale . ')'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 : '' )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 . ( $type->unsigned ? ' UNSIGNED' : '' )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 . ( $type->zerofill ? ' ZEROFILL' : '' );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 sub formatTypeName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 return $type->name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 sub formatTypeNameChar {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 return ($type->name . '('
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 . $type->MaxLength . ')'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 . ( is( $type, CharType ) ? $type->encoding : '' ) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 sub formatTypeNameVarChar {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 return ($type->name . '('
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 . $type->maxLength . ')'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 . ( is( $type, CharType ) ? $type->encoding : '' ) );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 sub formatTypeNameEnum {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 die ArgException->new( type => 'Invalid enum type' )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 unless is( $type, EnumType );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 return ($type->name . '('
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 . join( ',', map { $self->quote($_) } $type->enumValues )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 . ')' );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 sub formatStringValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 my ( $self, $value ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 if ( ref $value eq 'SCALAR' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 return $$value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 return $self->quote($value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 sub formatNumberValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 my ( $self, $value ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 if ( ref $value eq 'SCALAR' ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 return $$value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 $value =~ /^((\+|-)\s*)?\d+(\.\d+)?(e(\+|-)?\d+)?$/
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 or die ArgException->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 value => 'The specified value isn\'t a valid number',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 $value
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 return $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 our %TYPES_FORMATS = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 TINYINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 SMALLINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 MEDIUMINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 INT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 INTEGER => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 BIGINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 REAL => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 formatType => \&formatTypeNameReal,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 DOUBLE => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 formatType => \&formatTypeNameReal,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 FLOAT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 formatType => \&formatTypeNameReal,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 DECIMAL => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 formatType => \&formatTypeNameNumeric,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 NUMERIC => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 formatType => \&formatTypeNameNumeric,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195 DATE => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199 TIME => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 TIMESTAMP => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 DATETIME => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 CHAR => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 formatType => \&formatTypeNameChar,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215 VARCHAR => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 formatType => \&formatTypeNameVarChar,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
218 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
219 TINYBLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
220 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
221 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
222 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
223 BLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
224 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
225 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
226 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
227 MEDIUMBLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
228 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
229 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
230 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
231 LONGBLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
232 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
233 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
234 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
235 TINYTEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
236 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
237 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
238 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
239 TEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
240 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
241 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
242 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
243 MEDIUMTEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
244 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
245 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
246 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
247 LONGTEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
248 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
249 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
250 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
251 ENUM => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
252 formatType => \&formatTypeNameEnum,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
253 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
254 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
255 SET => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
256 formatType => \&formatTypeNameEnum,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
257 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
258 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
259 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
260
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
261 sub FormatTypeName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
262 my ( $self, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
263
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
264 my $fn = $TYPES_FORMATS{ $type->name }{formatType}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
265 or die ArgException->new( type => "The specified type is unknown",
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
266 $type->name );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
267
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
268 return $self->$fn($type);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
269 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
270
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
271 sub FormatValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
272 my ( $self, $value, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
273
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
274 my $fn = $TYPES_FORMATS{ $type->name }{formatValue}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
275 or die ArgException->new( type => "The specified type is unknown",
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
276 $type->name );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
277
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
278 return $self->$fn( $value, $type );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
279 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
280
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
281 sub FormatColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
282 my ( $self, $column ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
283
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
284 my @parts = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
285 $self->quote_names( $column->{name} ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
286 $self->FormatTypeName( $column->{type} ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
287 $column->{isNullable} ? 'NULL' : 'NOT NULL'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
288 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
289
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
290 push @parts, $self->FormatValue( $column->{defaultValue}, $column->{type} )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
291 if $column->{defaultValue};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
292
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
293 push @parts, 'AUTO_INCREMENT'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
294 if $column->{tag} and $column->{tag}->{auto_increment};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
295
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
296 return join ' ', @parts;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
297 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
298
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
299 sub FormatCreateTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
300 my ( $self, $op ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
301
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
302 my $table = $op->table;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
303
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
304 my @lines;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
305 my @body;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
306
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
307 push @lines, "CREATE TABLE " . $self->quote_names($table->{name}) . "(";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
308
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
309 push @body, map { " " . $self->FormatColumn($_) } @{ $table->{columns} }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
310 if $table->{columns};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
311
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
312 push @body, map { " " . $self->FormatConstraint($_) } @{ $table->{constraints} }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
313 if $table->{constraints};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
314
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
315 push @lines, join(",\n", @body);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
316
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
317 push @lines, ");";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
318
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
319 return join "\n", @lines;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
320 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
321
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
322 sub FormatDropTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
323 my ( $self, $op ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
324
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
325 return join ' ', 'DROP TABLE', $self->quote_names( $op->tableName ), ';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
326 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
327
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
328 sub FormatRenameTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
329 my ( $self, $op ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
330
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
331 return join ' ',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
332 'ALTER TABLE',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
333 $self->quote_names( $op->tableName ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
334 'RENAME TO',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
335 $self->quote_names( $op->tableNewName ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
336 ';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
337 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
338
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
339 sub FormatAlterTableAddColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
340 my ( $self, $op, $schema ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
341
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
342 my @parts = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
343 'ALTER TABLE',$self->quote_names($op->tableName), 'ADD COLUMN',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
344 $self->FormatColumn( $op->column )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
345 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
346
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
347 if ( defined $op->position ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
348
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
349 # mysql supports column reordering
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
350 # the new location is specified relative to the previous column
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
351 # to determine the name of the previous column we need to ask the schema
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
352
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
353 my $table = $schema->GetTable( $op->tableName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
354
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
355 if ( $op->position == 0 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
356 push @parts, 'FIRST';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
357 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
358 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
359 push @parts, 'AFTER';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
360
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
361 my $prevColumn = $table->GetColumnAt( $op->position - 1 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
362 push @parts, $self->quote_names( $prevColumn->{name} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
363 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
364 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
365
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
366 push @parts, ';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
367
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
368 return join ' ', @parts;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
369 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
370
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
371 sub FormatAlterTableDropColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
372 my ( $self, $op ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
373
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
374 return join ' ',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
375 'ALTER TABLE',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
376 $self->quote_names( $op->tableName ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
377 'DROP COLUMN',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
378 $self->quote_names( $op->columnName ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
379 ';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
380 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
381
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
382 sub FormatAlterTableChangeColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
383 my ( $self, $op, $schema ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
384
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
385 my $table = $schema->GetTable( $op->tableName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
386 my $column = $table->GetColumn( $op->columnName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
387
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
388 my @parts = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
389 'ALTER TABLE',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
390 $self->quote_names( $op->tableName ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
391 'MODIFY COLUMN',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
392 $self->quote_names( $op->columnName ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
393 $self->FormatColumn( $self->_Column2Traits($column) )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
394 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
395
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
396 if ( defined $op->position ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
397
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
398 # mysql supports column reordering
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
399 # the new location is specified relative to the previous column
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
400 # to determine the name of the previous column we need to ask the schema
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
401
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
402 if ( $op->position == 0 ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
403 push @parts, 'FIRST';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
404 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
405 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
406 push @parts, 'AFTER';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
408 my $prevColumn = $table->GetColumnAt( $op->position - 1 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
409 push @parts, $self->quote_names( $prevColumn->{name} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
410 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
411 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
412
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
413 push @parts, ';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
414 return join ' ', @parts;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
415 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
416
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
417 sub FormatConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
418 my ($self,$constraint) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
419
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
420 my @fkRules =
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
421 ( 'RESTRICT', 'CASCADE', 'SET NULL', 'SET DEFAULT', 'NO ACTION' );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
422
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
423 my @parts;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
424
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
425 if ( $constraint->constraintClass eq ForeignKey ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
426 push @parts,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
427 'CONSTRAINT',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
428 $self->quote_names( $constraint->{name} ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
429 'FOREIGN KEY',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
430 $self->quote_names( $constraint->{name} ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
431 '(',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
432 join( ', ', $self->quote_names( @{ $constraint->{columns} || [] } ) ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
433 ')',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
434 'REFERENCES', $self->quote_names( $constraint->{foreignTable} ), '(',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
435 join( ', ',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
436 $self->quote_names( @{ $constraint->{foreignColumns} || [] } ) ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
437 ')';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
438
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
439 if ( my $rule = $constraint->{onDelete} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
440 $rule = uc($rule);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
441 grep $_ eq $rule, @fkRules
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
442 or die Exception->new( "Invalid onDelete rule specified",
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
443 $constraint->{name}, $rule );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
444
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
445 push @parts, 'ON DELETE', $rule;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
446 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
447
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
448 if ( my $rule = $constraint->{onUpdate} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
449 $rule = uc($rule);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
450 grep $_ eq $rule, @fkRules
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
451 or die Exception->new( "Invalid onUpdate rule specified",
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
452 $constraint->{name}, $rule );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
453
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
454 push @parts, 'ON UPDATE', $rule;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
455 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
456
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
457 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
458 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
459 if ( $constraint->constraintClass eq PrimaryKey ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
460 push @parts, 'PRIMARY KEY';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
461
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
462 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
463 elsif ( $constraint->constraintClass eq UniqueIndex ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
464 push @parts, 'UNIQUE', $self->quote_names( $constraint->{name} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
465 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
466 elsif ( $constraint->constraintClass eq Index ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
467 push @parts, 'INDEX', $self->quote_names( $constraint->{name} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
468 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
469 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
470 die Exception->new( 'Invalid constraint type',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
471 $constraint->constraintClass );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
472 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
473
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
474 push @parts,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
475 '(',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
476 join( ', ', $self->quote_names( @{ $constraint->{columns} || [] } ) ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
477 ')';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
478 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
479
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
480
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
481 return join ' ', @parts;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
482 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
483
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
484 sub FormatAlterTableAddConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
485 my ( $self, $op ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
486
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
487 return join(' ',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
488 'ALTER TABLE',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
489 $self->quote_names( $op->tableName ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
490 'ADD',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
491 $self->FormatConstraint($op->constraint),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
492 ';'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
493 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
494 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
495
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
496 sub FormatAlterTableDropConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
497 my ( $self, $op, $constraintType ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
498
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
499 my @parts = ( 'ALTER TABLE', $self->quote_names( $op->tableName ), 'DROP' );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
500
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
501 if ( $constraintType eq PrimaryKey ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
502 push @parts, 'PRIMARY KEY';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
503 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
504 elsif ( $constraintType eq ForeignKey ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
505 push @parts, 'FOREIGN KEY', $self->quote_names( $op->constraintName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
506 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
507 elsif ( $constraintType eq UniqueIndex or $constraintType eq Index ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
508 push @parts, 'INDEX', $self->quote_names( $op->constraintName );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
509 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
510 else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
511 die Exception->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
512 'Invalid constraint type', $op->tableName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
513 $op->constraintName, $constraintType
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
514 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
515 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
516
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
517 push @parts, ';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
518
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
519 return join ' ', @parts;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
520 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
521
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
522 sub Format {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
523 my $self = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
524 my ($op) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
525
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
526 my $formatter = $TRAITS_FORMATS{ref $op}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
527 or die OpException->new("Don't know how to format the specified operation", $op);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
528
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
529 $self->$formatter(@_);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
530 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
531
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
532 sub _Column2Traits {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
533 my ( $self, $column, %options ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
534
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
535 return new IMPL::SQL::Schema::Traits::Column(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
536 $column->name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
537 $column->type,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
538 isNullable => $column->isNullable,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
539 defaultValue => $column->defaultValue,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
540 tag => $column->tag,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
541 %options
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
542 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
543 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
544
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
545 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
546
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
547 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
548
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
549 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
550
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
551 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
552
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
553 C<IMPL::SQL::Traits::MysqlFormatter> - преобразует операции над схемой в C<SQL>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
554 выражения.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
555
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
556 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
557
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
558 Используется для форматирования операций изменения схемы БД. Осуществляет
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
559 правильное экранирование имен, форматирование значений, имен типов данных.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
560
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
561 =cut