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

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