annotate Lib/IMPL/SQL/Schema/Table.pm @ 44:32d2350fccf9

ORM *Minor fixes *Working tarnsform to sql *Fixes to the sql traits
author Sergey
date Mon, 11 Jan 2010 01:42:00 +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::Table;
Sergey
parents:
diff changeset
3
Sergey
parents:
diff changeset
4 use IMPL::SQL::Schema::Column;
Sergey
parents:
diff changeset
5 use IMPL::SQL::Schema::Constraint;
Sergey
parents:
diff changeset
6 use IMPL::SQL::Schema::Constraint::PrimaryKey;
Sergey
parents:
diff changeset
7 use IMPL::SQL::Schema::Constraint::ForeignKey;
Sergey
parents:
diff changeset
8
Sergey
parents:
diff changeset
9 use base qw(IMPL::Object IMPL::Object::Disposable);
Sergey
parents:
diff changeset
10 use IMPL::Class::Property;
Sergey
parents:
diff changeset
11 use IMPL::Class::Property::Direct;
Sergey
parents:
diff changeset
12
Sergey
parents:
diff changeset
13 srand time;
Sergey
parents:
diff changeset
14
Sergey
parents:
diff changeset
15 BEGIN {
Sergey
parents:
diff changeset
16 public _direct property Name => prop_get;
Sergey
parents:
diff changeset
17 public _direct property Schema => prop_get;
Sergey
parents:
diff changeset
18 public _direct property Columns => prop_get;
Sergey
parents:
diff changeset
19 public _direct property Constraints => prop_get;
Sergey
parents:
diff changeset
20 public _direct property ColumnsByName => prop_none;
Sergey
parents:
diff changeset
21 public _direct property PrimaryKey => prop_get;
Sergey
parents:
diff changeset
22 public _direct property Tag => prop_all;
Sergey
parents:
diff changeset
23 }
Sergey
parents:
diff changeset
24
Sergey
parents:
diff changeset
25 sub CTOR {
Sergey
parents:
diff changeset
26 my ($this,%args) = @_;
Sergey
parents:
diff changeset
27
Sergey
parents:
diff changeset
28 $this->{$Name} = $args{'Name'} or die new IMPL::InvalidArgumentException('a table name is required');
Sergey
parents:
diff changeset
29 $this->{$Schema} = $args{'Schema'} or die new IMPL::InvalidArgumentException('a parent schema is required');
Sergey
parents:
diff changeset
30 }
Sergey
parents:
diff changeset
31
Sergey
parents:
diff changeset
32 sub InsertColumn {
Sergey
parents:
diff changeset
33 my ($this,$column,$index) = @_;
Sergey
parents:
diff changeset
34
Sergey
parents:
diff changeset
35 $index = ($this->{$Columns} ? scalar(@{$this->{$Columns}}) : 0) if not defined $index;
Sergey
parents:
diff changeset
36
Sergey
parents:
diff changeset
37 die new IMPL::InvalidArgumentException("The index is out of range") if ($index < 0 || $index > ($this->{$Columns} ? scalar(@{$this->{$Columns}}) : 0));
Sergey
parents:
diff changeset
38
Sergey
parents:
diff changeset
39 if (UNIVERSAL::isa($column,'IMPL::SQL::Schema::Column')) {
Sergey
parents:
diff changeset
40
Sergey
parents:
diff changeset
41 } elsif (UNIVERSAL::isa($column,'HASH')) {
Sergey
parents:
diff changeset
42 $column = new IMPL::SQL::Schema::Column(%{$column});
Sergey
parents:
diff changeset
43 } else {
Sergey
parents:
diff changeset
44 die new IMPL::InvalidArgumentException("The invalid column parameter");
Sergey
parents:
diff changeset
45 }
Sergey
parents:
diff changeset
46
Sergey
parents:
diff changeset
47 if (exists $this->{$ColumnsByName}->{$column->Name}) {
Sergey
parents:
diff changeset
48 die new IMPL::InvalidOperationException("The column already exists",$column->name);
Sergey
parents:
diff changeset
49 } else {
Sergey
parents:
diff changeset
50 $this->{$ColumnsByName}->{$column->Name} = $column;
Sergey
parents:
diff changeset
51 splice @{$this->{$Columns}},$index,0,$column;
Sergey
parents:
diff changeset
52 }
Sergey
parents:
diff changeset
53
Sergey
parents:
diff changeset
54 return $column;
Sergey
parents:
diff changeset
55 }
Sergey
parents:
diff changeset
56
Sergey
parents:
diff changeset
57 sub RemoveColumn {
Sergey
parents:
diff changeset
58 my ($this,$NameOrColumn,$Force) = @_;
Sergey
parents:
diff changeset
59
Sergey
parents:
diff changeset
60 my $ColName;
Sergey
parents:
diff changeset
61 if (UNIVERSAL::isa($NameOrColumn,'IMPL::SQL::Schema::Column')) {
Sergey
parents:
diff changeset
62 $ColName = $NameOrColumn->Name;
Sergey
parents:
diff changeset
63 } elsif (not ref $NameOrColumn) {
Sergey
parents:
diff changeset
64 $ColName = $NameOrColumn;
Sergey
parents:
diff changeset
65 }
Sergey
parents:
diff changeset
66
Sergey
parents:
diff changeset
67 if (exists $this->{$ColumnsByName}->{$ColName}) {
Sergey
parents:
diff changeset
68 my $index = 0;
Sergey
parents:
diff changeset
69 foreach my $column(@{$this->{$Columns}}) {
Sergey
parents:
diff changeset
70 last if $column->Name eq $ColName;
Sergey
parents:
diff changeset
71 $index++;
Sergey
parents:
diff changeset
72 }
Sergey
parents:
diff changeset
73
Sergey
parents:
diff changeset
74 my $column = $this->{$Columns}[$index];
Sergey
parents:
diff changeset
75 if (my @constraints = $this->GetColumnConstraints($column)){
Sergey
parents:
diff changeset
76 $Force or die new IMPL::InvalidOperationException('Can\'t remove column which is used in the constraints',@constraints);
Sergey
parents:
diff changeset
77 $this->RemoveConstraint($_) foreach @constraints;
Sergey
parents:
diff changeset
78 }
Sergey
parents:
diff changeset
79
Sergey
parents:
diff changeset
80 my $removed = splice @{$this->{$Columns}},$index,1;
Sergey
parents:
diff changeset
81 delete $this->{$ColumnsByName}->{$ColName};
Sergey
parents:
diff changeset
82 return $removed;
Sergey
parents:
diff changeset
83 } else {
Sergey
parents:
diff changeset
84 die new IMPL::InvalidOperationException("The column not found",$NameOrColumn->Name);
Sergey
parents:
diff changeset
85 }
Sergey
parents:
diff changeset
86 }
Sergey
parents:
diff changeset
87
Sergey
parents:
diff changeset
88 sub Column {
Sergey
parents:
diff changeset
89 my ($this,$name) = @_;
Sergey
parents:
diff changeset
90
Sergey
parents:
diff changeset
91 return $this->{$ColumnsByName}->{$name};
Sergey
parents:
diff changeset
92 }
Sergey
parents:
diff changeset
93
Sergey
parents:
diff changeset
94 sub ColumnAt {
Sergey
parents:
diff changeset
95 my ($this,$index) = @_;
Sergey
parents:
diff changeset
96
Sergey
parents:
diff changeset
97 die new IMPL::InvalidArgumentException("The index is out of range") if $index < 0 || $index >= ($this->{$Columns} ? scalar(@{$this->{$Columns}}) : 0);
Sergey
parents:
diff changeset
98
Sergey
parents:
diff changeset
99 return $this->{$Columns}[$index];
Sergey
parents:
diff changeset
100 }
Sergey
parents:
diff changeset
101
Sergey
parents:
diff changeset
102 sub AddConstraint {
Sergey
parents:
diff changeset
103 my ($this,$Constraint) = @_;
Sergey
parents:
diff changeset
104
Sergey
parents:
diff changeset
105 die new IMPL::InvalidArgumentException('The invalid parameter') if not UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint');
Sergey
parents:
diff changeset
106
Sergey
parents:
diff changeset
107 $Constraint->Table == $this or die new IMPL::InvalidOperationException('The constaint must belong to the target table');
Sergey
parents:
diff changeset
108
Sergey
parents:
diff changeset
109 if (exists $this->{$Constraints}->{$Constraint->Name}) {
Sergey
parents:
diff changeset
110 die new IMPL::InvalidOperationException('The table already has the specified constraint',$Constraint->Name);
Sergey
parents:
diff changeset
111 } else {
Sergey
parents:
diff changeset
112 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
Sergey
parents:
diff changeset
113 not $this->{$PrimaryKey} or die new IMPL::InvalidOperationException('The table already has a primary key');
Sergey
parents:
diff changeset
114 $this->{$PrimaryKey} = $Constraint;
Sergey
parents:
diff changeset
115 }
Sergey
parents:
diff changeset
116
Sergey
parents:
diff changeset
117 $this->{$Constraints}->{$Constraint->Name} = $Constraint;
Sergey
parents:
diff changeset
118 }
Sergey
parents:
diff changeset
119 }
Sergey
parents:
diff changeset
120
Sergey
parents:
diff changeset
121 sub RemoveConstraint {
Sergey
parents:
diff changeset
122 my ($this,$Constraint,$Force) = @_;
Sergey
parents:
diff changeset
123
Sergey
parents:
diff changeset
124 my $cn = UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint') ? $Constraint->Name : $Constraint;
Sergey
parents:
diff changeset
125 $Constraint = $this->{$Constraints}->{$cn} or die new IMPL::InvalidOperationException('The specified constraint doesn\'t exists',$cn);
Sergey
parents:
diff changeset
126
Sergey
parents:
diff changeset
127 if (UNIVERSAL::isa($Constraint,'IMPL::SQL::Schema::Constraint::PrimaryKey')) {
Sergey
parents:
diff changeset
128 not scalar keys %{$this->{$PrimaryKey}->ConnectedFK} or die new IMPL::InvalidOperationException('Can\'t remove Primary Key unless some foreign keys referenses it');
Sergey
parents:
diff changeset
129
Sergey
parents:
diff changeset
130 delete $this->{$PrimaryKey};
Sergey
parents:
diff changeset
131 }
Sergey
parents:
diff changeset
132 $Constraint->Dispose;
Sergey
parents:
diff changeset
133 delete $this->{$Constraints}->{$cn};
Sergey
parents:
diff changeset
134 return $cn;
Sergey
parents:
diff changeset
135 }
Sergey
parents:
diff changeset
136
Sergey
parents:
diff changeset
137 sub GetColumnConstraints {
Sergey
parents:
diff changeset
138 my ($this,@Columns) = @_;
Sergey
parents:
diff changeset
139
Sergey
parents:
diff changeset
140 my @cn = map { UNIVERSAL::isa($_ ,'IMPL::SQL::Schema::Column') ? $_ ->Name : $_ } @Columns;
Sergey
parents:
diff changeset
141 exists $this->{$ColumnsByName}->{$_} or die new IMPL::InvalidOperationException('The specified column isn\'t found',$_) foreach @cn;
Sergey
parents:
diff changeset
142
Sergey
parents:
diff changeset
143 return grep {$_->HasColumn(@cn)} values %{$this->{$Constraints}};
Sergey
parents:
diff changeset
144 }
Sergey
parents:
diff changeset
145
Sergey
parents:
diff changeset
146 sub SetPrimaryKey {
Sergey
parents:
diff changeset
147 my ($this,@ColumnList) = @_;
Sergey
parents:
diff changeset
148
Sergey
parents:
diff changeset
149 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::PrimaryKey(Name => $this->{$Name}.'_PK', Table => $this,Columns => \@ColumnList));
Sergey
parents:
diff changeset
150 }
Sergey
parents:
diff changeset
151
Sergey
parents:
diff changeset
152 sub LinkTo {
Sergey
parents:
diff changeset
153 my ($this,$table,@ColumnList) = @_;
Sergey
parents:
diff changeset
154 $table->PrimaryKey or die new IMPL::InvalidOperationException('The referenced table must have a primary key');
Sergey
parents:
diff changeset
155 my $constraintName = $this->{$Name}.'_'.$table->Name.'_FK_'.join('_',map {ref $_ ? $_->Name : $_} @ColumnList);
33
0004faa276dc small fixes, some new tests
Sergey
parents: 32
diff changeset
156 $this->AddConstraint(new IMPL::SQL::Schema::Constraint::ForeignKey(Name => $constraintName, Table => $this,Columns => \@ColumnList, ReferencedTable => $table, ReferencedColumns => $table->PrimaryKey->Columns));
32
Sergey
parents:
diff changeset
157 }
Sergey
parents:
diff changeset
158
Sergey
parents:
diff changeset
159 sub Dispose {
Sergey
parents:
diff changeset
160 my ($this) = @_;
Sergey
parents:
diff changeset
161
Sergey
parents:
diff changeset
162 $_->Dispose() foreach values %{$this->{$Constraints}};
Sergey
parents:
diff changeset
163
Sergey
parents:
diff changeset
164 undef %{$this};
Sergey
parents:
diff changeset
165 $this->SUPER::Dispose();
Sergey
parents:
diff changeset
166 }
Sergey
parents:
diff changeset
167
Sergey
parents:
diff changeset
168 1;