annotate Lib/IMPL/SQL/Schema.pm @ 35:f25d021780b3

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