1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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
35 Z_SortKeySpecList *sort_spec;
42 WRBUF sru11_sort_spec;
45 static int generate(ZOOM_query s)
51 wrbuf_rewind(s->full_query);
52 wrbuf_puts(s->full_query, s->query_string);
53 odr_reset(s->odr_query);
55 if (s->sort_spec && s->sort_strategy == SORT_STRATEGY_SRU11)
58 wrbuf_rewind(s->sru11_sort_spec);
60 r = yaz_sort_spec_to_srw_sortkeys(s->sort_spec,
65 switch (s->query_type)
67 case Z_Query_type_1: /* RPN */
69 (s->sort_strategy == SORT_STRATEGY_TYPE7 ||
70 s->sort_strategy == SORT_STRATEGY_EMBED))
72 int r = yaz_sort_spec_to_type7(s->sort_spec, s->full_query);
76 s->z_query = (Z_Query *) odr_malloc(s->odr_query,
78 s->z_query->which = Z_Query_type_1;
79 s->z_query->u.type_1 =
80 p_query_rpn(s->odr_query, wrbuf_cstr(s->full_query));
81 if (!s->z_query->u.type_1)
87 case Z_Query_type_104: /* CQL */
89 (s->sort_strategy == SORT_STRATEGY_CQL ||
90 s->sort_strategy == SORT_STRATEGY_EMBED))
92 int r = yaz_sort_spec_to_cql(s->sort_spec, s->full_query);
96 ext = (Z_External *) odr_malloc(s->odr_query, sizeof(*ext));
97 ext->direct_reference = odr_oiddup(s->odr_query,
98 yaz_oid_userinfo_cql);
99 ext->indirect_reference = 0;
101 ext->which = Z_External_CQL;
102 ext->u.cql = odr_strdup(s->odr_query, wrbuf_cstr(s->full_query));
104 s->z_query = (Z_Query *) odr_malloc(s->odr_query, sizeof(*s->z_query));
105 s->z_query->which = Z_Query_type_104;
106 s->z_query->u.type_104 = ext;
114 const char *ZOOM_query_get_sru11(ZOOM_query s)
116 if (wrbuf_len(s->sru11_sort_spec))
117 return wrbuf_cstr(s->sru11_sort_spec);
121 Z_Query *ZOOM_query_get_Z_Query(ZOOM_query s)
126 Z_SortKeySpecList *ZOOM_query_get_sortspec(ZOOM_query s)
128 return s->sort_strategy == SORT_STRATEGY_Z3950 ? s->sort_spec : 0;
131 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
133 WRBUF wrbuf = (WRBUF) client_data;
134 wrbuf_puts(wrbuf, buf);
137 const char *ZOOM_query_get_query_string(ZOOM_query s)
139 return wrbuf_cstr(s->full_query);
143 * Returns an xmalloc()d string containing RPN that corresponds to the
144 * CQL passed in. On error, sets the Connection object's error state
145 * and returns a null pointer.
146 * ### We could cache CQL parser and/or transformer in Connection.
148 static char *cql2pqf(ZOOM_connection c, const char *cql)
153 cql_transform_t trans;
156 parser = cql_parser_create();
157 if ((error = cql_parser_string(parser, cql)) != 0) {
158 cql_parser_destroy(parser);
159 ZOOM_set_error(c, ZOOM_ERROR_CQL_PARSE, cql);
163 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
166 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
168 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
171 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
172 cqlfile, strerror(errno));
173 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
177 WRBUF wrbuf_result = wrbuf_alloc();
178 error = cql_transform(trans, cql_parser_result(parser),
179 cql2pqf_wrbuf_puts, wrbuf_result);
183 error = cql_transform_error(trans, &addinfo);
184 sprintf(buf, "%.200s (addinfo=%.200s)",
185 cql_strerror(error), addinfo);
186 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
190 result = xstrdup(wrbuf_cstr(wrbuf_result));
192 cql_transform_close(trans);
193 wrbuf_destroy(wrbuf_result);
195 cql_parser_destroy(parser);
201 ZOOM_query_create(void)
203 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
208 s->odr_query = odr_createmem(ODR_ENCODE);
209 s->odr_sort_spec = odr_createmem(ODR_ENCODE);
211 s->full_query = wrbuf_alloc();
212 s->sort_strategy = SORT_STRATEGY_Z3950;
213 s->sru11_sort_spec = wrbuf_alloc();
218 ZOOM_query_destroy(ZOOM_query s)
224 if (s->refcount == 0)
226 odr_destroy(s->odr_query);
227 odr_destroy(s->odr_sort_spec);
228 xfree(s->query_string);
229 wrbuf_destroy(s->full_query);
230 wrbuf_destroy(s->sru11_sort_spec);
236 ZOOM_query_addref(ZOOM_query s)
243 ZOOM_query_prefix(ZOOM_query s, const char *str)
245 xfree(s->query_string);
246 s->query_string = xstrdup(str);
247 s->query_type = Z_Query_type_1;
252 ZOOM_query_cql(ZOOM_query s, const char *str)
254 xfree(s->query_string);
255 s->query_string = xstrdup(str);
256 s->query_type = Z_Query_type_104;
261 * Translate the CQL string client-side into RPN which is passed to
262 * the server. This is useful for server's that don't themselves
263 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
264 * only as a place to stash diagnostics if compilation fails; if this
265 * information is not needed, a null pointer may be used.
268 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
272 ZOOM_connection freeme = 0;
275 conn = freeme = ZOOM_connection_create(0);
277 rpn = cql2pqf(conn, str);
279 ZOOM_connection_destroy(freeme);
283 ret = ZOOM_query_prefix(s, rpn);
289 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
290 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
291 * to the server, as the YAZ GFS doesn't know how to handle this.
294 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
295 int *ccl_error, const char **error_string,
299 struct ccl_rpn_node *rpn;
300 CCL_bibset bibset = ccl_qual_mk();
303 ccl_qual_buf(bibset, config);
305 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
308 *error_string = ccl_err_msg(*ccl_error);
313 WRBUF wr = wrbuf_alloc();
316 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
319 ccl_qual_rm(&bibset);
324 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
326 return ZOOM_query_sortby2(s, "z3950", criteria);
330 ZOOM_query_sortby2(ZOOM_query s, const char *strategy, const char *criteria)
332 if (!strcmp(strategy, "z3950"))
334 s->sort_strategy = SORT_STRATEGY_Z3950;
336 else if (!strcmp(strategy, "type7"))
338 s->sort_strategy = SORT_STRATEGY_TYPE7;
340 else if (!strcmp(strategy, "cql"))
342 s->sort_strategy = SORT_STRATEGY_CQL;
344 else if (!strcmp(strategy, "sru11"))
346 s->sort_strategy = SORT_STRATEGY_SRU11;
348 else if (!strcmp(strategy, "embed"))
350 s->sort_strategy = SORT_STRATEGY_EMBED;
355 odr_reset(s->odr_sort_spec);
356 s->sort_spec = yaz_sort_spec(s->odr_sort_spec, criteria);
365 * c-file-style: "Stroustrup"
366 * indent-tabs-mode: nil
368 * vim: shiftwidth=4 tabstop=8 expandtab