annotate Lib/IMPL/SQL/Schema.pm @ 322:cca158327c47

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