annotate Lib/IMPL/SQL/Schema.pm @ 166:4267a2ac3d46

Added Class::Template, Rewritten SQL::Schema 'use parent' directive instead of 'use base'
author wizard
date Sat, 23 Apr 2011 23:12:06 +0400
parents 76515373dac0
children 1f7a6d762394
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;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
5 use IMPL::lang;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
6 use parent qw(
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
7 IMPL::Object
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
8 IMPL::Object::Disposable
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
9 IMPL::Object::Autofill
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
10 IMPL::Object::Clonable
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;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
14 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
15
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
16 require IMPL::SQL::Schema::Table;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
17
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
18 __PACKAGE__->PassThroughArgs;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
19
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
20 BEGIN {
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
21 public _direct property version => prop_get;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
22 public _direct property name => prop_get;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
23 private _direct property tables => prop_get;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
26 sub AddTable {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
27 my ($this,$table) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
28
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29 if (UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
30
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
31 $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
32 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
33
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
34 } elsif (UNIVERSAL::isa($table,'HASH')) {
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
35
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
36 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
37 $table = { %$table };
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
38 $table->{'schema'} = $this;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
39 $table = new IMPL::SQL::Schema::Table(%{$table});
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
40 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
41 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
42 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
43
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
44 $this->{$tables}{$table->name} = $table;
49
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
47 sub RemoveTable {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
48 my ($this,$table) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
50 my $tn = UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table->name : $table;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
51
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
52 $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
53
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
54 # drop foreign keys
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
55 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
56
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57 # drop table contents
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
58 $table->Dispose();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
59
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
60 return 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
61 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
62
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
63 sub ResolveTable {
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
64 my ($this,$table) = @_;
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
65
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
66 UNIVERSAL::isa($table,'IMPL::SQL::Schema::Table') ? $table : $this->{$tables}{$table};
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
67 }
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
68
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
69 sub Table {
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
70 goto &GetTable;
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
71 }
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
72
164
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
73 sub GetTable {
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
74 my ($this,$tableName) = @_;
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
75 return $this->{$tables}{$tableName};
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
76 }
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
77
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
78 sub GetTables {
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
79 my ($this) = @_;
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
80
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
81 return wantarray ? values %{$this->{$tables}} : [values %{$this->{$tables}}];
164
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
82 }
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
83
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
84 sub RenameTable {
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
85 my ($this,$oldName,$newName) = @_;
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
86
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
87 die new IMPL::InvalidOperationException("A source table doesn't exists", $oldName) unless exists $this->{$tables}{$oldName};
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
88 die new IMPL::InvalidOperationException("A target table already exists", $newName) if exists $this->{$tables}{$newName};
164
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
89
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
90 my $table = delete $this->{$tables}{$oldName};
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
91 $table->_setName($newName);
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
92 $this->{$tables}{$newName} = $table;
164
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
93 }
eb3e9861a761 SQL traits in progress
wizard
parents: 163
diff changeset
94
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
95 sub Dispose {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
96 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
97
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
98 $_->Dispose foreach values %{$this->{$tables}};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
99
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
100 delete $this->{$tables};
49
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 $this->SUPER::Dispose;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
105 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
106
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
107 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
108 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
109
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
110 =head1 SYNOPSIS
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
111
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
112 =begin code
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
113
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
114 require IMPL::SQL::Schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
115 use IMPL::SQL::Types qw(Varchar Integer);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
116
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
117 my $dbSchema = new IMPL::SQL::Schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
118
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
119 my $tbl = $dbSchema->AddTable({name => 'Person' });
49
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 => 'FirstName',
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
122 canBeNull => 1,
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
123 type => Varchar(255)
49
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 $tbl->AddColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
126 name => 'Age',
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
127 type => Integer
49
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
130 # so on
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
131
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
132 # and finally don't forget to
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
133
163
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
134 $dbSchema->Dispose();
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
135
6ce1f052b90a temp commit
wizard
parents: 49
diff changeset
136 =end code
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
137
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
138 =head1 DESCRIPTION
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
139
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
140 Схема реляциоонной базы данных, орентированная на язык SQL, содержит описания таблиц
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
141 которые являются частью базы. Позволяет создавать и удалать таблицы.
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
142
165
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
143 =head1 MEMBERS
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
144
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
145 =over
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
146
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
147 =item C<CTOR(%props)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
148
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
149 Конструктор заполняет объект свойствами из C<props>.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
150
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
151 =item C<[get]name>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
152
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
153 Имя схемы.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
154
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
155 =item C<[get]version>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
156
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
157 Версия схемы.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
158
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
159 =item C<AddTable($table)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
160
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
161 Доавляет таблицу в схему. C<$table> может быть либо таблице, либо хешем с набором
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
162 свойств для создания новой таблицы. Если таблица с таким именем уже существует в сехеме,
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
163 то вызывается исключение.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
164
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
165 =item C<GetTable($name)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
166
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
167 Возвращает таблицу с именем C<$name> или C<undef>.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
168
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
169 =item C<GetTables()>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
170
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
171 Возвращает список таблиц. В скалярном контексте - ссылку на массив с таблицами.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
172
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
173 =item C<ResolveTable($table)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
174
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
175 Если параметр C<$table> - таблица, то возвращается C<$table>, если C<$table> строка, то
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
176 ищется таблица с таким именем, если таблица не найдена, возвращается C<undef>.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
177
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
178 =item C<RenameTable($oldName,$newName)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
179
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
180 Происходит переименование таблицы. Если C<$oldName> не существует, либо если C<$newName>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
181 существует, вызывается исключение.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
182
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
183 =item C<RemoveTable($table)>
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
184
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
185 Удаляется таблица C<$table> с удалением всех связей и ограничений. Если такой таблицы нет,
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
186 то вызывается исключение. C<$table> может быть либо именем таблицы, либо объектом.
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
187
76515373dac0 Added Class::Template,
wizard
parents: 164
diff changeset
188 =back
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
189
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
190 =cut