comparison Lib/IMPL/SQL/Schema/Traits.pm @ 168:6148f89bb7bf

IMPL::SQL::Schema::Traits::Diff alfa version IMPL::lang added hash traits
author sourcer
date Mon, 16 May 2011 04:30:38 +0400
parents 1f7a6d762394
children fd92830036c3
comparison
equal deleted inserted replaced
167:1f7a6d762394 168:6148f89bb7bf
4 use IMPL::Exception(); 4 use IMPL::Exception();
5 5
6 use parent qw(IMPL::Object); 6 use parent qw(IMPL::Object);
7 use IMPL::Code::Loader(); 7 use IMPL::Code::Loader();
8 8
9 BEGIN { 9 # required for use with typeof operator
10 IMPL::Code::Loader->Provide(__PACKAGE__); 10 use IMPL::SQL::Schema::Constraint::PrimaryKey();
11 } 11 use IMPL::SQL::Schema::Constraint::Index();
12 use IMPL::SQL::Schema::Constraint::Unique();
13 use IMPL::SQL::Schema::Constraint::ForeignKey();
12 14
13 ################################################### 15 ###################################################
14 16
15 package IMPL::SQL::Schema::Traits::Table; 17 package IMPL::SQL::Schema::Traits::Table;
16 use base qw(IMPL::Object::Fields); 18 use base qw(IMPL::Object::Fields);
66 68
67 sub CTOR { 69 sub CTOR {
68 my ($this, $name, $columns) = @_; 70 my ($this, $name, $columns) = @_;
69 71
70 $this->{name} = $name; 72 $this->{name} = $name;
71 $$this->{columns} = $columns; # list of columnNames 73 $this->{columns} = $columns; # list of columnNames
74 }
75
76 sub constraintClass {
77 die new IMPL::NotImplementedException();
72 } 78 }
73 79
74 ################################################## 80 ##################################################
75 81
76 package IMPL::SQL::Schema::Traits::PrimaryKey; 82 package IMPL::SQL::Schema::Traits::PrimaryKey;
77 83
78 use base qw(IMPL::SQL::Schema::Traits::Constraint); 84 use base qw(IMPL::SQL::Schema::Traits::Constraint);
79 85
80 __PACKAGE__->PassThroughArgs; 86 __PACKAGE__->PassThroughArgs;
81 87
88 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::PrimaryKey };
89
82 ################################################## 90 ##################################################
83 91
84 package IMPL::SQL::Schema::Traits::Index; 92 package IMPL::SQL::Schema::Traits::Index;
85 93
86 use base qw(IMPL::SQL::Schema::Traits::Constraint); 94 use base qw(IMPL::SQL::Schema::Traits::Constraint);
87 95
88 __PACKAGE__->PassThroughArgs; 96 __PACKAGE__->PassThroughArgs;
89 97
98 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::Index };
99
90 ################################################## 100 ##################################################
91 101
92 package IMPL::SQL::Schema::Traits::Unique; 102 package IMPL::SQL::Schema::Traits::Unique;
93 103
94 use base qw(IMPL::SQL::Schema::Traits::Constraint); 104 use base qw(IMPL::SQL::Schema::Traits::Constraint);
95 105
96 __PACKAGE__->PassThroughArgs; 106 __PACKAGE__->PassThroughArgs;
107
108 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::Unique };
97 109
98 ################################################## 110 ##################################################
99 111
100 package IMPL::SQL::Schema::Traits::ForeignKey; 112 package IMPL::SQL::Schema::Traits::ForeignKey;
101 113
103 use fields qw( 115 use fields qw(
104 foreignTable 116 foreignTable
105 foreignColumns 117 foreignColumns
106 ); 118 );
107 119
120 use constant { constraintClass => typeof IMPL::SQL::Schema::Constraint::ForeignKey };
121
108 our %CTOR = ( 122 our %CTOR = (
109 'IMPL::SQL::Schema::Traits::Constraint' => sub { @_[0..1] } 123 'IMPL::SQL::Schema::Traits::Constraint' => sub { @_[0..1] }
110 ); 124 );
111 125
112 sub CTOR { 126 sub CTOR {
272 use IMPL::Class::Property; 286 use IMPL::Class::Property;
273 287
274 BEGIN { 288 BEGIN {
275 public property tableName => prop_get | owner_set; 289 public property tableName => prop_get | owner_set;
276 public property columnName => prop_get | owner_set; 290 public property columnName => prop_get | owner_set;
277 public property columnType => prop_get | owner_set; 291 public property columnType => prop_all;
278 public property defaultValue => prop_get | owner_set; 292 public property defaultValue => prop_all;
279 public property isNullable => prop_get | owner_set; 293 public property isNullable => prop_all;
280 public property options => prop_get | owner_set; 294 public property options => prop_all; # hash diff format, (keys have a prefix '+' - add or update value, '-' remove value)
281 } 295 }
282 296
283 sub CTOR { 297 sub CTOR {
284 my ($this, $table,$column,%args) = @_; 298 my ($this, $table,$column,%args) = @_;
285 299
295 309
296 local $@; 310 local $@;
297 311
298 return eval { 312 return eval {
299 my $column = $schema->GetTable($this->tableName)->GetColumn($this->columnName); 313 my $column = $schema->GetTable($this->tableName)->GetColumn($this->columnName);
300 $column->SetType($this->columnType) if $this->columnType; 314 $column->SetType($this->columnType) if defined $this->columnType;
301 $column->SetNullable($this->isNullable) if $this->isNullable; 315 $column->SetNullable($this->isNullable) if defined $this->isNullable;
302 $column->SetDefaultValue($this->defaultValue) if $this->defaultValue; 316 $column->SetDefaultValue($this->defaultValue) if defined $this->defaultValue;
303 $column->SetOptions($this->options) if $this->options; 317 $column->SetOptions($this->options) if defined $this->options;
304 318
305 return 1; 319 return 1;
306 } || 0; 320 } || 0;
307 } 321 }
308 322
333 my ($this,$schema) = @_; 347 my ($this,$schema) = @_;
334 348
335 local $@; 349 local $@;
336 350
337 return eval { 351 return eval {
338 $schema->GetTable($this->tableName)->AddConstraint($this->constraint); 352 $schema->GetTable($this->tableName)->AddConstraint($this->constraint->constraintClass, $this->constraint);
339 return 1; 353 return 1;
340 } || 0; 354 } || 0;
341 355
342 } 356 }
343 357