annotate lib/IMPL/SQL/Schema/Traits/mysql.pm @ 411:ee36115f6a34 ref20150831

sync
author cin
date Mon, 21 Sep 2015 00:53: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::Traits::mysql::Handler;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use parent qw(IMPL::Object);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 public _direct property SqlBatch => prop_all;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 sub formatTypeNameInteger {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 return $type->Name.($type->MaxLength ? '('.$type->MaxLength.')' : '').($type->Unsigned ? ' UNSIGNED': '').($type->Zerofill ? ' ZEROFILL' : '');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 sub formatTypeNameReal {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 return $type->Name.($type->MaxLength ? '('.$type->MaxLength.', '.$type->Scale.')' : '').($type->Unsigned ? ' UNSIGNED': '').($type->Zerofill ? ' ZEROFILL' : '');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 sub formatTypeNameNumeric {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 $type->MaxLength or die new IMPL::InvalidArgumentException('The length and precission must be specified',$type->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 return $type->Name.($type->MaxLength ? '('.$type->MaxLength.', '.$type->Scale.')' : '').($type->Unsigned ? ' UNSIGNED': '').($type->Zerofill ? ' ZEROFILL' : '');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 sub formatTypeName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 return $type->Name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 sub formatTypeNameChar {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 return (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 $type->Name.'('.$type->MaxLength.')'. (UNIVERSAL::isa($type,'IMPL::SQL::Schema::Type::mysql::CHAR') ? $type->Encoding : '')
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 sub formatTypeNameVarChar {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 return (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 $type->Name.'('.$type->MaxLength.')'. (UNIVERSAL::isa($type,'IMPL::SQL::Schema::Type::mysql::VARCHAR') ? $type->Encoding : '')
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 sub formatTypeNameEnum {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 die new Exception('Enum must be a type of either IMPL::SQL::Schema::Type::mysql::ENUM or IMPL::SQL::Schema::Type::mysql::SET') if not (UNIVERSAL::isa($type,'IMPL::SQL::Schema::Type::mysql::ENUM') or UNIVERSAL::isa($type,'IMPL::SQL::Schema::Type::mysql::SET'));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 return (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $type->Name.'('.join(',',map {quote($_)} $type->Values).')'
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 sub quote{
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 if (wantarray) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 return map { my $str=$_; $str=~ s/'/''/g; "'$str'"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 return join '',map { my $str=$_; $str=~ s/'/''/g; "'$str'"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 sub quote_names {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 if (wantarray) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 return map { my $str=$_; $str=~ s/`/``/g; "`$str`"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 return join '',map { my $str=$_; $str=~ s/`/``/g; "`$str`"; } @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 sub formatStringValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 my ($value) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 if (ref $value) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 if (UNIVERSAL::isa($value,'IMPL::SQL::Schema::mysql::Expression')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 return $value->as_string;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 die new Exception('Can\'t format the object as a value',ref $value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 return quote($value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 sub formatNumberValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 my ($value) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 if (ref $value) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 if (UNIVERSAL::isa($value,'IMPL::SQL::Schema::mysql::Expression')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 return $value->as_string;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 die new Exception('Can\'t format the object as a value',ref $value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 $value =~ /^((\+|-)\s*)?\d+(\.\d+)?(e(\+|-)?\d+)?$/ or die new Exception('The specified value isn\'t a valid number',$value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 return $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 my %TypesFormat = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 TINYINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 SMALLINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 MEDIUMINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 INT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 INTEGER => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 BIGINT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 formatType => \&formatTypeNameInteger,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 REAL => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 formatType => \&formatTypeNameReal,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 DOUBLE => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 formatType => \&formatTypeNameReal,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 FLOAT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 formatType => \&formatTypeNameReal,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 DECIMAL => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 formatType => \&formatTypeNameNumeric,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 NUMERIC => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 formatType => \&formatTypeNameNumeric,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 formatValue => \&formatNumberValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 DATE => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 TIME => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157 TIMESTAMP => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 DATETIME => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 CHAR => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 formatType => \&formatTypeNameChar,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169 VARCHAR => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 formatType => \&formatTypeNameVarChar,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 TINYBLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 BLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 MEDIUMBLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 LONGBLOB => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 TINYTEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 TEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197 MEDIUMTEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201 LONGTEXT => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202 formatType => \&formatTypeName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205 ENUM => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 formatType => \&formatTypeNameEnum,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209 SET => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 formatType => \&formatTypeNameEnum,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 formatValue => \&formatStringValue
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217 CREATE TABLE 'test'.'New Table' (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
218 'dd' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
219 `ff` VARCHAR(45) NOT NULL,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
220 `ffg` VARCHAR(45) NOT NULL DEFAULT 'aaa',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
221 `ddf` INTEGER UNSIGNED NOT NULL,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
222 PRIMARY KEY(`dd`),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
223 UNIQUE `Index_2`(`ffg`),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
224 CONSTRAINT `FK_New Table_1` FOREIGN KEY `FK_New Table_1` (`ddf`)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
225 REFERENCES `user` (`id`)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
226 ON DELETE RESTRICT
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
227 ON UPDATE RESTRICT
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
228 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
229 ENGINE = InnoDB;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
230 =cut
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
231 sub formatCreateTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
232 my ($table,$level,%options) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
233
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
234 my @sql;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
235
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
236 # table body
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
237 push @sql, map { formatColumn($_,$level+1) } @{$table->Columns} ;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
238 if ($options{'skip_foreign_keys'}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
239 push @sql, map { formatConstraint($_,$level+1) } grep {not UNIVERSAL::isa($_,'IMPL::SQL::Schema::Constraint::ForeignKey')} values %{$table->Constraints};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
240 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
241 push @sql, map { formatConstraint($_,$level+1) } values %{$table->Constraints};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
242 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
243
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
244 for(my $i = 0 ; $i < @sql -1; $i++) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
245 $sql[$i] .= ',';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
246 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
247
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
248 unshift @sql, "CREATE TABLE ".quote_names($table->Name)."(";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
249
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
250 if ($table->Tag) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
251 push @sql, ")";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
252 push @sql, formatTableTag($table->Tag,$level);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
253 $sql[$#sql].=';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
254 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
255 push @sql, ');';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
256 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
257
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
258 return map { (" " x $level) . $_ } @sql;
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 formatDropTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
262 my ($tableName,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
263
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
264 return " "x$level."DROP TABLE ".quote_names($tableName).";";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
265 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
266
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
267 sub formatTableTag {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
268 my ($tag,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
269 return map { " "x$level . "$_ = ".$tag->{$_} } grep {/^(ENGINE)$/i} keys %{$tag};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
270 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
271
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
272 sub formatColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
273 my ($column,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
274 $level ||= 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
275 return " "x$level.quote_names($column->Name)." ".formatType($column->Type)." ".($column->CanBeNull ? 'NULL' : 'NOT NULL').($column->DefaultValue ? formatValueToType($column->DefaultValue,$column->Type) : '' ).($column->Tag ? ' '.join(' ',$column->Tag) : '');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
276 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
277
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
278 sub formatType {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
279 my ($type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
280 my $format = $TypesFormat{uc $type->Name} or die new Exception('The unknown type name',$type->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
281 $format->{formatType}->($type);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
282 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
283
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
284 sub formatValueToType {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
285 my ($value,$type) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
286
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
287 my $format = $TypesFormat{uc $type->Name} or die new Exception('The unknown type name',$type->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
288 $format->{formatValue}->($value);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
289 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
290
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
291 sub formatConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
292 my ($constraint,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
293
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
294 if (UNIVERSAL::isa($constraint,'IMPL::SQL::Schema::Constraint::ForeignKey')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
295 return formatForeignKey($constraint,$level);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
296 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
297 return formatIndex($constraint, $level);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
298 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
299 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
300
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
301 sub formatIndex {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
302 my ($constraint,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
303
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
304 my $name = quote_names($constraint->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
305 my $columns = join(',',map quote_names($_->Name),@{$constraint->Columns});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
306
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
307 if (ref $constraint eq 'IMPL::SQL::Schema::Constraint::PrimaryKey') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
308 return " "x$level."PRIMARY KEY ($columns)";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
309 } elsif ($constraint eq 'IMPL::SQL::Schema::Constraint::Unique') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
310 return " "x$level."UNIQUE $name ($columns)";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
311 } elsif ($constraint eq 'IMPL::SQL::Schema::Constraint::Index') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
312 return " "x$level."INDEX $name ($columns)";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
313 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
314 die new IMPL::InvalidArgumentException('The unknown constraint', ref $constraint);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
315 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
316
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
317 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
318
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
319 sub formatForeignKey {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
320 my ($constraint,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
321
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
322 my $name = quote_names($constraint->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
323 my $columns = join(',',map quote_names($_->Name),@{$constraint->Columns});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
324
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
325 not $constraint->OnDelete or grep { uc $constraint->OnDelete eq $_ } ('RESTRICT','CASCADE','SET NULL','NO ACTION','SET DEFAULT') or die new IMPL::Exception('Invalid ON DELETE reference',$constraint->OnDelete);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
326 not $constraint->OnUpdate or grep { uc $constraint->OnUpdate eq $_ } ('RESTRICT','CASCADE','SET NULL','NO ACTION','SET DEFAULT') or die new IMPL::Exception('Invalid ON UPDATE reference',$constraint->OnUpdate);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
327
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
328 my $refname = quote_names($constraint->ReferencedPrimaryKey->Table->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
329 my $refcolumns = join(',',map quote_names($_->Name),@{$constraint->ReferencedPrimaryKey->Columns});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
330 return (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
331 " "x$level.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
332 "CONSTRAINT $name FOREIGN KEY $name ($columns) REFERENCES $refname ($refcolumns)".
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
333 ($constraint->OnUpdate ? 'ON UPDATE'.$constraint->OnUpdate : '').
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
334 ($constraint->OnDelete ? 'ON DELETE'.$constraint->OnDelete : '')
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
335 );
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 sub formatAlterTableRename {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
339 my ($oldName,$newName,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
340
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
341 return " "x$level."ALTER TABLE ".quote_names($oldName)." RENAME TO ".quote_names($newName).";";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
342 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
343
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
344 sub formatAlterTableDropColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
345 my ($tableName, $columnName,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
346
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
347 return " "x$level."ALTER TABLE ".quote_names($tableName)." DROP COLUMN ".quote_names($columnName).";";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
348 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
349
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
350 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
351 ALTER TABLE `test`.`user` ADD COLUMN `my_col` VARCHAR(45) NOT NULL AFTER `name2`
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
352 =cut
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
353 sub formatAlterTableAddColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
354 my ($tableName, $column, $table, $pos, $level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
355
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
356 my $posSpec = $pos == 0 ? 'FIRST' : 'AFTER '.quote_names($table->ColumnAt($pos-1)->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
357
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
358 return " "x$level."ALTER TABLE ".quote_names($tableName)." ADD COLUMN ".formatColumn($column) .' '. $posSpec.";";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
359 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
360
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
361 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
362 ALTER TABLE `test`.`manager` MODIFY COLUMN `description` VARCHAR(256) NOT NULL DEFAULT NULL;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
363 =cut
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
364 sub formatAlterTableChangeColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
365 my ($tableName,$column,$table,$pos,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
366 my $posSpec = $pos == 0 ? 'FIRST' : 'AFTER '.quote_names($table->ColumnAt($pos-1)->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
367 return " "x$level."ALTER TABLE ".quote_names($tableName)." MODIFY COLUMN ".formatColumn($column).' '. $posSpec.";";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
368 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
369
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
370 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
371 ALTER TABLE `test`.`manager` DROP INDEX `Index_2`;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
372 =cut
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
373 sub formatAlterTableDropConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
374 my ($tableName,$constraint,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
375 my $constraintName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
376 if (ref $constraint eq 'IMPL::SQL::Schema::Constraint::PrimaryKey') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
377 $constraintName = 'PRIMARY KEY';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
378 } elsif (ref $constraint eq 'IMPL::SQL::Schema::Constraint::ForeignKey') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
379 $constraintName = 'FOREIGN KEY '.quote_names($constraint->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
380 } elsif (UNIVERSAL::isa($constraint,'IMPL::SQL::Schema::Constraint::Index')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
381 $constraintName = 'INDEX '.quote_names($constraint->Name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
382 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
383 die new IMPL::Exception("The unknow type of the constraint",ref $constraint);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
384 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
385 return " "x$level."ALTER TABLE ".quote_names($tableName)." DROP $constraintName;";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
386 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
387
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
388 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
389 ALTER TABLE `test`.`session` ADD INDEX `Index_2`(`id`, `name`);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
390 =cut
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
391 sub formatAlterTableAddConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
392 my ($tableName,$constraint,$level) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
393
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
394 return " "x$level."ALTER TABLE ".quote_names($tableName)." ADD ".formatConstraint($constraint,0).';';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
395 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
396
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
397 sub CreateTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
398 my ($this,$tbl,%option) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
399
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
400 push @{$this->{$SqlBatch}},join("\n",formatCreateTable($tbl,0,%option));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
401
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
402 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
403 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
404
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
405 sub DropTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
406 my ($this,$tbl) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
408 push @{$this->{$SqlBatch}},join("\n",formatDropTable($tbl,0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
409
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
410 return 1;
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 sub RenameTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
414 my ($this,$oldName,$newName) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
415
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
416 push @{$this->{$SqlBatch}},join("\n",formatAlterTableRename($oldName,$newName,0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
417
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
418 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
419 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
420
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
421 sub AlterTableAddColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
422 my ($this,$tblName,$column,$table,$pos) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
423
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
424 push @{$this->{$SqlBatch}},join("\n",formatAlterTableAddColumn($tblName,$column,$table,$pos,0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
425
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
426 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
427 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
428 sub AlterTableDropColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
429 my ($this,$tblName,$columnName) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
430
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
431 push @{$this->{$SqlBatch}},join("\n",formatAlterTableDropColumn($tblName,$columnName,0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
432
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
433 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
434 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
435
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
436 sub AlterTableChangeColumn {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
437 my ($this,$tblName,$column,$table,$pos) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
438
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
439 push @{$this->{$SqlBatch}},join("\n",formatAlterTableChangeColumn($tblName,$column,$table,$pos,0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
440
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
441 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
442 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
443
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
444 sub AlterTableAddConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
445 my ($this,$tblName,$constraint) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
446
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
447 push @{$this->{$SqlBatch}},join("\n",formatAlterTableAddConstraint($tblName,$constraint,0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
448
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
449 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
450 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
451
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
452 sub AlterTableDropConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
453 my ($this,$tblName,$constraint) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
454
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
455 push @{$this->{$SqlBatch}},join("\n",formatAlterTableDropConstraint($tblName,$constraint,0));
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
456
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
457 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
458 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
459
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
460 sub Sql {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
461 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
462 if (wantarray) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
463 @{$this->SqlBatch || []};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
464 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
465 return join("\n",$this->SqlBatch);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
466 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
467 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
468
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
469 package IMPL::SQL::Schema::Traits::mysql;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
470 use parent qw(IMPL::SQL::Schema::Traits);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
471 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
472
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
473 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
474 public _direct property PendingConstraints => prop_none;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
475 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
476
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
477 our %CTOR = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
478 'IMPL::SQL::Schema::Traits' => sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
479 my %args = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
480 $args{'Handler'} = new IMPL::SQL::Schema::Traits::mysql::Handler;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
481 %args;
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
485 sub DropConstraint {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
486 my ($this,$constraint) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
487
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
488 if (UNIVERSAL::isa($constraint,'IMPL::SQL::Schema::Constraint::Index')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
489 return 1 if not grep { $this->TableInfo->{$this->MapTableName($constraint->Table->Name)}->{'Columns'}->{$_->Name} != IMPL::SQL::Schema::Traits::STATE_REMOVED} $constraint->Columns;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
490 my @constraints = grep {$_ != $constraint } $constraint->Table->GetColumnConstraints($constraint->Columns);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
491 if (scalar @constraints == 1 and UNIVERSAL::isa($constraints[0],'IMPL::SQL::Schema::Constraint::ForeignKey')) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
492 my $fk = shift @constraints;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
493 if ($this->TableInfo->{$this->MapTableName($fk->Table->Name)}->{'Constraints'}->{$fk->Name} != IMPL::SQL::Schema::Traits::STATE_REMOVED) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
494 push @{$this->PendingActions}, {Action => \&DropConstraint, Args => [$constraint]};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
495 $this->{$PendingConstraints}->{$constraint->UniqName}->{'attempts'} ++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
496
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
497 die new IMPL::Exception('Can\'t drop the primary key becouse of the foreing key',$fk->UniqName) if $this->{$PendingConstraints}->{$constraint->UniqName}->{'attempts'} > 2;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
498 return 2;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
499 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
500 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
501 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
502 $this->SUPER::DropConstraint($constraint);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
503 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
504
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
505 sub GetMetaTable {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
506 my ($class,$dbh) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
507
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
508 return IMPL::SQL::Schema::Traits::mysql::MetaTable->new( DBHandle => $dbh);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
509 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
510
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
511 package IMPL::SQL::Schema::Traits::mysql::MetaTable;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
512 use parent qw(IMPL::Object);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
513 use IMPL::Class::Property;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
514
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
515 BEGIN {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
516 public _direct property DBHandle => prop_none;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
517 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
518
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
519 sub ReadProperty {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
520 my ($this,$name) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
521
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
522 local $this->{$DBHandle}->{PrintError};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
523 $this->{$DBHandle}->{PrintError} = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
524 my ($val) = $this->{$DBHandle}->selectrow_array("SELECT value FROM _Meta WHERE name like ?", undef, $name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
525 return $val;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
526 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
527
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
528 sub SetProperty {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
529 my ($this,$name,$val) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
530
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
531 if ( $this->{$DBHandle}->selectrow_arrayref("SELECT TABLE_NAME FROM information_schema.`TABLES` T where TABLE_SCHEMA like DATABASE() and TABLE_NAME like '_Meta'")) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
532 if ($this->{$DBHandle}->selectrow_arrayref("SELECT name FROM _Meta WHERE name like ?", undef, $name)) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
533 $this->{$DBHandle}->do("UPDATE _Meta SET value = ? WHERE name like ?",undef,$val,$name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
534 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
535 $this->{$DBHandle}->do("INSERT INTO _Meta(name,value) VALUES ('$name',?)",undef,$val);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
536 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
537 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
538 $this->{$DBHandle}->do(q{
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
539 CREATE TABLE `_Meta` (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
540 `name` VARCHAR(255) NOT NULL,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
541 `value` LONGTEXT NULL,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
542 PRIMARY KEY(`name`)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
543 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
544 }) or die new IMPL::Exception("Failed to create table","_Meta");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
545
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
546 $this->{$DBHandle}->do("INSERT INTO _Meta(name,value) VALUES (?,?)",undef,$name,$val);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
547 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
548 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
549
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
550 1;