Mercurial > pub > Impl
diff Lib/IMPL/SQL/Schema.pm @ 278:4ddb27ff4a0b
core refactoring
author | cin |
---|---|
date | Mon, 04 Feb 2013 02:10:37 +0400 |
parents | 5c82eec23bb6 |
children |
line wrap: on
line diff
--- a/Lib/IMPL/SQL/Schema.pm Fri Feb 01 16:37:59 2013 +0400 +++ b/Lib/IMPL/SQL/Schema.pm Mon Feb 04 02:10:37 2013 +0400 @@ -1,54 +1,54 @@ use strict; package IMPL::SQL::Schema; - -use IMPL::lang qw(is :declare); - -use parent qw( - IMPL::Object - IMPL::Object::Disposable - IMPL::Object::Autofill - IMPL::Object::Clonable -); +use mro; -use IMPL::Class::Property::Direct; - -require IMPL::SQL::Schema::Table; - -__PACKAGE__->PassThroughArgs; - -BEGIN { - public _direct property version => PROP_GET; - public _direct property name => PROP_GET; - private _direct property tables => PROP_GET; -} +use IMPL::lang qw(is); +use IMPL::Const qw(:prop); +use Scalar::Util qw(reftype); +use IMPL::declare { + require => { + Table => 'IMPL::SQL::Schema::Table' + }, + base => [ + 'IMPL::Object' => undef, + 'IMPL::Object::Disposable' => undef, + 'IMPL::Object::Autofill' => '@_', + 'IMPL::Object::Clonable' => undef, + ], + props => [ + version => PROP_RO | PROP_DIRECT, + name => PROP_RO | PROP_DIRECT, + _tables => PROP_RO | PROP_DIRECT + ] +}; sub AddTable { my ($this,$table) = @_; - if (UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table')) { + if (is($table,Table)) { - $table->Schema == $this or die new IMPL::InvalidOperationException('The specified table must belong to the database'); - not exists $this->{$tables}->{$table->name} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database'); + $table->schema == $this or die new IMPL::InvalidOperationException('The specified table must belong to the database'); + not exists $this->{$_tables}->{$table->name} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database'); - } elsif (UNIVERSAL::isa($table,'HASH')) { + } elsif (reftype($table) eq 'HASH') { - not exists $this->{$tables}->{$table->{'name'}} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database'); + not exists $this->{$_tables}->{$table->{'name'}} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database'); $table = { %$table }; $table->{'schema'} = $this; - $table = new IMPL::SQL::Schema::Table(%{$table}); + $table = Table->new(%{$table}); } else { die new IMPL::InvalidArgumentException('Either a table object or a hash with table parameters is required'); } - $this->{$tables}{$table->name} = $table; + $this->{$_tables}{$table->name} = $table; } sub RemoveTable { my ($this,$table) = @_; - my $tn = UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table->name : $table; + my $tn = is($table,Table) ? $table->name : $table; - $table = delete $this->{$tables}{$tn} or die new IMPL::InvalidArgumentException('The table doesn\'t exists',$tn); + $table = delete $this->{$_tables}{$tn} or die new IMPL::InvalidArgumentException('The table doesn\'t exists',$tn); # drop foreign keys map { $_->table->RemoveConstraint($_) } values %{$table->primaryKey->connectedFK} if $table->primaryKey; @@ -62,39 +62,39 @@ sub ResolveTable { my ($this,$table) = @_; - UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table : $this->{$tables}{$table}; + is($table,Table) ? $table : $this->{$_tables}{$table}; } sub GetTable { my ($this,$tableName) = @_; - return $this->{$tables}{$tableName}; + return $this->{$_tables}{$tableName}; } sub GetTables { my ($this) = @_; - return wantarray ? values %{$this->{$tables}} : [values %{$this->{$tables}}]; + return wantarray ? values %{$this->{$_tables}} : [values %{$this->{$_tables}}]; } sub RenameTable { my ($this,$oldName,$newName) = @_; - die new IMPL::InvalidOperationException("A source table doesn't exists", $oldName) unless exists $this->{$tables}{$oldName}; - die new IMPL::InvalidOperationException("A target table already exists", $newName) if exists $this->{$tables}{$newName}; + die new IMPL::InvalidOperationException("A source table doesn't exists", $oldName) unless exists $this->{$_tables}{$oldName}; + die new IMPL::InvalidOperationException("A target table already exists", $newName) if exists $this->{$_tables}{$newName}; - my $table = delete $this->{$tables}{$oldName}; + my $table = delete $this->{$_tables}{$oldName}; $table->_setName($newName); - $this->{$tables}{$newName} = $table; + $this->{$_tables}{$newName} = $table; } sub Dispose { my ($this) = @_; - $_->Dispose foreach values %{$this->{$tables}}; + $_->Dispose foreach values %{$this->{$_tables}}; - delete $this->{$tables}; + delete $this->{$_tables}; - $this->SUPER::Dispose; + $this->next::method(); } 1;