comparison Lib/IMPL/SQL/Schema/Traits/mysql.pm @ 206:c8fe3f84feba

+IMPL::Web::Handlers::ViewSelector +IMPL::Web::Handlers::ErrorHandler *IMPL::Web::Handlers::RestController moved types mappings to ViewSelector
author sergey
date Thu, 03 May 2012 16:48:39 +0400
parents 76515373dac0
children 4ddb27ff4a0b
comparison
equal deleted inserted replaced
205:891c04080658 206:c8fe3f84feba
254 $sql[$#sql].=';'; 254 $sql[$#sql].=';';
255 } else { 255 } else {
256 push @sql, ');'; 256 push @sql, ');';
257 } 257 }
258 258
259 return map { ("\t" x $level) . $_ } @sql; 259 return map { (" " x $level) . $_ } @sql;
260 } 260 }
261 261
262 sub formatDropTable { 262 sub formatDropTable {
263 my ($tableName,$level) = @_; 263 my ($tableName,$level) = @_;
264 264
265 return "\t"x$level."DROP TABLE ".quote_names($tableName).";"; 265 return " "x$level."DROP TABLE ".quote_names($tableName).";";
266 } 266 }
267 267
268 sub formatTableTag { 268 sub formatTableTag {
269 my ($tag,$level) = @_; 269 my ($tag,$level) = @_;
270 return map { "\t"x$level . "$_ = ".$tag->{$_} } grep {/^(ENGINE)$/i} keys %{$tag}; 270 return map { " "x$level . "$_ = ".$tag->{$_} } grep {/^(ENGINE)$/i} keys %{$tag};
271 } 271 }
272 272
273 sub formatColumn { 273 sub formatColumn {
274 my ($column,$level) = @_; 274 my ($column,$level) = @_;
275 $level ||= 0; 275 $level ||= 0;
276 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) : ''); 276 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) : '');
277 } 277 }
278 278
279 sub formatType { 279 sub formatType {
280 my ($type) = @_; 280 my ($type) = @_;
281 my $format = $TypesFormat{uc $type->Name} or die new Exception('The unknown type name',$type->Name); 281 my $format = $TypesFormat{uc $type->Name} or die new Exception('The unknown type name',$type->Name);
304 304
305 my $name = quote_names($constraint->Name); 305 my $name = quote_names($constraint->Name);
306 my $columns = join(',',map quote_names($_->Name),@{$constraint->Columns}); 306 my $columns = join(',',map quote_names($_->Name),@{$constraint->Columns});
307 307
308 if (ref $constraint eq 'IMPL::SQL::Schema::Constraint::PrimaryKey') { 308 if (ref $constraint eq 'IMPL::SQL::Schema::Constraint::PrimaryKey') {
309 return "\t"x$level."PRIMARY KEY ($columns)"; 309 return " "x$level."PRIMARY KEY ($columns)";
310 } elsif ($constraint eq 'IMPL::SQL::Schema::Constraint::Unique') { 310 } elsif ($constraint eq 'IMPL::SQL::Schema::Constraint::Unique') {
311 return "\t"x$level."UNIQUE $name ($columns)"; 311 return " "x$level."UNIQUE $name ($columns)";
312 } elsif ($constraint eq 'IMPL::SQL::Schema::Constraint::Index') { 312 } elsif ($constraint eq 'IMPL::SQL::Schema::Constraint::Index') {
313 return "\t"x$level."INDEX $name ($columns)"; 313 return " "x$level."INDEX $name ($columns)";
314 } else { 314 } else {
315 die new IMPL::InvalidArgumentException('The unknown constraint', ref $constraint); 315 die new IMPL::InvalidArgumentException('The unknown constraint', ref $constraint);
316 } 316 }
317 317
318 } 318 }
327 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); 327 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);
328 328
329 my $refname = quote_names($constraint->ReferencedPrimaryKey->Table->Name); 329 my $refname = quote_names($constraint->ReferencedPrimaryKey->Table->Name);
330 my $refcolumns = join(',',map quote_names($_->Name),@{$constraint->ReferencedPrimaryKey->Columns}); 330 my $refcolumns = join(',',map quote_names($_->Name),@{$constraint->ReferencedPrimaryKey->Columns});
331 return ( 331 return (
332 "\t"x$level. 332 " "x$level.
333 "CONSTRAINT $name FOREIGN KEY $name ($columns) REFERENCES $refname ($refcolumns)". 333 "CONSTRAINT $name FOREIGN KEY $name ($columns) REFERENCES $refname ($refcolumns)".
334 ($constraint->OnUpdate ? 'ON UPDATE'.$constraint->OnUpdate : ''). 334 ($constraint->OnUpdate ? 'ON UPDATE'.$constraint->OnUpdate : '').
335 ($constraint->OnDelete ? 'ON DELETE'.$constraint->OnDelete : '') 335 ($constraint->OnDelete ? 'ON DELETE'.$constraint->OnDelete : '')
336 ); 336 );
337 } 337 }
338 338
339 sub formatAlterTableRename { 339 sub formatAlterTableRename {
340 my ($oldName,$newName,$level) = @_; 340 my ($oldName,$newName,$level) = @_;
341 341
342 return "\t"x$level."ALTER TABLE ".quote_names($oldName)." RENAME TO ".quote_names($newName).";"; 342 return " "x$level."ALTER TABLE ".quote_names($oldName)." RENAME TO ".quote_names($newName).";";
343 } 343 }
344 344
345 sub formatAlterTableDropColumn { 345 sub formatAlterTableDropColumn {
346 my ($tableName, $columnName,$level) = @_; 346 my ($tableName, $columnName,$level) = @_;
347 347
348 return "\t"x$level."ALTER TABLE ".quote_names($tableName)." DROP COLUMN ".quote_names($columnName).";"; 348 return " "x$level."ALTER TABLE ".quote_names($tableName)." DROP COLUMN ".quote_names($columnName).";";
349 } 349 }
350 350
351 =pod 351 =pod
352 ALTER TABLE `test`.`user` ADD COLUMN `my_col` VARCHAR(45) NOT NULL AFTER `name2` 352 ALTER TABLE `test`.`user` ADD COLUMN `my_col` VARCHAR(45) NOT NULL AFTER `name2`
353 =cut 353 =cut
354 sub formatAlterTableAddColumn { 354 sub formatAlterTableAddColumn {
355 my ($tableName, $column, $table, $pos, $level) = @_; 355 my ($tableName, $column, $table, $pos, $level) = @_;
356 356
357 my $posSpec = $pos == 0 ? 'FIRST' : 'AFTER '.quote_names($table->ColumnAt($pos-1)->Name); 357 my $posSpec = $pos == 0 ? 'FIRST' : 'AFTER '.quote_names($table->ColumnAt($pos-1)->Name);
358 358
359 return "\t"x$level."ALTER TABLE ".quote_names($tableName)." ADD COLUMN ".formatColumn($column) .' '. $posSpec.";"; 359 return " "x$level."ALTER TABLE ".quote_names($tableName)." ADD COLUMN ".formatColumn($column) .' '. $posSpec.";";
360 } 360 }
361 361
362 =pod 362 =pod
363 ALTER TABLE `test`.`manager` MODIFY COLUMN `description` VARCHAR(256) NOT NULL DEFAULT NULL; 363 ALTER TABLE `test`.`manager` MODIFY COLUMN `description` VARCHAR(256) NOT NULL DEFAULT NULL;
364 =cut 364 =cut
365 sub formatAlterTableChangeColumn { 365 sub formatAlterTableChangeColumn {
366 my ($tableName,$column,$table,$pos,$level) = @_; 366 my ($tableName,$column,$table,$pos,$level) = @_;
367 my $posSpec = $pos == 0 ? 'FIRST' : 'AFTER '.quote_names($table->ColumnAt($pos-1)->Name); 367 my $posSpec = $pos == 0 ? 'FIRST' : 'AFTER '.quote_names($table->ColumnAt($pos-1)->Name);
368 return "\t"x$level."ALTER TABLE ".quote_names($tableName)." MODIFY COLUMN ".formatColumn($column).' '. $posSpec.";"; 368 return " "x$level."ALTER TABLE ".quote_names($tableName)." MODIFY COLUMN ".formatColumn($column).' '. $posSpec.";";
369 } 369 }
370 370
371 =pod 371 =pod
372 ALTER TABLE `test`.`manager` DROP INDEX `Index_2`; 372 ALTER TABLE `test`.`manager` DROP INDEX `Index_2`;
373 =cut 373 =cut
381 } elsif (UNIVERSAL::isa($constraint,'IMPL::SQL::Schema::Constraint::Index')) { 381 } elsif (UNIVERSAL::isa($constraint,'IMPL::SQL::Schema::Constraint::Index')) {
382 $constraintName = 'INDEX '.quote_names($constraint->Name); 382 $constraintName = 'INDEX '.quote_names($constraint->Name);
383 } else { 383 } else {
384 die new IMPL::Exception("The unknow type of the constraint",ref $constraint); 384 die new IMPL::Exception("The unknow type of the constraint",ref $constraint);
385 } 385 }
386 return "\t"x$level."ALTER TABLE ".quote_names($tableName)." DROP $constraintName;"; 386 return " "x$level."ALTER TABLE ".quote_names($tableName)." DROP $constraintName;";
387 } 387 }
388 388
389 =pod 389 =pod
390 ALTER TABLE `test`.`session` ADD INDEX `Index_2`(`id`, `name`); 390 ALTER TABLE `test`.`session` ADD INDEX `Index_2`(`id`, `name`);
391 =cut 391 =cut
392 sub formatAlterTableAddConstraint { 392 sub formatAlterTableAddConstraint {
393 my ($tableName,$constraint,$level) = @_; 393 my ($tableName,$constraint,$level) = @_;
394 394
395 return "\t"x$level."ALTER TABLE ".quote_names($tableName)." ADD ".formatConstraint($constraint,0).';'; 395 return " "x$level."ALTER TABLE ".quote_names($tableName)." ADD ".formatConstraint($constraint,0).';';
396 } 396 }
397 397
398 sub CreateTable { 398 sub CreateTable {
399 my ($this,$tbl,%option) = @_; 399 my ($this,$tbl,%option) = @_;
400 400