# HG changeset patch # User cin # Date 1359059102 -14400 # Node ID 3f59fd828d5fe2a716d5ff72cc90714994b9241c # Parent 4abda21186cd455fb56ce53f2b74fc370c8850e1# Parent dacfe7c0311a9d61568adf8b280a65be4924ca4e merge diff -r 4abda21186cd -r 3f59fd828d5f Lib/IMPL/SQL/Schema/Table.pm --- a/Lib/IMPL/SQL/Schema/Table.pm Mon Jan 21 02:08:17 2013 +0400 +++ b/Lib/IMPL/SQL/Schema/Table.pm Fri Jan 25 00:25:02 2013 +0400 @@ -115,12 +115,47 @@ return $this->{$columns}[$index]; } -sub ColumnsCount { - my ($this) = @_; +sub SetColumnPosition { + my ($this,$nameOrColumn,$pos) = @_; + + my $colName; + if (is($nameOrColumn,'IMPL::SQL::Schema::Column')) { + $colName = $nameOrColumn->name; + } elsif (not ref $nameOrColumn) { + $colName = $nameOrColumn; + } else { + die IMPL::InvalidArgumentException->new(column => 'The specified column isn\'t found in the table'); + } + + die IMPL::InvalidArgumentException->new( 'pos' => 'The specified position is invalid') + if not defined $pos || $pos < 0 || $pos >= $this->columnsCount; + + my $index = 0; + foreach my $column(@{$this->{$columns}}) { + last if $column->name eq $colName; + $index++; + } + + if ($pos != $index) { + #position needs to be changed; + + my ($column) = splice @{$this->{$columns}}, $index, 1; + splice @{$this->{$columns}}, $pos, 0, $column; + } + + return; +} + +sub columnsCount { + my ($this) = @_; return scalar(@{$this->{$columns}}); } +sub ColumnsCount { + goto &columnsCount; +} + sub AddConstraint { my $this = shift; if (@_ == 1) { diff -r 4abda21186cd -r 3f59fd828d5f Lib/IMPL/SQL/Schema/Traits.pm --- a/Lib/IMPL/SQL/Schema/Traits.pm Mon Jan 21 02:08:17 2013 +0400 +++ b/Lib/IMPL/SQL/Schema/Traits.pm Fri Jan 25 00:25:02 2013 +0400 @@ -224,6 +224,7 @@ BEGIN { public property tableName => prop_get | owner_set; public property column => prop_get | owner_set; + public property position => prop_get | owner_set; } sub CTOR { @@ -290,6 +291,7 @@ public property columnType => prop_all; public property defaultValue => prop_all; public property isNullable => prop_all; + public property position => prop_all; public property options => prop_all; # hash diff format, (keys have a prefix '+' - add or update value, '-' remove value) } @@ -405,15 +407,13 @@ Данные, которые содержаться в примитивных операциях не могут существовать независимо от схемы. -=head1 OPEARATIONS +=head1 OPERATIONS =head2 General Методы обще для всех примитивных операций. -=over - -=item C +=head3 C Пытается приминить операцию к указанной схеме. @@ -431,164 +431,132 @@ =back -=back - =head2 Primitive operations -=over - -=item C +=head3 C Создает таблицу -=over +=head4 C -=item C - -=item C<[get]table> +=head4 C<[get]table> C - описание создаваемой таблицы -=back - -=item C +=head3 C Удалает таблицу по имени -=over +=head4 C -=item C - -=item C<[get]tableName> +=head4 C<[get]tableName> Имя удаляемой таблицы -=back - -=item C +=head3 C -=over +=head4 C -=item C - -=item C<[get]tableName> +=head4 C<[get]tableName> Имя таблицы, которую требуется переименовать -=item C<[get]tableNewName> +=head4 C<[get]tableNewName> Новое имя таблицы -=back - -=item C +=head3 C Добавляет столбец в таблицу -=over +=head4 C -=item C - -=item C<[get]tableName> +=head4 C<[get]tableName> Имя таблицы в которую нужно добавить столбец -=item C<[get]column> +=head4 C<[get]column> -C - описание столбца который нужно добавить +C - описание столбца который нужно добавить -=back +=head4 C<[get]position> -=item C +Позиция на которую нужно вставить столбец + +=head3 C Удаляет столбец из таблицы -=over +=head4 C -=item C - -=item C<[get]tableName> +=head4 C<[get]tableName> Имя таблицы в которой нужно удалить столбец -=item C<[get]columnName> +=head4 C<[get]columnName> Имя столбца для удаления -=back - -=item C +=head3 C Меняет описание столбца -=over - -=item C +=head4 C C<%args> - хеш, ключами которого являются оставшиеся свойства создаваемого объекта. -=item C<[get]tableName> +=head4 C<[get]tableName> Имя таблицы в которой находится столбец. -=item C<[get]columnName> +=head4 C<[get]columnName> Имя столбца для изменения -=item C<[get]columnType> +=head4 C<[get]columnType> Новый тип столбца. Не задан, если тип не меняется -=item C<[get]defaultValue> +=head4 C<[get]defaultValue> Значение по умолчанию. Не задано, если не меняется -=item C<[get]isNullable> +=head4 C<[get]isNullable> Может ли столбец содержать C. Не задано, если не меняется. -=item C<[get]options> - -Хеш опций, не задан, если опции не меняются +=head4 C<[get]options> -=back +Хеш опций, не задан, если опции не меняются. Данный хеш содержит разничу между +старыми и новыми значениями свойства C столбца. -=item C + +=head3 C Базовый класс для операций по добавлению ограничений -=over +=head4 C -=item C - -=item C<[get]tableName> +=head4 C<[get]tableName> Имя таблицы в которую добавляется ограничение. -=item C<[get]constraint> +=head4 C<[get]constraint> C - описние ограничения, которое нужно добавить. -=back - -=item C +=head3 C Удаляет ограничение на таблицу -=over +=head4 C -=item C - -=item C<[get]tableName> +=head4 C<[get]tableName> Имя таблицы в которой требуется удалить ограничение. -=item C<[get]constraintName> +=head4 C<[get]constraintName> Имя ограничения для удаления. -=back - -=back - =cut diff -r 4abda21186cd -r 3f59fd828d5f Lib/IMPL/SQL/Schema/Traits/MysqlFormatter.pm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/IMPL/SQL/Schema/Traits/MysqlFormatter.pm Fri Jan 25 00:25:02 2013 +0400 @@ -0,0 +1,96 @@ +package IMPL::SQL::Schema::Traits::MysqlFormatter; +use strict; + +sub quote{ + my $self = shift; + + if (wantarray) { + return map { my $str=$_; $str=~ s/'/''/g; "'$str'"; } @_; + } else { + return join '',map { my $str=$_; $str=~ s/'/''/g; "'$str'"; } @_; + } +} + +sub quote_names { + my $self = shift; + + if (wantarray) { + return map { my $str=$_; $str=~ s/`/``/g; "`$str`"; } @_; + } else { + return join '',map { my $str=$_; $str=~ s/`/``/g; "`$str`"; } @_; + } +} + +sub FormatTypeName { + my ($self,$type) = @_; + +} + +sub FormatValue { + my ($self,$value,$type) = @_; +} + +sub FormatColumn { + my ($self, $column) = @_; + + my @parts = ( + $self->quote_names($column->{name}), + $self->FormatTypeName($column->{type}), + $column->isNullable ? 'NULL' : 'NOT NULL' + ); + + push @parts, $self->FormatValue( $column->{defaultValue}, $column->{type} ) + if $column->{defaultValue}; + + push @parts, 'AUTO_INCREMENT' + if $column->{tag} and $column->{tag}->{auto_increment}; + + return join ' ', @parts; +} + +sub FormatCreateTable { + my ($self,$op) = @_; + + my $table = $op->table; + + my @lines; + + push @lines, "CREATE TABLE ("; + + push @lines, map { " " . $self->FormatColumn($_) } @{$table->{columns}} + if $table->{columns}; + + push @lines, ");"; + + return join "\n",@lines; +} + +sub FormatDropTable { + my ($self,$op) = @_; + + return join ' ', + 'DROP TABLE', + $self->quote_names($op->{tableName}), + ';'; +} + +sub FormatRenameTable { + my ($self,$op) = @_; + + return join ' ', + 'ALTER TABLE', + $self->quote_names($op->{tableName}), + 'RENAME TO', + $self->quote_names($op->{tableNewName}), + ';'; +} + +sub FormatAlterTableAddColumn { + my ($this,$op,$schema) = @_; + + my @parts; + + $schema->GetTable($op->{tableName}) +} + +1; \ No newline at end of file diff -r 4abda21186cd -r 3f59fd828d5f _doc/make.pl --- a/_doc/make.pl Mon Jan 21 02:08:17 2013 +0400 +++ b/_doc/make.pl Fri Jan 25 00:25:02 2013 +0400 @@ -4,6 +4,7 @@ use Pod::POM; use Pod::POM::View::HTML; use File::Spec; +use utf8; our $LibDir = '../Lib/IMPL'; our $OutDir = 'html';