egdb3_11_7
.metabib
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
compile_composite_attr(cattr_def text)
Parameters
Name
Type
Mode
cattr_def
text
IN
Definition
use JSON::XS; my $json = shift; my $def = decode_json($json); die("Composite attribute definition not supplied") unless $def; my $_cache = (exists $_SHARED{metabib_compile_composite_attr_cache}) ? 1 : 0; return $_SHARED{metabib_compile_composite_attr_cache}{$json} if ($_cache && $_SHARED{metabib_compile_composite_attr_cache}{$json}); sub recurse { my $d = shift; my $j = '&'; my @list; if (ref $d eq 'HASH') { # node or AND if (exists $d->{_attr}) { # it is a node my $plan = spi_prepare('SELECT * FROM metabib.full_attr_id_map WHERE attr = $1 AND value = $2', qw/TEXT TEXT/); my $id = spi_exec_prepared( $plan, {limit => 1}, $d->{_attr}, $d->{_val} )->{rows}[0]{id}; spi_freeplan($plan); return $id; } elsif (exists $d->{_not} && scalar(keys(%$d)) == 1) { # it is a NOT return '!' . recurse($$d{_not}); } else { # an AND list @list = map { recurse($$d{$_}) } sort keys %$d; } } elsif (ref $d eq 'ARRAY') { $j = '|'; @list = map { recurse($_) } @$d; } @list = grep { defined && $_ ne '' } @list; return '(' . join($j,@list) . ')' if @list; return ''; } my $val = recurse($def) || undef; $_SHARED{metabib_compile_composite_attr_cache}{$json} = $val if $_cache; return $val;