RPN log: change some types to Odr_int
[yaz-moved-to-github.git] / src / rpn2cql.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file
8  * \brief Implements RPN to CQL conversion
9  *
10  */
11
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <yaz/rpn2cql.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/diagbib1.h>
18 #include <yaz/z-core.h>
19 #include <yaz/wrbuf.h>
20
21 static void wrbuf_vputs(const char *buf, void *client_data)
22 {
23     wrbuf_write((WRBUF) client_data, buf, strlen(buf));
24 }
25
26 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
27 {
28     int j;
29     int server_choice = 1;
30     for (j = 0; j < attributes->num_attributes; j++)
31     {
32         Z_AttributeElement *ae = attributes->attributes[j];
33         if (*ae->attributeType == 1) /* use attribute */
34         {
35             if (ae->which == Z_AttributeValue_complex)
36             {
37                 Z_ComplexAttribute *ca = ae->value.complex;
38                 int i;
39                 for (i = 0; i < ca->num_list; i++)
40                 {
41                     Z_StringOrNumeric *son = ca->list[i];
42                     if (son->which == Z_StringOrNumeric_string)
43                         return son->u.string;
44                 }
45             }
46             server_choice = 0; /* not serverChoice because we have use attr */
47         }
48     }
49     if (server_choice)
50         return "cql.serverChoice";
51     return 0;
52 }
53
54 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
55 {
56     int j;
57     for (j = 0; j < attributes->num_attributes; j++)
58     {
59         Z_AttributeElement *ae = attributes->attributes[j];
60         if (*ae->attributeType == 2) /* relation attribute */
61         {
62             if (ae->which == Z_AttributeValue_numeric)
63             {
64                 /* Only support for numeric relation */
65                 Odr_int *relation = ae->value.numeric;
66                 /* map this numeric to representation in CQL */
67                 switch (*relation)
68                 {
69                     /* Unsure on whether this is the relation attribute constants? */
70                 case Z_ProximityOperator_Prox_lessThan: 
71                     return "<";
72                 case Z_ProximityOperator_Prox_lessThanOrEqual: 
73                     return "<="; 
74                 case Z_ProximityOperator_Prox_equal: 
75                     return "="; 
76                 case Z_ProximityOperator_Prox_greaterThanOrEqual: 
77                     return ">="; 
78                 case Z_ProximityOperator_Prox_greaterThan: 
79                     return ">"; 
80                 case Z_ProximityOperator_Prox_notEqual: 
81                     return "<>"; 
82                 case 100: 
83                     /* phonetic is not supported in CQL */
84                     return 0; 
85                 case 101: 
86                     /* stem is not supported in CQL */
87                     return 0; 
88                 case 102: 
89                     /* relevance is supported in CQL, but not implemented yet */
90                     return 0; 
91                 default:
92                     /* Invalid relation */
93                     return 0;
94                 }
95             }
96             else {
97                 /*  Can we have a complex relation value?
98                     Should we implement something?
99                 */
100             }
101         }
102     }
103     return "=";
104 }
105
106 static int rpn2cql_attr(cql_transform_t ct,
107                         Z_AttributeList *attributes, WRBUF w)
108 {
109     const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
110     const char *index = cql_lookup_reverse(ct, "index.", attributes);
111     const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
112
113     /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
114     if (!index)
115         index = lookup_index_from_string_attr(attributes);
116
117     /* Attempt to fix bug #2978: Look for a relation attribute */
118     if (!relation) 
119         relation = lookup_relation_index_from_attr(attributes);
120
121     if (!index)
122     {
123         cql_transform_set_error(ct,
124                                 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
125         return -1;
126     }
127     /* for serverChoice we omit index+relation+structure */
128     if (strcmp(index, "cql.serverChoice"))
129     {
130         wrbuf_puts(w, index);
131         if (relation)
132         {
133             if (!strcmp(relation, "exact"))
134                 relation = "==";
135             else if (!strcmp(relation, "eq"))
136                 relation = "=";
137             else if (!strcmp(relation, "le"))
138                 relation = "<=";
139             else if (!strcmp(relation, "ge"))
140                 relation = ">=";
141             /* Missing mapping of not equal, phonetic, stem and relevance */
142             wrbuf_puts(w, relation);
143         }
144         else
145             wrbuf_puts(w, "=");
146
147         if (structure)
148         {
149             if (strcmp(structure, "*"))
150             {
151                 wrbuf_puts(w, "/");
152                 wrbuf_puts(w, structure);
153                 wrbuf_puts(w, " ");
154             }
155         }
156     }
157     return 0;
158 }
159
160 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
161 static int checkForTruncation(int flag, Z_AttributeList *attributes)
162 {
163     int j;
164     for (j = 0; j < attributes->num_attributes; j++)
165     {
166         Z_AttributeElement *ae = attributes->attributes[j];
167         if (*ae->attributeType == 5) /* truncation attribute */
168         {
169             if (ae->which == Z_AttributeValue_numeric)
170             {
171                 Odr_int truncation = *(ae->value.numeric);
172                 /* This logic only works for Left, right and both. eg. 1,2,3 */
173                 if (truncation <= 3)
174                     return ((int) truncation & flag);
175             }
176             /* Complex: Shouldn't happen */
177         }
178     }
179     /* No truncation or unsupported */
180     return 0;
181 };
182
183 static int checkForLeftTruncation(Z_AttributeList *attributes) {
184         return checkForTruncation(1, attributes);
185 }
186
187 static int checkForRightTruncation(Z_AttributeList *attributes) {
188         return checkForTruncation(2, attributes);
189 };
190
191 static int rpn2cql_simple(cql_transform_t ct,
192                           void (*pr)(const char *buf, void *client_data),
193                           void *client_data,
194                           Z_Operand *q, WRBUF w)
195 {
196     int ret = 0;
197     if (q->which != Z_Operand_APT)
198     {
199         ret = -1;
200         cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
201     }
202     else
203     {
204         Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
205         Z_Term *term = apt->term;
206         const char *sterm = 0;
207         size_t lterm = 0;
208
209         wrbuf_rewind(w);
210         ret = rpn2cql_attr(ct, apt->attributes, w);
211
212         switch(term->which)
213         {
214         case Z_Term_general:
215             lterm = term->u.general->len;
216             sterm = (const char *) term->u.general->buf;
217             break;
218         case Z_Term_numeric:
219             wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
220             break;
221         case Z_Term_characterString:
222             sterm = term->u.characterString;
223             lterm = strlen(sterm);
224             break;
225         default:
226             ret = -1;
227             cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
228         }
229
230         if (term)
231         {
232             size_t i;
233             int must_quote = 0;
234             for (i = 0 ; i < lterm; i++)
235                 if (sterm[i] == ' ')
236                     must_quote = 1;
237             if (must_quote)
238                 wrbuf_puts(w, "\"");
239             /* Bug 2878: Check and add Truncation */
240                         if (checkForLeftTruncation(apt->attributes))
241                 wrbuf_puts(w, "*");
242             wrbuf_write(w, sterm, lterm);
243             /* Bug 2878: Check and add Truncation */
244                         if (checkForRightTruncation(apt->attributes))
245                 wrbuf_puts(w, "*");
246             if (must_quote)
247                 wrbuf_puts(w, "\"");
248         }
249         if (ret == 0)
250             pr(wrbuf_cstr(w), client_data);
251     }
252     return ret;
253 }
254
255
256 static int rpn2cql_structure(cql_transform_t ct,
257                              void (*pr)(const char *buf, void *client_data),
258                              void *client_data,
259                              Z_RPNStructure *q, int nested,
260                              WRBUF w)
261 {
262     if (q->which == Z_RPNStructure_simple)
263         return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
264     else
265     {
266         Z_Operator *op = q->u.complex->roperator;
267         int r;
268
269         if (nested)
270             pr("(", client_data);
271
272         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
273         if (r)
274             return r;
275         switch(op->which)
276         {
277         case  Z_Operator_and:
278             pr(" and ", client_data);
279             break;
280         case  Z_Operator_or:
281             pr(" or ", client_data);
282             break;
283         case  Z_Operator_and_not:
284             pr(" not ", client_data);
285             break;
286         case  Z_Operator_prox:
287             cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
288             return -1;
289         }
290         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
291         if (nested)
292             pr(")", client_data);
293         return r;
294     }
295 }
296
297 int cql_transform_rpn2cql_stream(cql_transform_t ct,
298                                  void (*pr)(const char *buf, void *client_data),
299                                  void *client_data,
300                                  Z_RPNQuery *q)
301 {
302     int r;
303     WRBUF w = wrbuf_alloc();
304     cql_transform_set_error(ct, 0, 0);
305     r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
306     wrbuf_destroy(w);
307     return r;
308 }
309
310
311 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
312                                 WRBUF w,
313                                 Z_RPNQuery *q)
314 {
315     return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
316 }
317
318 /*
319  * Local variables:
320  * c-basic-offset: 4
321  * c-file-style: "Stroustrup"
322  * indent-tabs-mode: nil
323  * End:
324  * vim: shiftwidth=4 tabstop=8 expandtab
325  */
326