1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM C query interface.
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
21 #include <yaz/pquery.h>
24 #include <yaz/sortspec.h>
26 #define SORT_STRATEGY_Z3950 0
27 #define SORT_STRATEGY_TYPE7 1
28 #define SORT_STRATEGY_CQL 2
29 #define SORT_STRATEGY_SRU11 3
30 #define SORT_STRATEGY_EMBED 4
31 #define SORT_STRATEGY_SOLR 5
36 Z_SortKeySpecList *sort_spec;
43 WRBUF sru11_sort_spec;
46 static int generate(ZOOM_query s)
52 wrbuf_rewind(s->full_query);
53 wrbuf_puts(s->full_query, s->query_string);
54 odr_reset(s->odr_query);
56 if (s->sort_spec && (s->sort_strategy == SORT_STRATEGY_SRU11 || s->sort_strategy == SORT_STRATEGY_SOLR))
59 wrbuf_rewind(s->sru11_sort_spec);
61 switch (s->sort_strategy)
63 case SORT_STRATEGY_SRU11:
64 r = yaz_sort_spec_to_srw_sortkeys(s->sort_spec, s->sru11_sort_spec);
66 case SORT_STRATEGY_SOLR:
67 r = yaz_sort_spec_to_solr_sortkeys(s->sort_spec, s->sru11_sort_spec);
73 switch (s->query_type)
75 case Z_Query_type_1: /* RPN */
77 (s->sort_strategy == SORT_STRATEGY_TYPE7 ||
78 s->sort_strategy == SORT_STRATEGY_EMBED))
80 int r = yaz_sort_spec_to_type7(s->sort_spec, s->full_query);
84 s->z_query = (Z_Query *) odr_malloc(s->odr_query,
86 s->z_query->which = Z_Query_type_1;
87 s->z_query->u.type_1 =
88 p_query_rpn(s->odr_query, wrbuf_cstr(s->full_query));
89 if (!s->z_query->u.type_1)
95 case Z_Query_type_104: /* CQL */
97 (s->sort_strategy == SORT_STRATEGY_CQL ||
98 s->sort_strategy == SORT_STRATEGY_EMBED))
100 int r = yaz_sort_spec_to_cql(s->sort_spec, s->full_query);
104 ext = (Z_External *) odr_malloc(s->odr_query, sizeof(*ext));
105 ext->direct_reference = odr_oiddup(s->odr_query,
106 yaz_oid_userinfo_cql);
107 ext->indirect_reference = 0;
109 ext->which = Z_External_CQL;
110 ext->u.cql = odr_strdup(s->odr_query, wrbuf_cstr(s->full_query));
112 s->z_query = (Z_Query *) odr_malloc(s->odr_query, sizeof(*s->z_query));
113 s->z_query->which = Z_Query_type_104;
114 s->z_query->u.type_104 = ext;
122 const char *ZOOM_query_get_sru11(ZOOM_query s)
124 if (wrbuf_len(s->sru11_sort_spec))
125 return wrbuf_cstr(s->sru11_sort_spec);
129 Z_Query *ZOOM_query_get_Z_Query(ZOOM_query s)
134 Z_SortKeySpecList *ZOOM_query_get_sortspec(ZOOM_query s)
136 return s->sort_strategy == SORT_STRATEGY_Z3950 ? s->sort_spec : 0;
139 const char *ZOOM_query_get_query_string(ZOOM_query s)
141 return wrbuf_cstr(s->full_query);
144 void ZOOM_query_get_hash(ZOOM_query s, WRBUF w)
146 wrbuf_printf(w, "%d;", s->query_type);
148 wrbuf_puts(w, s->query_string);
149 wrbuf_printf(w, ";%d;", s->sort_strategy);
151 yaz_sort_spec_to_type7(s->sort_spec, w);
155 * Returns an xmalloc()d string containing RPN that corresponds to the
156 * CQL passed in. On error, sets the Connection object's error state
157 * and returns a null pointer.
158 * ### We could cache CQL parser and/or transformer in Connection.
160 static char *cql2pqf(ZOOM_connection c, const char *cql)
165 cql_transform_t trans;
168 parser = cql_parser_create();
169 if ((error = cql_parser_string(parser, cql)) != 0) {
170 cql_parser_destroy(parser);
171 ZOOM_set_error(c, ZOOM_ERROR_CQL_PARSE, cql);
175 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
178 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
180 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
183 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
184 cqlfile, strerror(errno));
185 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
189 WRBUF wrbuf_result = wrbuf_alloc();
190 error = cql_transform(trans, cql_parser_result(parser),
191 wrbuf_vp_puts, wrbuf_result);
195 error = cql_transform_error(trans, &addinfo);
196 sprintf(buf, "%.200s (addinfo=%.200s)",
197 cql_strerror(error), addinfo);
198 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
202 result = xstrdup(wrbuf_cstr(wrbuf_result));
204 cql_transform_close(trans);
205 wrbuf_destroy(wrbuf_result);
207 cql_parser_destroy(parser);
213 ZOOM_query_create(void)
215 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
220 s->odr_query = odr_createmem(ODR_ENCODE);
221 s->odr_sort_spec = odr_createmem(ODR_ENCODE);
223 s->full_query = wrbuf_alloc();
224 s->sort_strategy = SORT_STRATEGY_Z3950;
225 s->sru11_sort_spec = wrbuf_alloc();
230 ZOOM_query_destroy(ZOOM_query s)
236 if (s->refcount == 0)
238 odr_destroy(s->odr_query);
239 odr_destroy(s->odr_sort_spec);
240 xfree(s->query_string);
241 wrbuf_destroy(s->full_query);
242 wrbuf_destroy(s->sru11_sort_spec);
248 ZOOM_query_addref(ZOOM_query s)
255 ZOOM_query_prefix(ZOOM_query s, const char *str)
257 xfree(s->query_string);
258 s->query_string = xstrdup(str);
259 s->query_type = Z_Query_type_1;
264 ZOOM_query_cql(ZOOM_query s, const char *str)
266 xfree(s->query_string);
267 s->query_string = xstrdup(str);
268 s->query_type = Z_Query_type_104;
273 * Translate the CQL string client-side into RPN which is passed to
274 * the server. This is useful for server's that don't themselves
275 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
276 * only as a place to stash diagnostics if compilation fails; if this
277 * information is not needed, a null pointer may be used.
280 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
284 ZOOM_connection freeme = 0;
287 conn = freeme = ZOOM_connection_create(0);
289 rpn = cql2pqf(conn, str);
291 ZOOM_connection_destroy(freeme);
295 ret = ZOOM_query_prefix(s, rpn);
301 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
302 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
303 * to the server, as the YAZ GFS doesn't know how to handle this.
306 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
307 int *ccl_error, const char **error_string,
311 struct ccl_rpn_node *rpn;
312 CCL_bibset bibset = ccl_qual_mk();
315 ccl_qual_buf(bibset, config);
317 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
320 *error_string = ccl_err_msg(*ccl_error);
325 WRBUF wr = wrbuf_alloc();
328 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
331 ccl_qual_rm(&bibset);
336 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
338 return ZOOM_query_sortby2(s, "z3950", criteria);
342 ZOOM_query_sortby2(ZOOM_query s, const char *strategy, const char *criteria)
344 if (!strcmp(strategy, "z3950"))
346 s->sort_strategy = SORT_STRATEGY_Z3950;
348 else if (!strcmp(strategy, "type7"))
350 s->sort_strategy = SORT_STRATEGY_TYPE7;
352 else if (!strcmp(strategy, "cql"))
354 s->sort_strategy = SORT_STRATEGY_CQL;
356 else if (!strcmp(strategy, "sru11"))
358 s->sort_strategy = SORT_STRATEGY_SRU11;
360 else if (!strcmp(strategy, "solr"))
362 s->sort_strategy = SORT_STRATEGY_SOLR;
364 else if (!strcmp(strategy, "embed"))
366 s->sort_strategy = SORT_STRATEGY_EMBED;
371 odr_reset(s->odr_sort_spec);
372 s->sort_spec = yaz_sort_spec(s->odr_sort_spec, criteria);
381 * c-file-style: "Stroustrup"
382 * indent-tabs-mode: nil
384 * vim: shiftwidth=4 tabstop=8 expandtab