Net/Z3950/FOLIO/RPN.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 256 CC 47 SCAR 67.8
CC
47
Cov
0%
_CQLTerm
1332.0
toCQL
42.0
_ap2index
20.0
CRAP 878.9
1 # The following code maps Z39.50 Type-1 queries to CQL by providing a
2 # toCQL() method on each query tree node type.
3  
4 package Net::Z3950::RPN::Term;
5  
6 sub _throw { return Net::Z3950::FOLIO::_throw(@_); }
7  
8 sub toCQL {
9 my $self = shift;
10 my($session, $defaultSet) = @_;
11 my $indexMap = $session->{cfg}->{indexMap};
12 my($field, $relation);
13  
14 my $attrs = $self->{attributes};
15 untie $attrs;
16  
17 # First we determine USE attribute
18 foreach my $attr (@$attrs) {
19 my $set = $attr->{attributeSet} || $defaultSet;
20 if ($set ne Net::Z3950::FOLIO::ATTRSET_BIB1() &&
21 lc($set) ne 'bib-1') {
22 # Unknown attribute set (anything except BIB-1)
23 _throw(121, $set);
24 }
25 if ($attr->{attributeType} == 1) {
26 my $val = $attr->{attributeValue};
27 $field = _ap2index($indexMap, $val);
28 $relation = _ap2relation($indexMap, $val);
29 }
30 }
31  
32 if (!$field && $indexMap) {
33 # No explicit access-point, fall back to default if specified
34 $field = _ap2index($indexMap, 'default');
35 $relation = _ap2relation($indexMap, 'default');
36 }
37  
38 if ($field) {
39 my @fields = split(/,/, $field);
40 if (@fields > 1) {
41 return '(' . join(' or ', map { $self->_CQLTerm($_, $relation) } @fields) . ')';
42 }
43 }
44  
45 return $self->_CQLTerm($field, $relation);
46 }
47  
48  
49 sub _CQLTerm {
50 my $self = shift;
51 my($field, $relation) = @_;
52  
53 my($left_anchor, $right_anchor) = (0, 0);
54 my($left_truncation, $right_truncation) = (0, 0);
55 my $term = $self->{term};
56 my $attrs = $self->{attributes};
57  
58 if (defined $field && $field =~ /(.*?)\/(.*)/) {
59 $field = $1;
60 $relation = "=/$2";
61 }
62  
63 # Handle non-use attributes
64 foreach my $attr (@$attrs) {
65 my $type = $attr->{attributeType};
66 my $value = $attr->{attributeValue};
67  
68 if ($type == 2) {
69 # Relation. The following switch hard-codes information
70 # about the crrespondance between the BIB-1 attribute set
71 # and CQL context set.
72 if ($relation) {
73 if ($value == 1) {
74 $relation = "<";
75 } elsif ($value == 2) {
76 $relation = "<=";
77 } elsif ($value == 3) {
78 $relation = "=";
79 } elsif ($value == 4) {
80 $relation = ">=";
81 } elsif ($value == 5) {
82 $relation = ">";
83 } elsif ($value == 6) {
84 $relation = "<>";
85 } elsif ($value == 100) {
86 $relation = "=/phonetic";
87 } elsif ($value == 101) {
88 $relation = "=/stem";
89 } elsif ($value == 102) {
90 $relation = "=/relevant";
91 } else {
92 _throw(117, $value);
93 }
94 }
95 }
96  
97 elsif ($type == 3) { # Position
98 if ($value == 1 || $value == 2) {
99 $left_anchor = 1;
100 } elsif ($value != 3) {
101 _throw(119, $value);
102 }
103 }
104  
105 elsif ($type == 4) { # Structure -- we ignore it
106 }
107  
108 elsif ($type == 5) { # Truncation
109 if ($value == 1) {
110 $right_truncation = 1;
111 } elsif ($value == 2) {
112 $left_truncation = 1;
113 } elsif ($value == 3) {
114 $right_truncation = 1;
115 $left_truncation = 1;
116 } elsif ($value == 101) {
117 # Process # in search term
118 $term =~ s/#/?/g;
119 } elsif ($value == 104) {
120 # Z39.58-style (CCL) truncation: #=single char, ?=multiple
121 $term =~ s/\?\d?/*/g;
122 $term =~ s/#/?/g;
123 } elsif ($value != 100) {
124 _throw(120, $value);
125 }
126 }
127  
128 elsif ($type == 6) { # Completeness
129 if ($value == 2 || $value == 3) {
130 $left_anchor = $right_anchor = 1;
131 } elsif ($value != 1) {
132 _throw(122, $value);
133 }
134 }
135  
136 elsif ($type != 1) { # Unknown attribute type
137 _throw(113, $type);
138 }
139 }
140  
141 $term = "*$term" if $left_truncation;
142 $term = "$term*" if $right_truncation;
143 $term = "^$term" if $left_anchor;
144 $term = "$term^" if $right_anchor;
145  
146 $term = "\"$term\"" if $term =~ /[\s""\/=]/;
147  
148 if (defined $field && defined $relation) {
149 $term = "$field $relation $term";
150 } elsif (defined $field) {
151 $term = "$field=$term";
152 } elsif (defined $relation) {
153 $term = "cql.serverChoice $relation $term";
154 }
155  
156 return $term;
157 }
158  
159  
160 sub _ap2index {
161 my($indexMap, $value) = @_;
162  
163 if (!defined $indexMap) {
164 # This allows us to use string-valued attributes when no indexes are defined.
165 return $value;
166 }
167  
168 my $field = $indexMap->{$value};
169 _throw(114, $value) if !defined $field;
170 return $field->{cql} if ref $field;
171 return $field;
172 }
173  
174  
175 sub _ap2relation {
176 my($indexMap, $value) = @_;
177  
178 return undef if !defined $indexMap;
179 my $field = $indexMap->{$value};
180 return undef if !defined $field || !ref $field;
181 return $field->{relation};
182 }
183  
184  
185 package Net::Z3950::RPN::RSID;
186 sub toCQL {
187 my $self = shift;
188 my($session, $defaultSet) = @_;
189  
190 my $zid = $self->{id};
191 my $rs = $session->{resultsets}->{$zid};
192 Net::Z3950::FOLIO::_throw(128, $zid) if !defined $rs; # "Illegal result set name"
193  
194 my $sid = $rs->{rsid};
195 return qq[cql.resultSetId="$sid"]
196 }
197  
198  
199 package Net::Z3950::RPN::And;
200 sub toCQL {
201 my $self = shift;
202 my $left = $self->[0]->toCQL(@_);
203 my $right = $self->[1]->toCQL(@_);
204 return "($left and $right)";
205 }
206  
207  
208 package Net::Z3950::RPN::Or;
209 sub toCQL {
210 my $self = shift;
211 my $left = $self->[0]->toCQL(@_);
212 my $right = $self->[1]->toCQL(@_);
213 return "($left or $right)";
214 }
215  
216  
217 package Net::Z3950::RPN::AndNot;
218 sub toCQL {
219 my $self = shift;
220 my $left = $self->[0]->toCQL(@_);
221 my $right = $self->[1]->toCQL(@_);
222 return "($left not $right)";
223 }
224  
225 1;