1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 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>
28 Z_SortKeySpecList *sort_spec;
34 Z_Query *ZOOM_query_get_Z_Query(ZOOM_query s)
40 Z_SortKeySpecList *ZOOM_query_get_sortspec(ZOOM_query s)
45 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
47 WRBUF wrbuf = (WRBUF) client_data;
48 wrbuf_puts(wrbuf, buf);
51 char *ZOOM_query_get_query_string(ZOOM_query s)
53 return s->query_string;
57 * Returns an xmalloc()d string containing RPN that corresponds to the
58 * CQL passed in. On error, sets the Connection object's error state
59 * and returns a null pointer.
60 * ### We could cache CQL parser and/or transformer in Connection.
62 static char *cql2pqf(ZOOM_connection c, const char *cql)
67 cql_transform_t trans;
70 parser = cql_parser_create();
71 if ((error = cql_parser_string(parser, cql)) != 0) {
72 cql_parser_destroy(parser);
73 ZOOM_set_error(c, ZOOM_ERROR_CQL_PARSE, cql);
77 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
80 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
82 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
85 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
86 cqlfile, strerror(errno));
87 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
91 WRBUF wrbuf_result = wrbuf_alloc();
92 error = cql_transform(trans, cql_parser_result(parser),
93 cql2pqf_wrbuf_puts, wrbuf_result);
97 error = cql_transform_error(trans, &addinfo);
98 sprintf(buf, "%.200s (addinfo=%.200s)",
99 cql_strerror(error), addinfo);
100 ZOOM_set_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
104 result = xstrdup(wrbuf_cstr(wrbuf_result));
106 cql_transform_close(trans);
107 wrbuf_destroy(wrbuf_result);
109 cql_parser_destroy(parser);
115 ZOOM_query_create(void)
117 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
122 s->odr = odr_createmem(ODR_ENCODE);
129 ZOOM_query_destroy(ZOOM_query s)
135 if (s->refcount == 0)
143 ZOOM_query_addref(ZOOM_query s)
149 ZOOM_query_prefix(ZOOM_query s, const char *str)
151 s->query_string = odr_strdup(s->odr, str);
152 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
153 s->z_query->which = Z_Query_type_1;
154 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
155 if (!s->z_query->u.type_1)
164 ZOOM_query_cql(ZOOM_query s, const char *str)
168 s->query_string = odr_strdup(s->odr, str);
170 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
171 ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
172 ext->indirect_reference = 0;
174 ext->which = Z_External_CQL;
175 ext->u.cql = s->query_string;
177 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
178 s->z_query->which = Z_Query_type_104;
179 s->z_query->u.type_104 = ext;
185 * Translate the CQL string client-side into RPN which is passed to
186 * the server. This is useful for server's that don't themselves
187 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
188 * only as a place to stash diagnostics if compilation fails; if this
189 * information is not needed, a null pointer may be used.
192 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
196 ZOOM_connection freeme = 0;
199 conn = freeme = ZOOM_connection_create(0);
201 rpn = cql2pqf(conn, str);
203 ZOOM_connection_destroy(freeme);
207 ret = ZOOM_query_prefix(s, rpn);
213 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
214 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
215 * to the server, as the YAZ GFS doesn't know how to handle this.
218 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
219 int *ccl_error, const char **error_string,
223 struct ccl_rpn_node *rpn;
224 CCL_bibset bibset = ccl_qual_mk();
227 ccl_qual_buf(bibset, config);
229 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
232 *error_string = ccl_err_msg(*ccl_error);
237 WRBUF wr = wrbuf_alloc();
240 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
243 ccl_qual_rm(&bibset);
248 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
250 s->sort_spec = yaz_sort_spec(s->odr, criteria);
260 * c-file-style: "Stroustrup"
261 * indent-tabs-mode: nil
263 * vim: shiftwidth=4 tabstop=8 expandtab