Crop/Object/Warehouse/Lang/SQL/Query/Select/Node.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 105 CC 5 SCAR 18.3
CC
5
Cov
0%
see_cache
20.0
parse_row
6.0
new
2.0
CRAP 6.2
1 package Crop::Object::Warehouse::Lang::SQL::Query::Select::Node;
2 use base qw/ Crop::Object /;
3  
4 =begin nd
5 Class: Crop::Object::Warehouse::Lang::SQL::Query::Select::Node
6 Node of parsed tree for a SELECT query.
7
8 Node presents an object-table.
9 =cut
10  
11 use v5.14;
12 use warnings;
13  
14 use Crop::Object::Constants;
15 use Crop::Object::Collection;
16 use Crop::Util qw/ load_class /;
17 use Crop::Error;
18  
19 use Crop::Debug;
20  
21 =begin nd
22 Variable: our %Attributes
23 Class members:
24  
25 attr - array of stored attributes of a table
26 cache - exemplars of 'class'
27 child - Collection of childs in the tree
28 class - object class name
29 clause - <Crop::Object::Warehouse::Lang::SQL::Clause> or undef
30 current - last item from current table;
31 object - either result blessed or Collection for root
32 parent - parent node or undef for root
33 parent_link - hash of parent=>child fields to link parent table
34 parent_view - parent attribute to hold extended object
35 pkey - array of ordered attributes names that consist the primary key
36 row_action - special action at data-handle stage; PARENTINIT only initialize parent view
37 table - original table name
38 table_effective - table name to used in JOIN
39 =cut
40 our %Attributes = (
41 attr => {mode => 'read'},
42 cache => {mode => 'read', default => {}},
43 child => {mode => 'read'},
44 class => {mode => 'read'},
45 clause => {mode => 'read/write'},
46 current => {mode => 'read/write'},
47 object => {mode => 'read/write'},
48 parent => {mode => 'read/write'},
49 parent_link => {mode => 'read'},
50 parent_view => {mode => 'read'},
51 pkey => {mode => 'read'},
52 row_action => {mode => 'read/write'},
53 table => {mode => 'read'},
54 table_effective => {mode => 'read'},
55 );
56  
57 =begin nd
58 Constructor: new (%attr)
59 Calculate shared attributes.
60
61 Returns:
62 $self - if ok
63 undef - otherwise
64 =cut
65 sub new {
66 my ($class, %attr) = @_;
67
68 exists $attr{class} or return warn "OBJECT|CRIT: Constructor of Node requires a 'class'";
69
70 $class->SUPER::new(
71 class => $attr{class},
72 pkey => [sort map $_->name, @{$attr{class}->Attributes(KEY)}],
73 table => $attr{class}->Table,
74 table_effective => $attr{class}->Table,
75 attr => $attr{class}->Attributes(STORED),
76 child => Crop::Object::Collection->new(__PACKAGE__),
77 %attr,
78 );
79 }
80  
81 =begin nd
82 Method: add_current ($item, $attrname)
83 Add object from database to the node resulting set.
84
85 Parameters:
86 $item - object to add
87 $attrname - target attribute
88 =cut
89 sub add_current {
90 my ($self, $item, $attrname) = @_;
91
92 $self->{current}{$attrname} = $item;
93 }
94  
95 =begin nd
96 Method: fields ( )
97 Get arrayref of each attribute prepended with table_effective.
98
99 Returns:
100 string such 'mytab$attr_1'
101 =cut
102 sub fields {
103 my $self = shift;
104
105 [
106 map "$self->{table_effective}." . $_->name . " AS $self->{table_effective}\$" . $_->name, @{$self->{attr}}
107
108 ];
109 }
110  
111 =begin nd
112 Method: init_parent_view ( )
113 Initialize parent view.
114  
115 Pure virtual.
116  
117 Returns:
118 error - in here
119 $self - in subclass
120 =cut
121 sub init_parent_view {
122 my $self = shift;
123 my $class = ref $self;
124
125 warn "DBASE: init_parent_view() must be redefined by subclass '$class'";
126 }
127  
128 =begin nd
129 Method: make_collection ($view)
130 Make a collection for $view.
131
132 Pure virtual.
133
134 Parameters:
135 $view - name of attribute
136 =cut
137 sub make_collection {
138 my ($self, $view) = @_;
139 my $class = ref $self;
140
141 warn "OBJECT|CRIT: make_collection($view) for class '$class' is not implemented";
142 }
143  
144 =begin nd
145 Method: parse_row ($row)
146 Handle row of the raw data from database and put object to the right place.
147
148 Virtual. Subclass could redefine this method.
149
150 Parameters:
151 $row - hashref to the entire row
152 =cut
153 sub parse_row {
154 my ($self, $row) = @_;
155 my $class = ref $self;
156
157 if ($class eq __PACKAGE__) {
158 my $item = $self->{class}->new($row->{$self->{table_effective}})->Set_state(DWHLINK);
159 $item->_Is_key_defined or return;
160  
161 # my $found_in_cache = $self->see_cache($item);
162
163 # if ($found_in_cache) {
164 # $item = $found_in_cache;
165 # }
166  
167 $self->{current} = $item;
168 $self->{parent}->add_current($item, $self->{parent_view});
169 } else {
170 warn "OBJECT|CRIT: parse_row() NOT IMPLEMENTED for class $class";
171 }
172 }
173  
174 =begin nd
175 Method: see_cache ($item)
176 Look up cache for $item.
177
178 If $item isn't in the cache, cache it.
179
180 Parameters:
181 $item - item to lookup
182
183 Returns:
184 cached item - if found
185 undef - otherwise
186 =cut
187 sub see_cache {
188 my ($self, $item) = @_;
189  
190 my $found;
191  
192 my $cur = $self->{cache};
193 for (my $i = 0; $i < @{$self->{pkey}}; ++$i) {
194 my $val = $item->{$self->{pkey}[$i]};
195
196 if ($i + 1 == @{$self->{pkey}}) { # last index
197 if (exists $cur->{$val}) {
198 $found = $cur->{$val}; # Found!
199 } else {
200 $cur->{$val} = $item; # put to cache
201 }
202 } else {
203 if (exists $cur->{$val}) {
204 $cur = $cur->{$val};
205 } else {
206 $cur->{$val} = {};
207 $cur = $cur->{$val}
208 }
209 }
210 }
211
212 $found;
213 }
214  
215 1;