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