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);
125 sks->u.missingValueData->size = sks->u.missingValueData->len;
127 sks->u.missingValueData->buf = odr_strdup(out, sort_flags+i);
128 i += strlen(sort_flags+i) - 1;
133 if (!sksl->num_specs)
138 int yaz_sort_spec_to_cql(Z_SortKeySpecList *sksl, WRBUF w)
141 for (i = 0; i < sksl->num_specs; i++)
143 Z_SortKeySpec *sks = sksl->specs[i];
146 if (sks->sortElement->which != Z_SortElement_generic)
149 sk = sks->sortElement->u.generic;
153 wrbuf_puts(w, " SORTBY ");
154 if (sk->which == Z_SortKey_sortAttributes)
156 else if (sk->which == Z_SortKey_sortField)
157 wrbuf_puts(w, sk->u.sortField);
158 switch (*sks->sortRelation)
160 case Z_SortKeySpec_ascending:
161 wrbuf_puts(w, "/ascending");
163 case Z_SortKeySpec_descending:
164 wrbuf_puts(w, "/descending");
167 switch (*sks->caseSensitivity)
169 case Z_SortKeySpec_caseSensitive:
170 wrbuf_puts(w, "/respectCase");
172 case Z_SortKeySpec_caseInsensitive:
173 wrbuf_puts(w, "/ignoreCase");
178 case Z_SortKeySpec_null:
180 case Z_SortKeySpec_abort:
181 wrbuf_puts(w, "/missingFail");
183 case Z_SortKeySpec_missingValueData:
184 wrbuf_puts(w, "/missingValue=");
185 wrbuf_write(w, (const char *) sks->u.missingValueData->buf,
186 sks->u.missingValueData->len);
193 int yaz_sort_spec_to_type7(Z_SortKeySpecList *sksl, WRBUF pqf)
196 for (i = 0; i < sksl->num_specs; i++)
198 Z_SortKeySpec *sks = sksl->specs[i];
201 if (sks->sortElement->which != Z_SortElement_generic)
204 sk = sks->sortElement->u.generic;
206 wrbuf_insert(pqf, 0, "@or ", 4);
208 if (sk->which == Z_SortKey_sortAttributes)
211 for (j = 0; j < sk->u.sortAttributes->list->num_attributes; j++)
213 Z_AttributeElement *el =
214 sk->u.sortAttributes->list->attributes[j];
215 if (el->which != Z_AttributeValue_numeric)
217 wrbuf_printf(pqf, " @attr " ODR_INT_PRINTF "=" ODR_INT_PRINTF,
218 *el->attributeType, *el->value.numeric);
221 else if (sk->which == Z_SortKey_sortField)
223 wrbuf_puts(pqf, " @attr 1=");
224 wrbuf_puts(pqf, sk->u.sortField);
226 switch (*sks->sortRelation)
228 case Z_SortKeySpec_ascending:
229 wrbuf_puts(pqf, " @attr 7=1 ");
231 case Z_SortKeySpec_descending:
232 wrbuf_puts(pqf, " @attr 7=2 ");
235 wrbuf_printf(pqf, "%d", i);
240 int yaz_sort_spec_to_srw_sortkeys(Z_SortKeySpecList *sksl, WRBUF w)
243 for (i = 0; i < sksl->num_specs; i++)
245 Z_SortKeySpec *sks = sksl->specs[i];
248 if (sks->sortElement->which != Z_SortElement_generic)
251 sk = sks->sortElement->u.generic;
256 if (sk->which == Z_SortKey_sortAttributes)
258 else if (sk->which == Z_SortKey_sortField)
260 wrbuf_puts(w, sk->u.sortField);
262 wrbuf_puts(w, ",,"); /* path is absent */
263 switch (*sks->sortRelation)
265 case Z_SortKeySpec_ascending:
268 case Z_SortKeySpec_descending:
273 switch (*sks->caseSensitivity)
275 case Z_SortKeySpec_caseSensitive:
278 case Z_SortKeySpec_caseInsensitive:
285 case Z_SortKeySpec_null:
286 wrbuf_puts(w, "highValue");
288 case Z_SortKeySpec_abort:
289 wrbuf_puts(w, "abort");
291 case Z_SortKeySpec_missingValueData:
292 wrbuf_write(w, (const char *) sks->u.missingValueData->buf,
293 sks->u.missingValueData->len);
300 int yaz_sort_spec_to_solr_sortkeys(Z_SortKeySpecList *sksl, WRBUF w)
303 for (i = 0; i < sksl->num_specs; i++)
305 Z_SortKeySpec *sks = sksl->specs[i];
308 if (sks->sortElement->which != Z_SortElement_generic)
311 sk = sks->sortElement->u.generic;
316 if (sk->which == Z_SortKey_sortAttributes)
318 else if (sk->which == Z_SortKey_sortField)
320 wrbuf_puts(w, sk->u.sortField);
322 switch (*sks->sortRelation)
324 case Z_SortKeySpec_ascending:
325 wrbuf_puts(w, " asc");
327 case Z_SortKeySpec_descending:
328 wrbuf_puts(w, " desc");
336 int yaz_srw_sortkeys_to_sort_spec(const char *srw_sortkeys, WRBUF w)
338 /* sru sortkey layout: path,schema,ascending,caseSensitive,missingValue */
339 /* see cql_sortby_to_sortkeys of YAZ. */
341 int num_sortspec = 0;
343 NMEM nmem = nmem_create();
346 nmem_strsplit_blank(nmem, srw_sortkeys, &sortspec, &num_sortspec);
347 for (i = 0; i < num_sortspec; i++)
352 int case_sensitive = 0;
353 const char *missing = 0;
354 nmem_strsplitx(nmem, ",", sortspec[i], &arg, &num_arg, 0);
356 if (num_arg > 2 && arg[2][0])
357 ascending = atoi(arg[2]);
358 if (num_arg > 3 && arg[3][0])
359 case_sensitive = atoi(arg[3]);
360 if (num_arg > 4 && arg[4][0])
366 wrbuf_puts(w, arg[0]); /* field */
369 wrbuf_puts(w, ascending ? "a" : "d");
370 wrbuf_puts(w, case_sensitive ? "s" : "i");
373 if (!strcmp(missing, "omit")) {
376 else if (!strcmp(missing, "abort"))
378 else if (!strcmp(missing, "lowValue")) {
381 else if (!strcmp(missing, "highValue")) {
387 wrbuf_puts(w, missing);
395 int yaz_solr_sortkeys_to_sort_spec(const char *solr_sortkeys, WRBUF w)
397 /* Solr sortkey layout: field order[, field order] */
398 /* see cql_sortby_to_sortkeys of YAZ. */
400 int num_sortspec = 0;
402 NMEM nmem = nmem_create();
405 nmem_strsplit(nmem, ",", solr_sortkeys, &sortspec, &num_sortspec);
406 for (i = 0; i < num_sortspec; i++)
411 int case_sensitive = 0;
412 nmem_strsplitx(nmem, " ", sortspec[i], &arg, &num_arg, 0);
417 if (!yaz_matchstr(arg[1], "asc"))
419 else if (!yaz_matchstr(arg[1], "desc"))
427 wrbuf_puts(w, arg[0]); /* field */
430 wrbuf_putc(w, order);
431 // Always in-sensitive
432 wrbuf_puts(w, case_sensitive ? "s" : "i");
442 * c-file-style: "Stroustrup"
443 * indent-tabs-mode: nil
445 * vim: shiftwidth=4 tabstop=8 expandtab