blib/lib/PQL/Cache.pm

filter: Statementstmt 90.4% 352 / 389 Branchbran 67.5% 100 / 148 Conditioncond 56.2% 9 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 87.2% 41 / 47 Podpod 0.0% 0 / 20 totaltotal 80.1% 504 / 629 CC 99 SCAR 20.3
CC
99
Cov
83%
export_indexes
30.0
_find_matches
18.3
_handle_where
16.6
CRAP 7.7
↑ Summary
1 #==============================================================================
2 #
3 # PQL::Cache
4 # A cache using Perl Query Language, similar to SQL and DBIx::Class
5 #
6 # Ralf Peine, Sun Dec 21 13:51:53 2014
7 #
8 #==============================================================================
9  
10 package PQL::Cache;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 $VERSION ='0.800';
15  
16 # standards
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # use Data::Dumper;
20  
21 # --- Create Instance -----------------
22 sub new
23 1 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
24 1 my $caller = $_[0];
25 1 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
26  
27 # let the class go
28 1 my $self = {};
29 1 bless $self, $class;
30  
31 1 $self->_init();
32  
33 1 return $self;
34 }
35  
36 # --- _init ------------------------------------------------------------------
37 sub _init
38 1 ▶{
Subroutine _init: called CC=1 SCAR=0.0
39 1 my ($self # instance_ref
40 ) = @_;
41  
42 1 $self->clear();
43 1 $self->{_table_def} = {};
44 1 $self->_implement_compare_subs();
45 }
46  
47 # --- clear the stored data ---------------------------------
48 1 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
49 1 my ($self, # instance_ref
50 # $cache_to_clear # optional: cache to clear. clear all if unset
51 ) = @_;
52 1 $self->{_cache} = {};
53 1 $self->{_index} = {};
54 1 $self->{_next_free_index_list} = {};
55 }
56  
57 # --- table_definition ---------------------------------------------------------
58  
59 2 ▶sub set_table_definition {
Subroutine set_table_definition: called CC=2 SCAR=7.0
Pod undocumented
60 2 my ($self, # instance_ref
61 $table_name, # cache (table) to store data...
62 $table_def # definitions for table
63 ) = @_;
64 2 ▶ croak "no table set" unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 2 2
65  
66 2 $self->{_table_def}->{$table_name} = $table_def;
67 2 $self->_prepare_cache($table_name);
68 2 return $table_name;
69 }
70  
71 45 ▶sub get_table_definition {
Subroutine get_table_definition: called CC=3 SCAR=13.0
Pod undocumented
72 45 my ($self, # instance_ref
73 $table_name, # cache (table) to store data...
74 ) = @_;
75 45 ▶ croak "no table set " unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 45 45
76  
77 45 my $table_def = $self->{_table_def}->{$table_name};
78  
79 45 ▶ unless ($table_def) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_def) 0 45 45
80 0 my $known_tables = "known tables: ".join ("\n", sort keys (%{$self->{_table_def}}), '');
81 # print "$known_tables\n";
82 0 croak "No definition found for table [$table_name], $known_tables";
83 }
84  
85 45 return $table_def;
86 }
87  
88 # --- table cache --------------------------------------------
89  
90 69 ▶sub get_table_cache {
Subroutine get_table_cache: called CC=3 SCAR=11.3
Pod undocumented
91 69 my ($self, # instance_ref
92 $table_name, # cache (table) to store data...
93 ) = @_;
94 69 ▶ croak "no table set" unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 69 69
95  
96 69 my $table = $self->{_cache}->{$table_name};
97  
98 69 ▶ croak "table [$table_name] not found" unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 69 69
99  
100 69 return $table;
101 }
102  
103 # --- table_index --------------------------------------------
104  
105 39 ▶sub get_table_index {
Subroutine get_table_index: called CC=3 SCAR=11.3
Pod undocumented
106 39 my ($self, # instance_ref
107 $table_name, # index (table) to store data...
108 ) = @_;
109 39 ▶ croak "no table set" unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 39 39
110  
111 39 my $index = $self->{_index}->{$table_name};
112
113 39 ▶ croak "index of table [$table_name] not found" unless $index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 0 39 39
114  
115 39 return $index;
116 }
117  
118 # --- table_keys --------------------------------------------
119  
120 33 ▶sub get_table_keys {
Subroutine get_table_keys: called CC=3 SCAR=11.0
Pod undocumented
121 33 my ($self, # instance_ref
122 $table_name, # table
123 $column, # column
124 ) = @_;
125 33 ▶ croak "no table set" unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 33 33
126  
127 33 my $keys = $self->{_keys}->{$table_name};
128  
129 33 ▶ return $keys unless defined $column;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column 27 6 33
130
131 6 return $keys->{$column};
132 }
133  
134 # --- get index of next free field in table -----------------
135 6 ▶sub get_next_free_idx {
Subroutine get_next_free_idx: called CC=3 SCAR=11.1
Pod undocumented
136 6 my ($self, # instance_ref
137 $table_name, # cache (table) to store data...
138 ) = @_;
139  
140 6 my $cache = $self->get_table_cache($table_name);
141  
142 6 my $index = $self->{_next_free_index_list}->{$table_name};
143 6 ▶ $index = 0 unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 2 4 6
144 6 ▶ while ($cache->[$index]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->[$index]) 0 6 6
145 # print "#### search next free index $index\n";
146 0 $index++;
147 }
148 6 $self->{_next_free_index_list}->{$table_name} = $index + 1;
149 6 return $index;
150 }
151  
152 # --- get compare subs ---
153  
154 23 ▶sub get_compare_subs {
Subroutine get_compare_subs: called CC=2 SCAR=7.2
Pod undocumented
155 23 my ($self, # instance_ref
156 ) = @_;
157  
158 23 ▶ return $self->{_compare_subs} || {};
Condition50%1/2
$self->{'_compare_subs'} || {}
l!l
230
MC/DC0%0/1
$self->{'_compare_subs'} || {}
$self->{'_compare_subs'}
Truth table50%1/2
A
A= $self->{'_compare_subs'}
Aresult
11
00
159 }
160  
161 # --- get combine subs ---
162  
163 23 ▶sub get_combine_subs {
Subroutine get_combine_subs: called CC=2 SCAR=7.2
Pod undocumented
164 23 my ($self, # instance_ref
165 ) = @_;
166  
167 23 ▶ return $self->{_combine_subs} || {};
Condition50%1/2
$self->{'_combine_subs'} || {}
l!l
230
MC/DC0%0/1
$self->{'_combine_subs'} || {}
$self->{'_combine_subs'}
Truth table50%1/2
A
A= $self->{'_combine_subs'}
Aresult
11
00
168 }
169  
170 # --- get step2 subs ---
171  
172 23 ▶sub get_step2_subs {
Subroutine get_step2_subs: called CC=2 SCAR=7.2
Pod undocumented
173 23 my ($self, # instance_ref
174 ) = @_;
175  
176 23 ▶ return $self->{_step2_subs} || {};
Condition50%1/2
$self->{'_step2_subs'} || {}
l!l
230
MC/DC0%0/1
$self->{'_step2_subs'} || {}
$self->{'_step2_subs'}
Truth table50%1/2
A
A= $self->{'_step2_subs'}
Aresult
11
00
177 }
178  
179 # --- implement compare subs ----------------------------------------------------------------
180  
181 1 ▶sub _implement_compare_subs {
Subroutine _implement_compare_subs: called CC=4 SCAR=14.1
182 1 my ($self, # instance_ref
183 ) = @_;
184  
185 1 my $step2_subs = $self->{_step2_subs};
186  
187 1 ▶ $step2_subs = $self->{_step2_subs} = {} unless $step2_subs;
Branch: 1 branch
BranchTrueFalseTotal
unless $step2_subs 1 0 1
188  
189 1 ▶ $step2_subs->{ge} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
190 my ($column, $compare_value,
191 1 ) = @{$_[0]};
192
193 1 ▶ croak '"ge" operation needs SCALAR as parameter' if ref $compare_value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $compare_value 0 1 1
194 1 ▶ return sub { $_->{$column} ge $compare_value };
Subroutine __ANON__: called CC=1 SCAR=0.0
195 1 };
196  
197 2 ▶ $step2_subs->{le} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
198 my ($column, $compare_value,
199 2 ) = @{$_[0]};
200  
201 2 ▶ croak '"ge" operation needs SCALAR as parameter' if ref $compare_value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $compare_value 0 2 2
202 2 ▶ return sub { $_->{$column} le $compare_value };
Subroutine __ANON__: called CC=1 SCAR=0.0
203 1 };
204  
205 4 ▶ $step2_subs->{obj} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
206 my ($operation, $matching_sub,
207 4 ) = @{$_[0]};
208
209 4 ▶ croak '"$operation" operation needs CODE_REF as parameter' unless ref $matching_sub eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $matching_sub eq "CODE" 0 4 4
210 4 return $matching_sub;
211 1 };
212  
213 1 ▶ $step2_subs->{data} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
214 my ($operation, $matching_sub,
215 1 ) = @{$_[0]};
216 1 ▶ croak '"$operation" operation needs CODE_REF as parameter' unless ref $matching_sub eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $matching_sub eq "CODE" 0 1 1
217 1 return $matching_sub;
218 1 };
219  
220 1 ▶ $step2_subs->{sub_value} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
221 my ($key,
222 $matching_sub
223 1 ) = @{$_[0]};
224  
225 1 ▶ croak '"data" operation needs CODE_REF as parameter' unless ref $matching_sub eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $matching_sub eq "CODE" 0 1 1
226 1 ▶ return sub { my $obj = $_; { local $_ = $obj->{$key}; return $matching_sub->() }};
Subroutine __ANON__: called CC=1 SCAR=0.0
227 1 };
228  
229 1 my $combine_subs = $self->{_combine_subs};
230  
231 1 ▶ $combine_subs = $self->{_combine_subs} = {} unless $combine_subs;
Branch: 1 branch
BranchTrueFalseTotal
unless $combine_subs 1 0 1
232  
233 0 ▶ $combine_subs->{and} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
234 0 my ($conditions,
235 ) = @_;
236  
237 0 ▶ croak '"and" operation needs ARRAY_REF as parameter' unless ref $conditions eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conditions eq "ARRAY" 0 0 0
238  
239 0 return $self->_handle_where(@_);
240 1 };
241
242 1 my $compare_subs = $self->{_compare_subs};
243  
244 1 ▶ $compare_subs = $self->{_compare_subs} = {} unless $compare_subs;
Branch: 1 branch
BranchTrueFalseTotal
unless $compare_subs 1 0 1
245  
246 2 ▶ $compare_subs->{in} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
247 my ($key,
248 $array_ref
249 2 ) = @{$_[0]};
250  
251 2 ▶ croak '"in" operation needs ARRAY_REF as parameter' unless ref $array_ref eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $array_ref eq "ARRAY" 0 2 2
252  
253 2 my $search_key = $self->escape_regular_expression_special_chars($key);
254 2 my @in_select_arr;
255 2 foreach my $in_value (@$array_ref) {
256 7 $in_value = $self->escape_regular_expression_special_chars($in_value);
257 7 push (@in_select_arr, $in_value);
258 }
259 2 my $values_str = join ("|", @in_select_arr);
260 2 return ("#\\|$search_key=($values_str)\\|#", $values_str);
261
262 1 };
263
264 8 ▶ $compare_subs->{is} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
265 my ($key,
266 $value
267 8 ) = @{$_[0]};
268  
269 8 ▶ croak '"is" operation needs SCALAR as parameter'.ref $value if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 8 8
270  
271 8 my $search_key = $self->escape_regular_expression_special_chars($key);
272 8 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
273 8 $value = $self->escape_regular_expression_special_chars($value);
274 8 return ("#\\|$search_key=$value\\|#", $value);
275 1 };
276  
277 5 ▶ $compare_subs->{like} = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
278 my ($key,
279 $value
280 5 ) = @{$_[0]};
281
282 5 ▶ croak '"like" operation needs SCALAR as parameter'.ref $value if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 5 5
283
284 5 my $pattern = $value;
285  
286 5 my $search_key = $self->escape_regular_expression_special_chars($key);
287 5 ▶ if ($pattern =~ /^\^/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^\^/o) { } 1 4 5
288 1 $pattern =~ s/^\^//o;
289 }
290 else {
291 4 $pattern = "[^|]*".$pattern;
292 }
293
294 5 ▶ if ($pattern =~ /\$$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /\$$/o) { } 1 4 5
295 1 $pattern =~ s/\$$//o;
296 }
297 else {
298 4 $pattern .= '[^|]*'
299 }
300 # print "like $pattern\n";
301 5 return ("#\\|$search_key=$pattern\\|#", $value);
302 1 };
303  
304 0 ▶ $compare_subs->{or} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
305 0 carp "or is currently not supported";
306 1 };
307 }
308  
309 # --- prepare cache, if not already done ------------------
310  
311 8 ▶sub _prepare_cache {
Subroutine _prepare_cache: called CC=5 SCAR=16.1
312 8 my ($self, # instance_ref
313 $table_name, # cache (table) to store data...
314 ) = @_;
315  
316 8 ▶ $self->{_cache}->{$table_name} = [] unless $self->{_cache}->{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_cache"}{$table_name} 2 6 8
317 8 ▶ $self->{_index}->{$table_name} = [] unless $self->{_index}->{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_index"}{$table_name} 2 6 8
318  
319 8 ▶ unless ($self->{_keys}->{$table_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_keys"}{$table_name}) 2 6 8
320 2 my $table_def = $self->get_table_definition($table_name);
321 # print "# Define key columns for table $table_name: ";
322 2 foreach my $key_column (@{$table_def->{keys}}) {
323 # print "$key_column, ";
324 2 $self->{_keys}->{$table_name}->{$key_column} = {};
325 }
326 # print "\n";
327 }
328 }
329  
330 # --- add data ----------------------------------------------
331 6 ▶sub insert {
Subroutine insert: called CC=5 SCAR=17.5
Pod undocumented
332 6 my ($self, # instance_ref
333 $table_name, # cache (table) to store data...
334 $data,
335 ) = @_;
336  
337 6 ▶ croak "no table set" unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 6 6
338 6 ▶ croak "no data given" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 6 6
339  
340 6 $self->_prepare_cache($table_name);
341  
342 6 my $count = 0;
343 6 ▶ if (ref $data eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 6 6
344 0 foreach my $single_data (@$data) {
345 0 $count = $self->_insert($table_name, $single_data);
346 }
347 }
348 else {
349 6 $count = $self->_insert($table_name, $data);
350 }
351 6 return $count;
352 }
353  
354 # --- add data ----------------------------------------------
355 6 ▶sub _insert {
Subroutine _insert: called CC=3 SCAR=11.0
356 6 my ($self, # instance_ref
357 $table_name, # cache (table) to store data...
358 $data,
359 ) = @_;
360  
361 6 my $cache = $self->get_table_cache($table_name);
362 6 my $index = $self->get_table_index($table_name);
363  
364 6 my $next_idx = $self->get_next_free_idx($table_name);
365  
366 6 my $index_str = $self->build_index_string($table_name, $data, $next_idx);
367  
368 6 my $table_def = $self->get_table_definition($table_name);
369  
370 6 foreach my $key_column (@{$table_def->{keys}}) {
371 6 my $key_index_hash = $self->get_table_keys($table_name, $key_column);
372 6 my $value = $data->{$key_column}; # TODO: later use sub_ref to get value
373 6 my $key_index = $key_index_hash->{$value};
374 6 ▶ $key_index_hash->{$value} = $key_index = [] unless $key_index;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_index 6 0 6
375 6 push (@$key_index, $next_idx);
376 }
377  
378 6 $cache->[$next_idx] = $data;
379 6 $index->[$next_idx] = $index_str;
380 6 return scalar @$cache;
381 }
382  
383 # --- update the index key(s) ----------------------------------------------
384 1 ▶sub update_index {
Subroutine update_index: called CC=5 SCAR=17.2
Pod undocumented
385 1 my ($self, # instance_ref
386 $table_name, # cache (table) to store data...
387 $data, # optional: later update single data
388 ) = @_;
389  
390 1 ▶ croak "no table set" unless defined $table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_name 0 1 1
391  
392 1 my $cache_arr = $self->get_table_cache($table_name);
393 1 my $index_arr = $self->get_table_index($table_name);
394  
395 1 ▶ croak "No data found for table '$table_name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_arr and $index_arr 0 1 1
Condition33%1/3
$cache_arr and $index_arr
!ll&&!rl&&r
001
MC/DC0%0/2
$cache_arr and $index_arr
$cache_arr $index_arr
Truth table33%1/3
A and B
A= $cache_arrB= $index_arr
ABresult
0X0
100
111
396 unless $cache_arr && $index_arr;
397  
398 # print "### Update index for cache table $table_name\n";
399  
400 1 foreach my $index (0..$#$cache_arr) {
401 5 $self->_update_index_key($table_name, $index);
402 }
403 }
404  
405 # --- update the index key ----------------------------------------------
406 5 ▶sub _update_index_key {
Subroutine _update_index_key: called CC=2 SCAR=7.0
407 5 my ($self, # instance_ref
408 $table_name, # cache (table) to store data...
409 $index # internal index, where data is stored
410 ) = @_;
411  
412 5 my $cache_arr = $self->get_table_cache($table_name);
413 5 my $index_arr = $self->get_table_index($table_name);
414  
415 5 my $data = $cache_arr->[$index];
416  
417 5 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 5 5
418  
419 5 my $index_str = $self->build_index_string($table_name, $data, $index);
420  
421 # print "$index_str\n";
422  
423 5 $index_arr->[$index] = $index_str;
424 }
425  
426 # --- build the string to add to the index for searching -------------------
427 11 ▶sub build_index_string {
Subroutine build_index_string: called CC=3 SCAR=11.0
Pod undocumented
428 11 my ($self, # instance_ref
429 $table_name, # cache (table) to store data...
430 $data,
431 $index
432 ) = @_;
433  
434 11 my $table_def = $self->get_table_definition($table_name);
435  
436 11 my $index_str = "#|$index|#";
437  
438 11 my $value;
439 11 foreach my $key (@{$table_def->{keys}}, @{$table_def->{columns}}) {
440 66 $value = $data->{$key};
441 66 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 66 66
442 66 $index_str .= "#|$key=$value|#"
443 }
444  
445 11 return $index_str;
446 }
447  
448 # --- Same like SQL DELETE ---------------------------------
449  
450 1 ▶sub delete {
Subroutine delete: called CC=7 SCAR=19.5
Pod undocumented
451 1 my ($self, # instance_ref
452 %query, # perl query
453 ) = @_;
454  
455 1 my $tid_list = $self->_find_matches(%query);
456  
457 1 my $table_name = $query{from};
458 1 my $string_index_field = $self->get_table_index($table_name);
459 1 my $cache = $self->get_table_cache($table_name);
460  
461 1 my $deleted_rows_count = 0;
462 1 my $next_free_idx = $self->{_next_free_index_list}->{$table_name};
463 1 ▶ $next_free_idx = scalar @$cache unless defined $next_free_idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next_free_idx 0 1 1
464
465 1 foreach my $index (@$tid_list) {
466 1 my $object = $cache->[$index];
467 1 ▶ $next_free_idx = $index if $index < $next_free_idx;
Branch: 1 branch
BranchTrueFalseTotal
if $index < $next_free_idx 1 0 1
468 1 $cache->[$index] = '';
469 1 $string_index_field->[$index] = '';
470
471 1 my $table_key_indexes = $self->get_table_keys($table_name);
472 1 foreach my $column_key (keys(%$table_key_indexes)) {
473 1 my $values_hash = $table_key_indexes->{$column_key};
474 1 my $value = $object->{$column_key}; #TODO get value
475 1 my $index_arr_ref = $values_hash->{$value};
476 1 my $new_index_arr_ref = $values_hash->{$value} = [];
477 1 foreach my $other_index (@$index_arr_ref) {
478 1 ▶ push (@$new_index_arr_ref, $other_index) if $index != $other_index;
Branch: 1 branch
BranchTrueFalseTotal
if $index != $other_index 0 1 1
479 }
480 }
481 1 $deleted_rows_count++;
482 }
483  
484 1 $self->{_next_free_index_list}->{$table_name} = $next_free_idx;
485  
486 1 return $deleted_rows_count;
487 }
488  
489 # --- extract touple ids from selection list ---------------------------------
490 23 ▶sub extract_tid_from_selection {
Subroutine extract_tid_from_selection: called CC=2 SCAR=6.9
Pod undocumented
491 23 my ($self, # instance_ref
492 $selection, # perl query
493 ) = @_;
494  
495 my @result = map {
496 23 ▶ if (/^#\|(\d+)\|#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^#\|(\d+)\|#/) { } 61 1 62
497 61 $1;
498 } else {
499 ()
500 1 }
501 } @$selection;
502  
503 23 return \@result;
504 }
505  
506 # --- Same like SQL SELECT ---------------------------------
507 25 ▶sub select {
Subroutine select: called CC=2 SCAR=6.9
Pod undocumented
508 25 my ($self, # instance_ref
509 %query, # perl query
510 ) = @_;
511  
512 25 my $tids = $self->_find_matches(%query);
513  
514 24 my $table_name = $query{from};
515 24 my $cache = $self->get_table_cache($table_name);
516  
517 my @result = map {
518 24 $cache->[$_];
519 } @$tids;
520  
521 24 my $result_ref = \@result;
522
523 24 my $columns = $query{what};
524  
525 24 ▶ $result_ref = $self->_select_columns($columns, $result_ref)
Branch: 1 branch
BranchTrueFalseTotal
if $columns 24 0 24
526 if $columns;
527  
528 24 return $result_ref;
529 }
530  
531 # --- select columns ----------------------
532  
533 24 ▶sub _select_columns {
Subroutine _select_columns: called CC=5 SCAR=16.6
534 24 my ($self, # instance_ref
535 $columns, # string 'all' or ArrayRef with column names
536 $results, # ArrayRef of results
537 ) = @_;
538  
539 24 my $value_type = ref ($columns);
540 24 ▶ if ($value_type) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value_type) { } 1 23 24
elsif ($columns eq 'all') { } 23 0 23
541 1 ▶ if ($value_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_type eq 'ARRAY') { } 1 0 1
542 my @what_list = map {
543 1 my $result_row = {};
544 3 foreach my $key (@$columns) {
545 6 $result_row->{$key} = $_->{$key};
546 }
547 3 $result_row;
548 } @$results;
549 1 $results = \@what_list;
550 }
551 else {
552 0 croak "unknown column selection by what => $value_type ";
553 }
554 }
555 elsif ($columns eq 'all') {
556 }
557 else {
558 0 croak "unknown column selection by what => '$columns'";
559 }
560 24 return $results;
561 }
562  
563 # --- Find matching elements --- used for selection and removing ------
564 26 ▶sub _find_matches {
Subroutine _find_matches: called CC=18 SCAR=29.1
565 26 my ($self, # instance_ref
566 %query, # perl query
567 ) = @_;
568  
569 26 my $table_name = $query{from};
570 26 my $table_def = $self->get_table_definition($table_name);
571 26 my $string_index_field = $self->get_table_index($table_name);
572 26 my $key_index_field = $self->get_table_keys($table_name);
573 26 my $table_objs = $self->get_table_cache($table_name);
574  
575 26 my $search_str = "";
576  
577 # my %conditions = $self->_handle_where($query{where});
578 26 my $query = $self->_handle_where($query{where}, $key_index_field);
579 26 my $index_keys = $query->{indexes};
580 26 my $conditions = $query->{conditions};
581 26 my $values = $query->{values};
582 26 my $step2_operations = $query->{step2_operations};
583  
584 26 my @selection;
585 26 my $first_search = 1;
586 26 my $tids_first_run;
587  
588 26 ▶ if ($index_keys && scalar @$index_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_keys and scalar @$index_keys) 3 23 26
Condition100%2/2
$index_keys and scalar @$index_keys
!ll
323
MC/DC100%1/1
$index_keys and scalar @$index_keys
$index_keys
Truth table100%2/2
A
A= $index_keys
Aresult
00
11
589 3 $first_search = 0;
590 3 my ($column, $value);
591  
592 3 ▶ while (defined ($column = shift @$index_keys)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($column = shift @$index_keys)) 3 3 6
593 3 $value = shift @$index_keys;
594 3 my $matches = $key_index_field->{$column}->{$value};
595 3 ▶ if ($tids_first_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tids_first_run) { } 0 3 3
596 0 ▶ if (scalar @$tids_first_run < scalar @$matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$tids_first_run < scalar @$matches) 0 0 0
597 0 $tids_first_run = $matches;
598 }
599 }
600 else {
601 3 $tids_first_run = $matches;
602 }
603 }
604 }
605  
606 26 ▶ if ($first_search) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_search) { } 23 3 26
607 23 $search_str = "";
608  
609 23 foreach my $key (@{$table_def->{keys}}, @{$table_def->{columns}}) {
610
611 # print "#### '$key'\n";
612
613 138 ▶ if ($conditions->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conditions->{$key}) 14 124 138
614 14 ▶ $search_str .= ".*" if $search_str;
Branch: 1 branch
BranchTrueFalseTotal
if $search_str 2 12 14
615 14 $search_str .= $conditions->{$key};
616 }
617 }
618 # print "##### search string: $search_str\n";
619 23 ▶ if ($search_str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_str) { } 12 11 23
620 12 @selection = grep (/$search_str/, @$string_index_field);
621 }
622 else {
623 # --- empty condition, select all -------
624 11 @selection = @$string_index_field;
625 }
626
627 23 $tids_first_run = $self->extract_tid_from_selection(\@selection);
628 }
629 else {
630 3 my @step1_operations;
631 3 foreach my $key (keys(%$conditions)) {
632 0 my $pattern = $values->{$key};
633 # print "$key => $pattern\n";
634 0 ▶ push (@step1_operations, sub { $_->{$key} =~ /$pattern/ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
635 }
636 3 $step2_operations = [@step1_operations, @$step2_operations];
637 }
638  
639 # print Dumper($tids_first_run);
640  
641 26 my $tids = $tids_first_run;
642  
643 26 ▶ if ($step2_operations && scalar @$step2_operations) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step2_operations and scalar @$step2_operations) 7 19 26
Condition100%2/2
$step2_operations and scalar @$step2_operations
!ll
323
MC/DC100%1/1
$step2_operations and scalar @$step2_operations
$step2_operations
Truth table100%2/2
A
A= $step2_operations
Aresult
00
11
644 7 $tids = [];
645 7 foreach my $tid (@$tids_first_run) {
646 25 local $_ = $table_objs->[$tid];
647 25 my $selected = 1;
648 # print "$tid\n";
649 25 foreach my $selection_sub (@$step2_operations) {
650 # print Dumper ($selection_sub);
651 31 ▶ unless ($selection_sub->()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$selection_sub()) 16 14 30
652 16 $selected = 0;
653 16 last;
654 }
655 }
656 24 ▶ push (@$tids, $tid) if $selected;
Branch: 1 branch
BranchTrueFalseTotal
if $selected 8 16 24
657 }
658 }
659  
660 25 return $tids;
661 }
662  
663 # --- handle where condition part -----------------------------
664  
665 26 ▶sub _handle_where {
Subroutine _handle_where: called CC=16 SCAR=28.1
666 26 my ($self, # instance_ref
667 $where_condition, # perl query
668 $table_indexes, # hash with all indexes of table
669 ) = @_;
670  
671 26 my $conditions = {};
672 26 my $values = {};
673 26 my $operation_func_used = 0;
674 26 my $index = 0;
675 26 my $search_str;
676  
677 26 ▶ return $conditions unless $where_condition;
Branch: 1 branch
BranchTrueFalseTotal
unless $where_condition 3 23 26
678
679 23 my $compare_subs = $self->get_compare_subs();
680 23 my $combine_subs = $self->get_combine_subs();
681 23 my $step2_subs = $self->get_step2_subs();
682  
683 23 my @step2_operation_list;
684  
685 my @indexes;
686
687 23 ▶ while ($index < $#$where_condition) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $where_condition->$#*) 27 23 50
688 27 $operation_func_used = 0;
689 27 my $operation = '?';
690 27 my $param_1 = $where_condition->[$index++];
691 27 my $param_2 = $where_condition->[$index++];
692  
693 27 my $param_2_ref = ref $param_2;
694  
695 # print "###1 operation: $operation ($param_2_ref)\n";
696  
697 27 ▶ if (!$param_2_ref) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $param_2_ref) { } 11 16 27
elsif ($param_2_ref eq 'ARRAY') { } 2 14 16
elsif ($param_2_ref eq 'HASH') { } 8 6 14
elsif ($param_2_ref eq 'CODE') { } 6 0 6
698 11 $operation = 'is';
699 }
700 elsif ($param_2_ref eq "ARRAY") {
701 2 $operation = 'in';
702 }
703 elsif ($param_2_ref eq "HASH") {
704 8 ($operation) = keys %$param_2;
705 8 ($param_2) = values %$param_2;
706 }
707 elsif ($param_2_ref eq "CODE") {
708 6 local $_;
709 6 ▶ if ($param_1 eq 'data') {
Branch: 2 branches
BranchTrueFalseTotal
if ($param_1 eq 'data') { } 1 5 6
elsif ($param_1 eq 'obj') { } 4 1 5
710 1 $operation = 'data';
711 }
712 elsif ($param_1 eq 'obj') {
713 4 $operation = 'obj';
714 }
715 else {
716 1 $operation = 'sub_value';
717 }
718 }
719 else {
720 # print "####2 Dump param_2 = " .Dumper ($param_2);
721 0 print "###3 operation: $operation ($param_1, $param_2) ($param_2_ref)\n";
722
723 0 return ();
724 }
725
726 27 my $parameters = [$param_1, $param_2];
727  
728 27 my $comb_ref = $combine_subs->{$operation};
729  
730 27 ▶ return $comb_ref->($parameters, $table_indexes) if $comb_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $comb_ref 0 27 27
731  
732 27 my $use_index = 0;
733  
734 27 ▶ if ($operation eq 'is') {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq "is") 11 16 27
735 11 my $column = $parameters->[0];
736 11 my $value = $parameters->[1];
737 # print "is $column => $value => $table_indexes\n";
738 11 ▶ if ($table_indexes->{$column}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_indexes->{$column}) 3 8 11
739 # print "# --- use key index field [$column]\n";
740 3 $use_index = 1;
741 3 push (@indexes, $column, $value);
742 }
743 }
744  
745 27 ▶ unless ($use_index) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_index) 24 3 27
746 24 my $cond_ref = $compare_subs->{$operation};
747  
748 24 ▶ if ($cond_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond_ref) { } 15 9 24
749 15 my $column = $parameters->[0];
750 15 ($conditions->{$column}, $values->{$column}) = $cond_ref->($parameters);
751 } else {
752 9 my $step2_operation = $step2_subs->{$operation};
753
754 9 ▶ if ($step2_operation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step2_operation) { } 9 0 9
755 9 push (@step2_operation_list, $step2_operation->($parameters));
756 9 next;
757 }
758 else {
759 # parse_error ("Unknown operation '$operation'")
760 0 ▶ unless ($cond_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cond_ref) 0 0 0
761 0 warn ("Unknown operation '$operation'");
762 0 ▶ $cond_ref = sub { return 0; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
763 }
764 }
765 }
766 }
767 }
768  
769 return {
770 23 indexes => \@indexes,
771 conditions => $conditions,
772 values => $values,
773 step2_operations => \@step2_operation_list};
774 }
775  
776 # --- parse error --------------------------------------------
777  
778 0 ▶sub parse_error {
Subroutine parse_error: not called CC=1 SCAR=6.9
Pod undocumented
779 0 my $message = shift;
780  
781 0 croak "PQL-Parser: $message";
782 }
783  
784 # --- escape character with special meanings in reg exes for exact search ------------
785 30 ▶sub escape_regular_expression_special_chars {
Subroutine escape_regular_expression_special_chars: called CC=3 SCAR=11.0
Pod undocumented
786 30 my ($self, # instance_ref
787 $text # text to escape chars in it
788 ) = @_;
789  
790 30 my $result_text = '';
791 30 my $rest = $text;
792  
793 30 ▶ return $text unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 30 30
794  
795 30 ▶ while ($text =~ /([\/\(\)\[\]\.\?\+\*\^\$\|\{\}\\])/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ m<([/\(\)\[\]\.\?\+\*\^\$\|\{\}\\])>o) 3 30 33
796 3 $result_text .= $`."\\".$1;
797 3 $rest = $text = $';
798 }
799  
800 30 $result_text .= $rest;
801 30 return $result_text;
802 }
803  
804 # --- export indexes for human analysis -----------------------
805 # don't use data in any scripting !!!!!!!!!!!!!!!
806  
807 0 ▶ sub export_indexes {
Subroutine export_indexes: not called CC=5 SCAR=34.0
Pod undocumented
808 0 my ($self, # instance_ref
809 $table
810 ) = @_;
811  
812 0 my @results;
813  
814 0 ▶ if ($table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table) { } 0 0 0
815 0 my $string_index_field = $self->get_table_index($table);
816 0 foreach my $row (@$string_index_field) {
817 0 push (@results, "table:$table$row");
818 }
819 }
820 else {
821 0 foreach $table (sort(keys(%{$self->{_index}}))) {
822 0 my $string_index_field = $self->get_table_index($table);
823 0 foreach my $row (@$string_index_field) {
824 0 push (@results, "table:$table$row");
825 }
826 }
827 }
828 0 return \@results;
829 }
830  
831 1 1;
832  
833 __END__
↑ Summary