annotate Lib/IMPL/SQL/Schema.pm @ 233:3cebcf6fdb9b

refactoring, cleaning code
author sergey
date Thu, 11 Oct 2012 04:53:08 +0400
parents 5c82eec23bb6
children 4ddb27ff4a0b
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
232
5c82eec23bb6 Fixed degradations due refactoring
sergey
parents: 194
diff changeset
4 use IMPL::lang qw(is :declare);
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
5
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
6 use parent qw(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
7 IMPL::Object
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
8 IMPL::Object::Disposable
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
9 IMPL::Object::Autofill
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
10 IMPL::Object::Clonable
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
11 );
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
12
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
13 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
14
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
15 require IMPL::SQL::Schema::Table;
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 __PACKAGE__->PassThroughArgs;
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 BEGIN {
167
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
20 public _direct property version => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
21 public _direct property name => PROP_GET;
1f7a6d762394 SQL schema in progress
sourcer
parents: 165
diff changeset
22 private _direct property tables => PROP_GET;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
23 }
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28 if (UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
29
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
30 $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
31 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
32
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
33 } elsif (UNIVERSAL::isa($table,'HASH')) {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
34
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
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');
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;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
38 $table = new IMPL::SQL::Schema::Table(%{$table});
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
165
76515373dac0 Added Class::Template,
wizard
parents: 164
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
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
49 my $tn = UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table->name : $table;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
50
76515373dac0 Added Class::Template,
wizard
parents: 164
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
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
65 UNIVERSAL::isa($table,'IMPL::SQL::Schema::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) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
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
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
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
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
82 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
83 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
84
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
85 my $table = delete $this->{$tables}{$oldName};
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
diff changeset
86 $table->_setName($newName);
4d0e1962161c Replaced tabs with spaces
cin
parents: 180
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
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
93 $_->Dispose foreach values %{$this->{$tables}};
49
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 delete $this->{$tables};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
96
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
97 $this->SUPER::Dispose;
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