| 1 | package Crop::Object::Extern::Refbook; | ||
| 2 | use base qw / Crop::Object::Extern /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Extern::Refbook | ||
| 6 | Link one-to-many in 'many' side. | ||
| 7 | |||
| 8 | For example: User is borned in City. Many Users could have one City. | ||
| 9 | Bob & Alice is born in London, so a selected user Bob is from London. | ||
| 10 | |||
| 11 | Is assumed the record in Refbook is always presences. | ||
| 12 | |||
| 13 | (start code) | ||
| 14 | our %Attributes = ( | ||
| 15 | #user name | ||
| 16 | name => undef, | ||
| 17 | id_city => {key => 'extern'}, | ||
| 18 | city => {type => 'cache'}, | ||
| 19 | EXT => { | ||
| 20 | city => { | ||
| 21 | type => 'refbook', | ||
| 22 | class => 'City', | ||
| 23 | xattr => {id_city => 'id'}, | ||
| 24 | view => 'city', | ||
| 25 | }, | ||
| 26 | }, | ||
| 27 | ); | ||
| 28 | (end code) | ||
| 29 | =cut | ||
| 30 | |||
| 31 | use v5.14; | ||
| 32 | use warnings; | ||
| 33 | |||
| 34 | use Crop::Error; | ||
| 35 | use Crop::Util 'load_class'; | ||
| 36 | use Crop::Object::Constants; | ||
| 37 | use Crop::Object::Warehouse::Lang::SQL::Query::Select::Node; | ||
| 38 | |||
| 39 | =begin nd | ||
| 40 | Variable: our %Attributes | ||
| 41 | Class attributes: | ||
| 42 | |||
| 43 | class - class of extended object | ||
| 44 | view - cached attribute where reuslt goes to | ||
| 45 | xattr - hash of corresponding attributes of extern key base=>ext | ||
| 46 | =cut | ||
| 47 | our %Attributes = ( | ||
| 48 | class => {mode => 'read'}, | ||
| 49 | view => {mode => 'read'}, | ||
| 50 | xattr => {mode => 'read'}, | ||
| 51 | ); | ||
| 52 | |||
| 53 | =begin nd | ||
| 54 | Method: make_link ($parent) | ||
| 55 | Create node and link it to the $parent. | ||
| 56 | |||
| 57 | The 'clause' attribute of new node will not be established. Calling code musth to do that work. | ||
| 58 | |||
| 59 | Parameters: | ||
| 60 | $parent - parent node | ||
| 61 | |||
| 62 | Returns: | ||
| 63 | new node - if ok | ||
| 64 | undef - otherwise | ||
| 65 | =cut | ||
| 66 | sub make_link { | ||
| 67 | my ($self, $parent) = @_; | ||
| 68 | |||
| 69 | my $class = $self->{class}; | ||
| 70 | load_class($class) or return warn "OBJECT|CRIT: Can not parse declaration for non-existing class $class"; | ||
| 71 | |||
| 72 | my $node = Crop::Object::Warehouse::Lang::SQL::Query::Select::Node->new( | ||
| 73 | class => $class, | ||
| 74 | table => $class->Table, | ||
| 75 | table_effective => $class->Table, | ||
| 76 | attr => $class->Attributes(STORED), | ||
| 77 | # clause => $clause, # should be calculated by calling code | ||
| 78 | parent => $parent, | ||
| 79 | parent_link => $self->{xattr}, | ||
| 80 | parent_view => $self->{view}, | ||
| 81 | ); | ||
| 82 | |||
| 83 | $parent->child->Push($node); | ||
| 84 | |||
| 85 | $node; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; |