annotate Lib/Schema/DB.pm @ 42:4ff27cd051e3

updated ORM schema model
author Sergey
date Thu, 07 Jan 2010 15:34:42 +0300
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 package Schema::DB;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 use Schema::DB::Table;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 DeclareProperty Version => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 DeclareProperty Name => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 DeclareProperty Tables => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 sub AddTable {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 my ($this,$table) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 if (UNIVERSAL::isa($table,'Schema::DB::Table')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 $table->Schema == $this or die new Exception('The specified table must belong to the database');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 not exists $this->{$Tables}->{$table->Name} or die new Exception('a table with the same name already exists in the database');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 } elsif (UNIVERSAL::isa($table,'HASH')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 not exists $this->{$Tables}->{$table->{'Name'}} or die new Exception('a table with the same name already exists in the database');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 $table->{'Schema'} = $this;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 $table = new Schema::DB::Table(%{$table});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 die new Exception('Either a table object or a hash with table parameters is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 $this->{$Tables}{$table->Name} = $table;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 sub RemoveTable {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 my ($this,$table) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 my $tn = UNIVERSAL::isa($table,'Schema::DB::Table') ? $table->Name : $table;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 $table = delete $this->{$Tables}{$tn} or die new Exception('The table doesn\'t exists',$tn);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 # drop foreign keys
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 map { $_->Table->RemoveConstraint($_) } values %{$table->PrimaryKey->ConnectedFK} if $table->PrimaryKey;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 # drop table contents
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 $table->Dispose();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 return 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 sub Dispose {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 $_->Dispose foreach values %{$this->{$Tables}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 delete $this->{$Tables};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 $this->SUPER::Dispose;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 1;