comparison Lib/IMPL/SQL/Schema.pm @ 278:4ddb27ff4a0b

core refactoring
author cin
date Mon, 04 Feb 2013 02:10:37 +0400
parents 5c82eec23bb6
children
comparison
equal deleted inserted replaced
277:6585464c4664 278:4ddb27ff4a0b
1 use strict; 1 use strict;
2 package IMPL::SQL::Schema; 2 package IMPL::SQL::Schema;
3 use mro;
3 4
4 use IMPL::lang qw(is :declare); 5 use IMPL::lang qw(is);
5 6 use IMPL::Const qw(:prop);
6 use parent qw( 7 use Scalar::Util qw(reftype);
7 IMPL::Object 8 use IMPL::declare {
8 IMPL::Object::Disposable 9 require => {
9 IMPL::Object::Autofill 10 Table => 'IMPL::SQL::Schema::Table'
10 IMPL::Object::Clonable 11 },
11 ); 12 base => [
12 13 'IMPL::Object' => undef,
13 use IMPL::Class::Property::Direct; 14 'IMPL::Object::Disposable' => undef,
14 15 'IMPL::Object::Autofill' => '@_',
15 require IMPL::SQL::Schema::Table; 16 'IMPL::Object::Clonable' => undef,
16 17 ],
17 __PACKAGE__->PassThroughArgs; 18 props => [
18 19 version => PROP_RO | PROP_DIRECT,
19 BEGIN { 20 name => PROP_RO | PROP_DIRECT,
20 public _direct property version => PROP_GET; 21 _tables => PROP_RO | PROP_DIRECT
21 public _direct property name => PROP_GET; 22 ]
22 private _direct property tables => PROP_GET; 23 };
23 }
24 24
25 sub AddTable { 25 sub AddTable {
26 my ($this,$table) = @_; 26 my ($this,$table) = @_;
27 27
28 if (UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table')) { 28 if (is($table,Table)) {
29 29
30 $table->Schema == $this or die new IMPL::InvalidOperationException('The specified table must belong to the database'); 30 $table->schema == $this or die new IMPL::InvalidOperationException('The specified table must belong to the database');
31 not exists $this->{$tables}->{$table->name} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database'); 31 not exists $this->{$_tables}->{$table->name} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database');
32 32
33 } elsif (UNIVERSAL::isa($table,'HASH')) { 33 } elsif (reftype($table) eq 'HASH') {
34 34
35 not exists $this->{$tables}->{$table->{'name'}} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database'); 35 not exists $this->{$_tables}->{$table->{'name'}} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database');
36 $table = { %$table }; 36 $table = { %$table };
37 $table->{'schema'} = $this; 37 $table->{'schema'} = $this;
38 $table = new IMPL::SQL::Schema::Table(%{$table}); 38 $table = Table->new(%{$table});
39 } else { 39 } else {
40 die new IMPL::InvalidArgumentException('Either a table object or a hash with table parameters is required'); 40 die new IMPL::InvalidArgumentException('Either a table object or a hash with table parameters is required');
41 } 41 }
42 42
43 $this->{$tables}{$table->name} = $table; 43 $this->{$_tables}{$table->name} = $table;
44 } 44 }
45 45
46 sub RemoveTable { 46 sub RemoveTable {
47 my ($this,$table) = @_; 47 my ($this,$table) = @_;
48 48
49 my $tn = UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table->name : $table; 49 my $tn = is($table,Table) ? $table->name : $table;
50 50
51 $table = delete $this->{$tables}{$tn} or die new IMPL::InvalidArgumentException('The table doesn\'t exists',$tn); 51 $table = delete $this->{$_tables}{$tn} or die new IMPL::InvalidArgumentException('The table doesn\'t exists',$tn);
52 52
53 # drop foreign keys 53 # drop foreign keys
54 map { $_->table->RemoveConstraint($_) } values %{$table->primaryKey->connectedFK} if $table->primaryKey; 54 map { $_->table->RemoveConstraint($_) } values %{$table->primaryKey->connectedFK} if $table->primaryKey;
55 55
56 # drop table contents 56 # drop table contents
60 } 60 }
61 61
62 sub ResolveTable { 62 sub ResolveTable {
63 my ($this,$table) = @_; 63 my ($this,$table) = @_;
64 64
65 UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table : $this->{$tables}{$table}; 65 is($table,Table) ? $table : $this->{$_tables}{$table};
66 } 66 }
67 67
68 sub GetTable { 68 sub GetTable {
69 my ($this,$tableName) = @_; 69 my ($this,$tableName) = @_;
70 return $this->{$tables}{$tableName}; 70 return $this->{$_tables}{$tableName};
71 } 71 }
72 72
73 sub GetTables { 73 sub GetTables {
74 my ($this) = @_; 74 my ($this) = @_;
75 75
76 return wantarray ? values %{$this->{$tables}} : [values %{$this->{$tables}}]; 76 return wantarray ? values %{$this->{$_tables}} : [values %{$this->{$_tables}}];
77 } 77 }
78 78
79 sub RenameTable { 79 sub RenameTable {
80 my ($this,$oldName,$newName) = @_; 80 my ($this,$oldName,$newName) = @_;
81 81
82 die new IMPL::InvalidOperationException("A source table doesn't exists", $oldName) unless exists $this->{$tables}{$oldName}; 82 die new IMPL::InvalidOperationException("A source table doesn't exists", $oldName) unless exists $this->{$_tables}{$oldName};
83 die new IMPL::InvalidOperationException("A target table already exists", $newName) if exists $this->{$tables}{$newName}; 83 die new IMPL::InvalidOperationException("A target table already exists", $newName) if exists $this->{$_tables}{$newName};
84 84
85 my $table = delete $this->{$tables}{$oldName}; 85 my $table = delete $this->{$_tables}{$oldName};
86 $table->_setName($newName); 86 $table->_setName($newName);
87 $this->{$tables}{$newName} = $table; 87 $this->{$_tables}{$newName} = $table;
88 } 88 }
89 89
90 sub Dispose { 90 sub Dispose {
91 my ($this) = @_; 91 my ($this) = @_;
92 92
93 $_->Dispose foreach values %{$this->{$tables}}; 93 $_->Dispose foreach values %{$this->{$_tables}};
94 94
95 delete $this->{$tables}; 95 delete $this->{$_tables};
96 96
97 $this->SUPER::Dispose; 97 $this->next::method();
98 } 98 }
99 99
100 1; 100 1;
101 101
102 __END__ 102 __END__