annotate Lib/Schema/DB/Traits/mysql.pm @ 27:b544a772b654

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