32
|
1 use strict;
|
|
2 package IMPL::SQL::Schema;
|
|
3
|
33
|
4 use base qw(IMPL::Object IMPL::Object::Disposable IMPL::Object::Autofill);
|
32
|
5 use IMPL::Class::Property;
|
|
6 use IMPL::Class::Property::Direct;
|
|
7
|
|
8 require IMPL::SQL::Schema::Table;
|
|
9
|
33
|
10 __PACKAGE__->PassThroughArgs;
|
|
11
|
32
|
12 BEGIN {
|
|
13 public _direct property Version => prop_get;
|
|
14 public _direct property Name => prop_get;
|
|
15 public _direct property Tables => prop_get;
|
|
16 }
|
|
17
|
|
18 sub AddTable {
|
|
19 my ($this,$table) = @_;
|
|
20
|
|
21 if (UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table')) {
|
|
22 $table->Schema == $this or die new IMPL::InvalidOperationException('The specified table must belong to the database');
|
|
23 not exists $this->{$Tables}->{$table->Name} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database');
|
|
24 } elsif (UNIVERSAL::isa($table,'HASH')) {
|
|
25 not exists $this->{$Tables}->{$table->{'Name'}} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database');
|
|
26 $table->{'Schema'} = $this;
|
|
27 $table = new IMPL::SQL::Schema::Table(%{$table});
|
|
28 } else {
|
|
29 die new IMPL::InvalidArgumentException('Either a table object or a hash with table parameters is required');
|
|
30 }
|
|
31
|
|
32 $this->{$Tables}{$table->Name} = $table;
|
|
33 }
|
|
34
|
|
35 sub RemoveTable {
|
|
36 my ($this,$table) = @_;
|
|
37
|
|
38 my $tn = UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table->Name : $table;
|
|
39 $table = delete $this->{$Tables}{$tn} or die new IMPL::InvalidArgumentException('The table doesn\'t exists',$tn);
|
|
40
|
|
41 # drop foreign keys
|
|
42 map { $_->Table->RemoveConstraint($_) } values %{$table->PrimaryKey->ConnectedFK} if $table->PrimaryKey;
|
|
43
|
|
44 # drop table contents
|
|
45 $table->Dispose();
|
|
46
|
|
47 return 1;
|
|
48 }
|
|
49
|
|
50 sub Dispose {
|
|
51 my ($this) = @_;
|
|
52
|
|
53 $_->Dispose foreach values %{$this->{$Tables}};
|
|
54
|
|
55 delete $this->{$Tables};
|
|
56
|
|
57 $this->SUPER::Dispose;
|
|
58 }
|
|
59
|
|
60 1;
|
|
61
|
|
62 __END__
|
|
63 =pod
|
|
64
|
|
65 =head1 SINOPSYS
|
|
66
|
|
67 require IMPL::SQL::Schema;
|
|
68 use IMPL::SQL::Types qw(Varchar Integer);
|
|
69
|
|
70 my $dbSchema = new IMPL::SQL::Schema;
|
|
71
|
|
72 my $tbl = $dbSchema->AddTable({Name => 'Person' });
|
|
73 $tbl->AddColumn({
|
|
74 Name => 'FirstName',
|
|
75 CanBeNull => 1,
|
|
76 Type => Varchar(255)
|
|
77 });
|
|
78 $tbl->AddColumn({
|
|
79 Name => 'Age',
|
|
80 Type => Integer
|
|
81 });
|
|
82
|
|
83 # so on
|
|
84
|
|
85 # and finally don't forget to
|
|
86
|
|
87 $dbSchema->Dispoce();
|
|
88
|
|
89 =head1 DESCRIPTION
|
|
90
|
|
91 Схема реляциоонной базы данных, орентированная на язык SQL, содержит описания таблиц
|
|
92 которые являются частью базы. Позволяет создавать и удалать таблицы.
|
|
93
|
|
94 Имея две схемы можно создавать скрипты для примениения изменений схемы данных C<<IMPL::SQL::Traits>>
|
|
95
|
|
96 =cut
|