49
|
1 use strict;
|
|
2 package Schema::DB;
|
|
3 use Common;
|
|
4 use Schema::DB::Table;
|
|
5
|
|
6 our @ISA = qw(Object);
|
|
7
|
|
8 BEGIN {
|
|
9 DeclareProperty Version => ACCESS_READ;
|
|
10 DeclareProperty Name => ACCESS_READ;
|
|
11 DeclareProperty Tables => ACCESS_READ;
|
|
12 }
|
|
13
|
|
14 sub AddTable {
|
|
15 my ($this,$table) = @_;
|
|
16
|
|
17 if (UNIVERSAL::isa($table,'Schema::DB::Table')) {
|
|
18 $table->Schema == $this or die new Exception('The specified table must belong to the database');
|
|
19 not exists $this->{$Tables}->{$table->Name} or die new Exception('a table with the same name already exists in the database');
|
|
20 } elsif (UNIVERSAL::isa($table,'HASH')) {
|
|
21 not exists $this->{$Tables}->{$table->{'Name'}} or die new Exception('a table with the same name already exists in the database');
|
|
22 $table->{'Schema'} = $this;
|
|
23 $table = new Schema::DB::Table(%{$table});
|
|
24 } else {
|
|
25 die new Exception('Either a table object or a hash with table parameters is required');
|
|
26 }
|
|
27
|
|
28 $this->{$Tables}{$table->Name} = $table;
|
|
29 }
|
|
30
|
|
31 sub RemoveTable {
|
|
32 my ($this,$table) = @_;
|
|
33
|
|
34 my $tn = UNIVERSAL::isa($table,'Schema::DB::Table') ? $table->Name : $table;
|
|
35 $table = delete $this->{$Tables}{$tn} or die new Exception('The table doesn\'t exists',$tn);
|
|
36
|
|
37 # drop foreign keys
|
|
38 map { $_->Table->RemoveConstraint($_) } values %{$table->PrimaryKey->ConnectedFK} if $table->PrimaryKey;
|
|
39
|
|
40 # drop table contents
|
|
41 $table->Dispose();
|
|
42
|
|
43 return 1;
|
|
44 }
|
|
45
|
|
46 sub Dispose {
|
|
47 my ($this) = @_;
|
|
48
|
|
49 $_->Dispose foreach values %{$this->{$Tables}};
|
|
50
|
|
51 delete $this->{$Tables};
|
|
52
|
|
53 $this->SUPER::Dispose;
|
|
54 }
|
|
55
|
|
56
|
|
57 1;
|