comparison Lib/IMPL/SQL/Schema.pm @ 32:56cef8e3cda6

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