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;
44 static int generate(ZOOM_query s)
50 wrbuf_rewind(s->full_query);
51 wrbuf_puts(s->full_query, s->query_string);
52 odr_reset(s->odr_query);
54 switch (s->query_type)
56 case Z_Query_type_1: /* RPN */
58 (s->sort_strategy == SORT_STRATEGY_TYPE7 ||
59 s->sort_strategy == SORT_STRATEGY_EMBED))
61 int r = yaz_sort_spec_to_type7(s->sort_spec, s->full_query);
65 s->z_query = (Z_Query *) odr_malloc(s->odr_query,
67 s->z_query->which = Z_Query_type_1;
68 s->z_query->u.type_1 =
69 p_query_rpn(s->odr_query, wrbuf_cstr(s->full_query));
70 if (!s->z_query->u.type_1)
76 case Z_Query_type_104: /* CQL */
78 (s->sort_strategy == SORT_STRATEGY_CQL ||
79 s->sort_strategy == SORT_STRATEGY_EMBED))
81 int r = yaz_sort_spec_to_cql(s->sort_spec, s->full_query);
85 ext = (Z_External *) odr_malloc(s->odr_query, sizeof(*ext));
86 ext->direct_reference = odr_oiddup(s->odr_query,
87 yaz_oid_userinfo_cql);
88 ext->indirect_reference = 0;
90 ext->which = Z_External_CQL;
91 ext->u.cql = odr_strdup(s->odr_query, wrbuf_cstr(s->full_query));
93 s->z_query = (Z_Query *) odr_malloc(s->odr_query, sizeof(*s->z_query));
94 s->z_query->which = Z_Query_type_104;
95 s->z_query->u.type_104 = ext;
103 Z_Query *ZOOM_query_get_Z_Query(ZOOM_query s)
108 Z_SortKeySpecList *ZOOM_query_get_sortspec(ZOOM_query s)
110 return s->sort_strategy == SORT_STRATEGY_Z3950 ? s->sort_spec : 0;
113 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
115 WRBUF wrbuf = (WRBUF) client_data;
116 wrbuf_puts(wrbuf, buf);
119 const char *ZOOM_query_get_query_string(ZOOM_query s)
121 return wrbuf_cstr(s->full_query);
125 * Returns an xmalloc()d string containing RPN that corresponds to the
126 * CQL passed in. On error, sets the Connection object's error state
127 * and returns a null pointer.
128 * ### We could cache CQL parser and/or transformer in Connection.
130 static char *cql2pqf(ZOOM_connection c, const char *cql)
135 cql_transform_t trans;
138 parser = cql_parser_create();
139 if ((error = cql_parser_string(parser, cql)) != 0) {
140 cql_parser_destroy(parser);
141 ZOOM_set_error(c, ZOOM_ERROR_CQL_PARSE, cql);
145 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
148 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
150 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
153 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
154 cqlfile, strerror(errno));
155 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
159 WRBUF wrbuf_result = wrbuf_alloc();
160 error = cql_transform(trans, cql_parser_result(parser),
161 cql2pqf_wrbuf_puts, wrbuf_result);
165 error = cql_transform_error(trans, &addinfo);
166 sprintf(buf, "%.200s (addinfo=%.200s)",
167 cql_strerror(error), addinfo);
168 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
172 result = xstrdup(wrbuf_cstr(wrbuf_result));
174 cql_transform_close(trans);
175 wrbuf_destroy(wrbuf_result);
177 cql_parser_destroy(parser);
183 ZOOM_query_create(void)
185 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
190 s->odr_query = odr_createmem(ODR_ENCODE);
191 s->odr_sort_spec = odr_createmem(ODR_ENCODE);
193 s->full_query = wrbuf_alloc();
194 s->sort_strategy = SORT_STRATEGY_Z3950;
199 ZOOM_query_destroy(ZOOM_query s)
205 if (s->refcount == 0)
207 odr_destroy(s->odr_query);
208 odr_destroy(s->odr_sort_spec);
209 xfree(s->query_string);
210 wrbuf_destroy(s->full_query);
216 ZOOM_query_addref(ZOOM_query s)
223 ZOOM_query_prefix(ZOOM_query s, const char *str)
225 xfree(s->query_string);
226 s->query_string = xstrdup(str);
227 s->query_type = Z_Query_type_1;
232 ZOOM_query_cql(ZOOM_query s, const char *str)
234 xfree(s->query_string);
235 s->query_string = xstrdup(str);
236 s->query_type = Z_Query_type_104;
241 * Translate the CQL string client-side into RPN which is passed to
242 * the server. This is useful for server's that don't themselves
243 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
244 * only as a place to stash diagnostics if compilation fails; if this
245 * information is not needed, a null pointer may be used.
248 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
252 ZOOM_connection freeme = 0;
255 conn = freeme = ZOOM_connection_create(0);
257 rpn = cql2pqf(conn, str);
259 ZOOM_connection_destroy(freeme);
263 ret = ZOOM_query_prefix(s, rpn);
269 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
270 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
271 * to the server, as the YAZ GFS doesn't know how to handle this.
274 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
275 int *ccl_error, const char **error_string,
279 struct ccl_rpn_node *rpn;
280 CCL_bibset bibset = ccl_qual_mk();
283 ccl_qual_buf(bibset, config);
285 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
288 *error_string = ccl_err_msg(*ccl_error);
293 WRBUF wr = wrbuf_alloc();
296 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
299 ccl_qual_rm(&bibset);
304 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
306 return ZOOM_query_sortby2(s, "z3950", criteria);
310 ZOOM_query_sortby2(ZOOM_query s, const char *strategy, const char *criteria)
312 if (!strcmp(strategy, "z3950"))
314 s->sort_strategy = SORT_STRATEGY_Z3950;
316 else if (!strcmp(strategy, "type7"))
318 s->sort_strategy = SORT_STRATEGY_TYPE7;
320 else if (!strcmp(strategy, "cql"))
322 s->sort_strategy = SORT_STRATEGY_CQL;
324 else if (!strcmp(strategy, "sru11"))
326 s->sort_strategy = SORT_STRATEGY_SRU11;
328 else if (!strcmp(strategy, "embed"))
330 s->sort_strategy = SORT_STRATEGY_EMBED;
335 odr_reset(s->odr_sort_spec);
336 s->sort_spec = yaz_sort_spec(s->odr_sort_spec, criteria);
345 * c-file-style: "Stroustrup"
346 * indent-tabs-mode: nil
348 * vim: shiftwidth=4 tabstop=8 expandtab