annotate Lib/IMPL/SQL/Schema.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents d1676be8afcc
children 5c82eec23bb6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
1 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
2 package IMPL::SQL::Schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
3
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
4 use IMPL::_core::version;
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
5
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
6 use IMPL::lang qw(is :declare :constants);
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
7
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
8 use parent qw(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
9 IMPL::Object
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
10 IMPL::Object::Disposable
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
11 IMPL::Object::Autofill
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
12 IMPL::Object::Clonable
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
13 );
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
14
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
15 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
16
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
17 require IMPL::SQL::Schema::Table;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
18
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
19 __PACKAGE__->PassThroughArgs;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
20
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
21 BEGIN {
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
22 public _direct property version => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
23 public _direct property name => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
24 private _direct property tables => PROP_GET;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
25 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
26
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
27 sub AddTable {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28 my ($this,$table) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
30 if (UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
31
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
32 $table->Schema == $this or die new IMPL::InvalidOperationException('The specified table must belong to the database');
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
33 not exists $this->{$tables}->{$table->name} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database');
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
34
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
35 } elsif (UNIVERSAL::isa($table,'HASH')) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
36
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
37 not exists $this->{$tables}->{$table->{'name'}} or die new IMPL::InvalidOperationException('a table with the same name already exists in the database');
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
38 $table = { %$table };
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
39 $table->{'schema'} = $this;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
40 $table = new IMPL::SQL::Schema::Table(%{$table});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
41 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
42 die new IMPL::InvalidArgumentException('Either a table object or a hash with table parameters is required');
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
43 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
44
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
45 $this->{$tables}{$table->name} = $table;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
46 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
47
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48 sub RemoveTable {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49 my ($this,$table) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
50
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
51 my $tn = UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table->name : $table;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
52
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
53 $table = delete $this->{$tables}{$tn} or die new IMPL::InvalidArgumentException('The table doesn\'t exists',$tn);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
54
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
55 # drop foreign keys
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
56 map { $_->table->RemoveConstraint($_) } values %{$table->primaryKey->connectedFK} if $table->primaryKey;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
58 # drop table contents
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
59 $table->Dispose();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
60
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
61 return 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
62 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
63
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
64 sub ResolveTable {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
65 my ($this,$table) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
66
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
67 UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table : $this->{$tables}{$table};
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
68 }
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
69
164
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
70 sub GetTable {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
71 my ($this,$tableName) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
72 return $this->{$tables}{$tableName};
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
73 }
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
74
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
75 sub GetTables {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
76 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
77
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
78 return wantarray ? values %{$this->{$tables}} : [values %{$this->{$tables}}];
164
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
79 }
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
80
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
81 sub RenameTable {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
82 my ($this,$oldName,$newName) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
83
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
84 die new IMPL::InvalidOperationException("A source table doesn't exists", $oldName) unless exists $this->{$tables}{$oldName};
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
85 die new IMPL::InvalidOperationException("A target table already exists", $newName) if exists $this->{$tables}{$newName};
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
86
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
87 my $table = delete $this->{$tables}{$oldName};
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
88 $table->_setName($newName);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
89 $this->{$tables}{$newName} = $table;
164
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
90 }
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
91
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
92 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
93 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
94
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
95 $_->Dispose foreach values %{$this->{$tables}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
96
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
97 delete $this->{$tables};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
98
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
99 $this->SUPER::Dispose;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
100 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
101
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
102 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
103
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
104 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
105 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
106
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
107 =head1 SYNOPSIS
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
108
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
109 =begin code
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
110
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
111 require IMPL::SQL::Schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
112 use IMPL::SQL::Types qw(Varchar Integer);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
113
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
114 my $dbSchema = new IMPL::SQL::Schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
115
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
116 my $tbl = $dbSchema->AddTable({name => 'Person' });
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
117 $tbl->AddColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
118 name => 'FirstName',
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
119 canBeNull => 1,
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
120 type => Varchar(255)
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
121 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
122 $tbl->AddColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
123 name => 'Age',
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
124 type => Integer
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
125 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
126
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
127 # so on
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
128
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
129 # and finally don't forget to
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
130
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
131 $dbSchema->Dispose();
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
132
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
133 =end code
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
134
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
135 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
136
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
137 Схема реляциоонной базы данных, орентированная на язык SQL, содержит описания таблиц
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
138 которые являются частью базы. Позволяет создавать и удалать таблицы.
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
139
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
140 =head1 MEMBERS
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
141
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
142 =over
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
143
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
144 =item C<CTOR(%props)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
145
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
146 Конструктор заполняет объект свойствами из C<props>.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
147
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
148 =item C<[get]name>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
149
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
150 Имя схемы.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
151
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
152 =item C<[get]version>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
153
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
154 Версия схемы.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
155
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
156 =item C<AddTable($table)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
157
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
158 Доавляет таблицу в схему. C<$table> может быть либо таблице, либо хешем с набором
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
159 свойств для создания новой таблицы. Если таблица с таким именем уже существует в сехеме,
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
160 то вызывается исключение.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
161
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
162 =item C<GetTable($name)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
163
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
164 Возвращает таблицу с именем C<$name> или C<undef>.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
166 =item C<GetTables()>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
167
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
168 Возвращает список таблиц. В скалярном контексте - ссылку на массив с таблицами.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
169
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
170 =item C<ResolveTable($table)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
171
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
172 Если параметр C<$table> - таблица, то возвращается C<$table>, если C<$table> строка, то
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
173 ищется таблица с таким именем, если таблица не найдена, возвращается C<undef>.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
174
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
175 =item C<RenameTable($oldName,$newName)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
176
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
177 Происходит переименование таблицы. Если C<$oldName> не существует, либо если C<$newName>
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
178 существует, вызывается исключение.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
179
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
180 =item C<RemoveTable($table)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
181
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
182 Удаляется таблица C<$table> с удалением всех связей и ограничений. Если такой таблицы нет,
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 167
diff changeset
183 то вызывается исключение. C<$table> может быть либо именем таблицы, либо объектом.
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
184
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
185 =back
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
186
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
187 =cut