| 1 | package Crop::Object::Sequence; | ||
| 2 | use base qw / Crop::Object /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Sequence | ||
| 6 | Primary key includes the 'n' attribute. | ||
| 7 | |||
| 8 | IMPORTANT: the %Attributes of derived class MUST setup the 'key' | ||
| 9 | > id_obj => {key => 'extern'} | ||
| 10 | or SELECT will give incomplete results! | ||
| 11 | |||
| 12 | 'n' is a counter inside other part of a primary key. It starts from '1' and must be continious, whithout missing elements. | ||
| 13 | The 'n' defines sorting order. | ||
| 14 | |||
| 15 | As the other keys, 'n' has the 'key' element in their declaration | ||
| 16 | > our %Attributes = ( | ||
| 17 | > n => {key => 'counter'}, | ||
| 18 | >); | ||
| 19 | =cut | ||
| 20 | |||
| 21 | use v5.14; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use Crop::Object::Constants; | ||
| 25 | use Crop::Debug; | ||
| 26 | |||
| 27 | =begin nd | ||
| 28 | Variable: our %Attributes | ||
| 29 | Class attributes: | ||
| 30 | |||
| 31 | n - counter inside primary key | ||
| 32 | =cut | ||
| 33 | our %Attributes = ( | ||
| 34 | n => {mode => 'read', type => 'key', key => 'counter'}, | ||
| 35 | ); | ||
| 36 | |||
| 37 | =begin nd | ||
| 38 | Method: _Prepare_key ( ) | ||
| 39 | Generate 'n'. | ||
| 40 | |||
| 41 | An error will arised though only one 'extern' key is missed. | ||
| 42 | |||
| 43 | Returns: | ||
| 44 | n - actual vaule; always is true | ||
| 45 | undef - one of the 'extern' key is missed | ||
| 46 | =cut | ||
| 47 | sub _Prepare_key { | ||
| 48 | my $self = shift; | ||
| 49 | |||
| 50 | my $table = $self->Table or return warn "OBJECT|ERR: Can't generate primary key because of missing table."; | ||
| 51 | |||
| 52 | my %key; | ||
| 53 | my $counter; | ||
| 54 | my $attrs = $self->Attributes(KEY); | ||
| 55 | for (@$attrs) { | ||
| 56 | my $val = $self->{$_->name}; | ||
| 57 | |||
| 58 | if ($_->key->type eq 'counter') { | ||
| 59 | $counter = $val; | ||
| 60 | } else { | ||
| 61 | return unless $val; | ||
| 62 | |||
| 63 | $key{$_->name} = $val; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | return $counter if $counter; | ||
| 67 | |||
| 68 | my $last = $self->All(%key, SORT => 'n desc')->First; | ||
| 69 | $self->{n} = $last ? $last->n + 1 : 1; # returns n >= 1; true | ||
| 70 | } | ||
| 71 | |||
| 72 | =begin nd | ||
| 73 | Method: Table ( ) | ||
| 74 | Table storage in the Warehouse. | ||
| 75 | |||
| 76 | This method must be redefined by subclass. | ||
| 77 | |||
| 78 | Returns: | ||
| 79 | undef - and the error will arised | ||
| 80 | =cut | ||
| 81 | sub Table { return warn "OBJECT|ERR: The Table method should be redefined by subclass"; } | ||
| 82 | |||
| 83 | 1; |