1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SortSpec parsing.
16 #include <yaz/matchstr.h>
18 #include <yaz/z-core.h>
19 #include <yaz/sortspec.h>
20 #include <yaz/oid_db.h>
21 #include <yaz/wrbuf.h>
23 Z_SortKeySpecList *yaz_sort_spec(ODR out, const char *arg)
25 char sort_string_buf[64], sort_flags[64];
26 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
27 odr_malloc(out, sizeof(*sksl));
31 sksl->specs = (Z_SortKeySpec **)odr_malloc(out, sizeof(sksl->specs) * 20);
33 while ((sscanf(arg, "%63s %63s%n", sort_string_buf,
34 sort_flags, &off)) == 2 && off > 1)
37 char *sort_string_sep;
38 char *sort_string = sort_string_buf;
39 Z_SortKeySpec *sks = (Z_SortKeySpec *) odr_malloc(out, sizeof(*sks));
40 Z_SortKey *sk = (Z_SortKey *) odr_malloc(out, sizeof(*sk));
43 sksl->specs[sksl->num_specs++] = sks;
44 sks->sortElement = (Z_SortElement *)
45 odr_malloc(out, sizeof(*sks->sortElement));
46 sks->sortElement->which = Z_SortElement_generic;
47 sks->sortElement->u.generic = sk;
49 if ((sort_string_sep = strchr(sort_string, '=')))
52 sk->which = Z_SortKey_sortAttributes;
53 sk->u.sortAttributes = (Z_SortAttributes *)
54 odr_malloc(out, sizeof(*sk->u.sortAttributes));
55 sk->u.sortAttributes->id = odr_oiddup(out, yaz_oid_attset_bib_1);
56 sk->u.sortAttributes->list = (Z_AttributeList *)
57 odr_malloc(out, sizeof(*sk->u.sortAttributes->list));
58 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
60 sizeof(*sk->u.sortAttributes->list->attributes));
61 while (i < 10 && sort_string && sort_string_sep)
63 Z_AttributeElement *el = (Z_AttributeElement *)
64 odr_malloc(out, sizeof(*el));
65 sk->u.sortAttributes->list->attributes[i] = el;
67 el->attributeType = odr_intdup(out, atoi(sort_string));
68 el->which = Z_AttributeValue_numeric;
70 odr_intdup(out, odr_atoi(sort_string_sep + 1));
72 sort_string = strchr(sort_string, ',');
76 sort_string_sep = strchr(sort_string, '=');
79 sk->u.sortAttributes->list->num_attributes = i;
83 sk->which = Z_SortKey_sortField;
84 sk->u.sortField = odr_strdup (out, sort_string);
86 sks->sortRelation = odr_intdup(out, Z_SortKeySpec_ascending);
87 sks->caseSensitivity = odr_intdup(out, Z_SortKeySpec_caseInsensitive);
89 sks->which = Z_SortKeySpec_null;
90 sks->u.null = odr_nullval ();
92 for (i = 0; sort_flags[i]; i++)
94 switch (sort_flags[i])
99 *sks->sortRelation = Z_SortKeySpec_descending;
104 *sks->sortRelation = Z_SortKeySpec_ascending;
108 *sks->caseSensitivity = Z_SortKeySpec_caseInsensitive;
112 *sks->caseSensitivity = Z_SortKeySpec_caseSensitive;
115 sks->which = Z_SortKeySpec_abort;
116 sks->u.abort = odr_nullval();
119 sks->which = Z_SortKeySpec_missingValueData;
120 sks->u.missingValueData = (Odr_oct*)
121 odr_malloc(out, sizeof(Odr_oct));
123 sks->u.missingValueData->len = strlen(sort_flags+i);
124 sks->u.missingValueData->buf = odr_strdup(out, sort_flags+i);
125 i += strlen(sort_flags+i) - 1;
130 if (!sksl->num_specs)
135 int yaz_sort_spec_to_cql(Z_SortKeySpecList *sksl, WRBUF w)
138 for (i = 0; i < sksl->num_specs; i++)
140 Z_SortKeySpec *sks = sksl->specs[i];
143 if (sks->sortElement->which != Z_SortElement_generic)
146 sk = sks->sortElement->u.generic;
150 wrbuf_puts(w, " SORTBY ");
151 if (sk->which == Z_SortKey_sortAttributes)
153 else if (sk->which == Z_SortKey_sortField)
154 wrbuf_puts(w, sk->u.sortField);
155 switch (*sks->sortRelation)
157 case Z_SortKeySpec_ascending:
158 wrbuf_puts(w, "/ascending");
160 case Z_SortKeySpec_descending:
161 wrbuf_puts(w, "/descending");
164 switch (*sks->caseSensitivity)
166 case Z_SortKeySpec_caseSensitive:
167 wrbuf_puts(w, "/respectCase");
169 case Z_SortKeySpec_caseInsensitive:
170 wrbuf_puts(w, "/ignoreCase");
175 case Z_SortKeySpec_null:
177 case Z_SortKeySpec_abort:
178 wrbuf_puts(w, "/missingFail");
180 case Z_SortKeySpec_missingValueData:
181 wrbuf_puts(w, "/missingValue=");
182 wrbuf_write(w, (const char *) sks->u.missingValueData->buf,
183 sks->u.missingValueData->len);
190 int yaz_sort_spec_to_type7(Z_SortKeySpecList *sksl, WRBUF pqf)
193 for (i = 0; i < sksl->num_specs; i++)
195 Z_SortKeySpec *sks = sksl->specs[i];
198 if (sks->sortElement->which != Z_SortElement_generic)
201 sk = sks->sortElement->u.generic;
203 wrbuf_insert(pqf, 0, "@or ", 4);
205 if (sk->which == Z_SortKey_sortAttributes)
208 for (j = 0; j < sk->u.sortAttributes->list->num_attributes; j++)
210 Z_AttributeElement *el =
211 sk->u.sortAttributes->list->attributes[j];
212 if (el->which != Z_AttributeValue_numeric)
214 wrbuf_printf(pqf, " @attr " ODR_INT_PRINTF "=" ODR_INT_PRINTF,
215 *el->attributeType, *el->value.numeric);
218 else if (sk->which == Z_SortKey_sortField)
220 wrbuf_puts(pqf, " @attr 1=");
221 wrbuf_puts(pqf, sk->u.sortField);
223 switch (*sks->sortRelation)
225 case Z_SortKeySpec_ascending:
226 wrbuf_puts(pqf, " @attr 7=1 ");
228 case Z_SortKeySpec_descending:
229 wrbuf_puts(pqf, " @attr 7=2 ");
232 wrbuf_printf(pqf, "%d", i);
237 int yaz_sort_spec_to_srw_sortkeys(Z_SortKeySpecList *sksl, WRBUF w)
240 for (i = 0; i < sksl->num_specs; i++)
242 Z_SortKeySpec *sks = sksl->specs[i];
245 if (sks->sortElement->which != Z_SortElement_generic)
248 sk = sks->sortElement->u.generic;
253 if (sk->which == Z_SortKey_sortAttributes)
255 else if (sk->which == Z_SortKey_sortField)
257 wrbuf_puts(w, sk->u.sortField);
259 wrbuf_puts(w, ",,"); /* path is absent */
260 switch (*sks->sortRelation)
262 case Z_SortKeySpec_ascending:
265 case Z_SortKeySpec_descending:
270 switch (*sks->caseSensitivity)
272 case Z_SortKeySpec_caseSensitive:
275 case Z_SortKeySpec_caseInsensitive:
282 case Z_SortKeySpec_null:
283 wrbuf_puts(w, "highValue");
285 case Z_SortKeySpec_abort:
286 wrbuf_puts(w, "abort");
288 case Z_SortKeySpec_missingValueData:
289 wrbuf_write(w, (const char *) sks->u.missingValueData->buf,
290 sks->u.missingValueData->len);
297 int yaz_sort_spec_to_solr_sortkeys(Z_SortKeySpecList *sksl, WRBUF w)
300 for (i = 0; i < sksl->num_specs; i++)
302 Z_SortKeySpec *sks = sksl->specs[i];
305 if (sks->sortElement->which != Z_SortElement_generic)
308 sk = sks->sortElement->u.generic;
313 if (sk->which == Z_SortKey_sortAttributes)
315 else if (sk->which == Z_SortKey_sortField)
317 wrbuf_puts(w, sk->u.sortField);
319 switch (*sks->sortRelation)
321 case Z_SortKeySpec_ascending:
322 wrbuf_puts(w, " asc");
324 case Z_SortKeySpec_descending:
325 wrbuf_puts(w, " desc");
333 int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
335 /* sru sortkey layout: path,schema,ascending,caseSensitive,missingValue */
336 /* see cql_sortby_to_sortkeys of YAZ. */
338 int num_sortspec = 0;
340 NMEM nmem = nmem_create();
343 nmem_strsplit_blank(nmem, srw_sortkeys, &sortspec, &num_sortspec);
344 for (i = 0; i < num_sortspec; i++)
349 int case_sensitive = 0;
350 const char *missing = 0;
351 nmem_strsplitx(nmem, ",", sortspec[i], &arg, &num_arg, 0);
353 if (num_arg > 2 && arg[2][0])
354 ascending = atoi(arg[2]);
355 if (num_arg > 3 && arg[3][0])
356 case_sensitive = atoi(arg[3]);
357 if (num_arg > 4 && arg[4][0])
363 wrbuf_puts(w, arg[0]); /* field */
366 wrbuf_puts(w, ascending ? "a" : "d");
367 wrbuf_puts(w, case_sensitive ? "s" : "i");
370 if (!strcmp(missing, "omit")) {
373 else if (!strcmp(missing, "abort"))
375 else if (!strcmp(missing, "lowValue")) {
378 else if (!strcmp(missing, "highValue")) {
384 wrbuf_puts(w, missing);
392 int yaz_solr_sortkeys_to_sort_spec(const char *solr_sortkeys, WRBUF w)
394 /* Solr sortkey layout: field order[, field order] */
395 /* see cql_sortby_to_sortkeys of YAZ. */
397 int num_sortspec = 0;
399 NMEM nmem = nmem_create();
402 nmem_strsplit(nmem, ",", solr_sortkeys, &sortspec, &num_sortspec);
403 for (i = 0; i < num_sortspec; i++)
408 int case_sensitive = 0;
409 nmem_strsplitx(nmem, " ", sortspec[i], &arg, &num_arg, 0);
414 if (!yaz_matchstr(arg[1], "asc"))
416 else if (!yaz_matchstr(arg[1], "desc"))
424 wrbuf_puts(w, arg[0]); /* field */
427 wrbuf_putc(w, order);
428 // Always in-sensitive
429 wrbuf_puts(w, case_sensitive ? "s" : "i");
439 * c-file-style: "Stroustrup"
440 * indent-tabs-mode: nil
442 * vim: shiftwidth=4 tabstop=8 expandtab