9 #include <yaz/yaz-util.h>
10 #include <yaz/proto.h>
13 #include <yaz/pquery.h>
15 #include "zebra_perl.h"
22 yaz_log_init_prefix ("ZebraPerl");
23 yaz_log (LOG_LOG, "Zebra API initialized");
28 yaz_log (LOG_LOG, "Zebra API destroyed");
31 /* Logging facilities from yaz */
32 void logLevel (int level) {
33 yaz_log_init_level(level);
36 void logFile (const char *fname) {
37 yaz_log_init_file(fname);
40 void logMsg (int level, const char *message) {
41 logf(level, "%s", message);
44 /* debug tool for data1... maybe should be moved to data1.
45 perl can't really pass filehandles */
46 void data1_print_tree(data1_handle dh, data1_node *n) {
47 data1_pr_tree(dh, n, stdout);
50 /* ---------------------------------------------------------------------------
52 2 phase retrieval - I didn't manage to return array of blessed references
53 to wrapped structures... it's feasible, but I'll need some time
57 void record_retrieve(RetrievalObj *ro,
65 RetrievalRecordBuf *buf =
66 (RetrievalRecordBuf *) odr_malloc(stream, sizeof(*buf));
68 res->errCode = ro->records[i].errCode;
69 if (ro->records[i].errString) {
70 res->errString = odr_strdup(stream, ro->records[i].errString);
74 res->position = ro->records[i].position;
75 res->base = ro->records[i].base;
76 res->format = (char *)
77 yaz_z3950_oid_value_to_str(ro->records[i].format, CLASS_RECSYN);
79 res->buf->len = ro->records[i].len;
80 res->buf->buf = ro->records[i].buf;
81 res->score = ro->records[i].score;
82 res->sysno = ro->records[i].sysno;
88 /* most of the code here was copied from yaz-client */
89 void records_retrieve(ZebraHandle zh,
99 static enum oid_value recordsyntax = VAL_SUTRS;
100 static enum oid_value schema = VAL_NONE;
101 static Z_ElementSetNames *elementSetNames = 0;
102 static Z_RecordComposition compo;
103 static Z_ElementSetNames esn;
104 static char what[100];
110 if (from < 1) from = 1;
111 if (from > to) to = from;
112 res->noOfRecords = to - from + 1;
114 res->records = odr_malloc (stream,
115 sizeof(*res->records) * (res->noOfRecords));
117 for (i = 0; i<res->noOfRecords; i++) res->records[i].position = from+i;
119 if (!a_eset || !*a_eset) {
122 strcpy(what, a_eset);
123 esn.which = Z_ElementSetNames_generic;
124 esn.u.generic = what;
125 elementSetNames = &esn;
128 if (!a_schema || !*a_schema) {
131 schema = oid_getvalbyname (a_schema);
132 if (schema == VAL_NONE) {
133 logf(LOG_WARN,"unknown schema '%s'",a_schema);
138 if (!a_format || !*a_format) {
139 recordsyntax = VAL_SUTRS;
141 recordsyntax = oid_getvalbyname (a_format);
142 if (recordsyntax == VAL_NONE) {
143 logf(LOG_WARN,"unknown record syntax '%s', using SUTRS",a_schema);
144 recordsyntax = VAL_SUTRS;
148 if (schema != VAL_NONE) {
151 prefschema.proto = PROTO_Z3950;
152 prefschema.oclass = CLASS_SCHEMA;
153 prefschema.value = schema;
155 compo.which = Z_RecordComp_complex;
156 compo.u.complex = (Z_CompSpec *)
157 odr_malloc(stream, sizeof(*compo.u.complex));
158 compo.u.complex->selectAlternativeSyntax = (bool_t *)
159 odr_malloc(stream, sizeof(bool_t));
160 *compo.u.complex->selectAlternativeSyntax = 0;
162 compo.u.complex->generic = (Z_Specification *)
163 odr_malloc(stream, sizeof(*compo.u.complex->generic));
164 compo.u.complex->generic->which = Z_Schema_oid;
165 compo.u.complex->generic->schema.oid = (Odr_oid *)
166 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
167 if (!compo.u.complex->generic->schema.oid)
169 /* OID wasn't a schema! Try record syntax instead. */
170 prefschema.oclass = CLASS_RECSYN;
171 compo.u.complex->generic->schema.oid = (Odr_oid *)
172 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
174 if (!elementSetNames)
175 compo.u.complex->generic->elementSpec = 0;
178 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
179 odr_malloc(stream, sizeof(Z_ElementSpec));
180 compo.u.complex->generic->elementSpec->which =
181 Z_ElementSpec_elementSetName;
182 compo.u.complex->generic->elementSpec->u.elementSetName =
183 elementSetNames->u.generic;
185 compo.u.complex->num_dbSpecific = 0;
186 compo.u.complex->dbSpecific = 0;
187 compo.u.complex->num_recordSyntax = 0;
188 compo.u.complex->recordSyntax = 0;
190 else if (elementSetNames) {
191 compo.which = Z_RecordComp_simple;
192 compo.u.simple = elementSetNames;
195 if (compo.which == -1) {
196 api_records_retrieve (zh, stream, setname,
199 res->noOfRecords, res->records);
201 api_records_retrieve (zh, stream, setname,
204 res->noOfRecords, res->records);
209 int zebra_cql2pqf (cql_transform_t ct,
210 const char *query, char *res, int len) {
213 const char *addinfo = "";
214 CQL_parser cp = cql_parser_create();
216 if (status = cql_parser_string(cp, query)) {
217 cql_parser_destroy(cp);
221 if (cql_transform_buf(ct, cql_parser_result(cp), res, len)) {
222 status = cql_transform_error(ct, &addinfo);
223 logf (LOG_WARN,"Transform error %d %s\n", status, addinfo ? addinfo : "");
224 cql_parser_destroy(cp);
228 cql_parser_destroy(cp);
232 void zebra_scan_PQF (ZebraHandle zh,
235 const char *pqf_query)
237 Z_AttributesPlusTerm *zapt;
240 oid_value attributeset;
241 ZebraScanEntry *entries;
245 "scan req: pos:%d, num:%d, partial:%d",
246 so->position, so->num_entries, so->is_partial);
248 zapt = p_query_scan (stream, PROTO_Z3950, &attrsetid, pqf_query);
250 oidname = yaz_z3950oid_to_str (attrsetid, &class);
251 logf (LOG_DEBUG, "Attributreset: %s", oidname);
252 attributeset = oid_getvalbyname(oidname);
255 logf (LOG_WARN, "bad query %s\n", pqf_query);
260 so->entries = (scanEntry *)
261 odr_malloc (stream, sizeof(so->entries) * (so->num_entries));
264 zebra_scan (zh, stream, zapt, attributeset,
265 &so->position, &so->num_entries,
266 (ZebraScanEntry **) &so->entries, &so->is_partial);
269 "scan res: pos:%d, num:%d, partial:%d",
270 so->position, so->num_entries, so->is_partial);
273 scanEntry *getScanEntry(ScanObj *so, int pos) {
274 return (&so->entries[pos-1]);