-ZOOM_API(ZOOM_query)
- ZOOM_query_create(void)
-{
- ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
-
- s->refcount = 1;
- s->z_query = 0;
- s->sort_spec = 0;
- s->odr = odr_createmem(ODR_ENCODE);
- s->query_string = 0;
-
- return s;
-}
-
-ZOOM_API(void)
- ZOOM_query_destroy(ZOOM_query s)
-{
- if (!s)
- return;
-
- (s->refcount)--;
- if (s->refcount == 0)
- {
- odr_destroy(s->odr);
- xfree(s);
- }
-}
-
-ZOOM_API(int)
- ZOOM_query_prefix(ZOOM_query s, const char *str)
-{
- s->query_string = odr_strdup(s->odr, str);
- s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
- s->z_query->which = Z_Query_type_1;
- s->z_query->u.type_1 = p_query_rpn(s->odr, str);
- if (!s->z_query->u.type_1)
- {
- s->z_query = 0;
- return -1;
- }
- return 0;
-}
-
-ZOOM_API(int)
- ZOOM_query_cql(ZOOM_query s, const char *str)
-{
- Z_External *ext;
-
- s->query_string = odr_strdup(s->odr, str);
-
- ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
- ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
- ext->indirect_reference = 0;
- ext->descriptor = 0;
- ext->which = Z_External_CQL;
- ext->u.cql = s->query_string;
-
- s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
- s->z_query->which = Z_Query_type_104;
- s->z_query->u.type_104 = ext;
-
- return 0;
-}
-
-/*
- * Translate the CQL string client-side into RPN which is passed to
- * the server. This is useful for server's that don't themselves
- * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
- * only as a place to stash diagnostics if compilation fails; if this
- * information is not needed, a null pointer may be used.
- */
-ZOOM_API(int)
- ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
-{
- char *rpn;
- int ret;
- ZOOM_connection freeme = 0;
-
- if (conn == 0)
- conn = freeme = ZOOM_connection_create(0);
-
- rpn = cql2pqf(conn, str);
- if (freeme != 0)
- ZOOM_connection_destroy(freeme);
- if (rpn == 0)
- return -1;
-
- ret = ZOOM_query_prefix(s, rpn);
- xfree(rpn);
- return ret;
-}
-
-/*
- * Analogous in every way to ZOOM_query_cql2rpn(), except that there
- * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
- * to the server, as the YAZ GFS doesn't know how to handle this.
- */
-ZOOM_API(int)
- ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
- int *ccl_error, const char **error_string,
- int *error_pos)
-{
- int ret;
- struct ccl_rpn_node *rpn;
- CCL_bibset bibset = ccl_qual_mk();
-
- if (config)
- ccl_qual_buf(bibset, config);
-
- rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
- if (!rpn)
- {
- *error_string = ccl_err_msg(*ccl_error);
- ret = -1;
- }
- else
- {
- WRBUF wr = wrbuf_alloc();
- ccl_pquery(wr, rpn);
- ccl_rpn_delete(rpn);
- ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
- wrbuf_destroy(wr);
- }
- ccl_qual_rm(&bibset);
- return ret;
-}
-
-ZOOM_API(int)
- ZOOM_query_sortby(ZOOM_query s, const char *criteria)
-{
- s->sort_spec = yaz_sort_spec(s->odr, criteria);
- if (!s->sort_spec)
- return -1;
- return 0;
-}
-