1 //#include "zebraapi.h"
6 #include <yaz/yaz-util.h>
10 #include <yaz/pquery.h>
12 #include "zebra_perl.h"
19 yaz_log_init_prefix ("ZebraPerl");
20 yaz_log (LOG_LOG, "Zebra API initialized");
25 yaz_log (LOG_LOG, "Zebra API destroyed");
28 /* Logging facilities from yaz */
29 void logLevel (int level) {
30 yaz_log_init_level(level);
33 void logFile (const char *fname) {
34 yaz_log_init_file(fname);
37 void logMsg (int level, const char *message) {
38 logf(level, "%s", message);
41 /* debug tool for data1... maybe should be moved to data1.
42 perl can't really pass filehandles */
43 void data1_print_tree(data1_handle dh, data1_node *n) {
44 data1_pr_tree(dh, n, stdout);
47 /* ---------------------------------------------------------------------------
49 2 phase retrieval - I didn't manage to return array of blessed references
50 to wrapped structures... it's feasible, but I'll need some time
54 void record_retrieve(RetrievalObj *ro,
62 RetrievalRecordBuf *buf =
63 (RetrievalRecordBuf *) odr_malloc(stream, sizeof(*buf));
65 res->errCode = ro->records[i].errCode;
66 if (ro->records[i].errString) {
67 res->errString = odr_strdup(stream, ro->records[i].errString);
71 res->position = ro->records[i].position;
72 res->base = ro->records[i].base;
73 res->format = (char *)
74 yaz_z3950_oid_value_to_str(ro->records[i].format, CLASS_RECSYN);
76 res->buf->len = ro->records[i].len;
77 res->buf->buf = ro->records[i].buf;
78 res->score = ro->records[i].score;
79 res->sysno = ro->records[i].sysno;
85 /* most of the code here was copied from yaz-client */
86 void records_retrieve(ZebraHandle zh,
96 static enum oid_value recordsyntax = VAL_SUTRS;
97 static enum oid_value schema = VAL_NONE;
98 static Z_ElementSetNames *elementSetNames = 0;
99 static Z_RecordComposition compo;
100 static Z_ElementSetNames esn;
101 static char what[100];
107 if (from < 1) from = 1;
108 if (from > to) to = from;
109 res->noOfRecords = to - from + 1;
111 res->records = odr_malloc (stream,
112 sizeof(*res->records) * (res->noOfRecords));
114 for (i = 0; i<res->noOfRecords; i++) res->records[i].position = from+i;
116 if (!a_eset || !*a_eset) {
119 strcpy(what, a_eset);
120 esn.which = Z_ElementSetNames_generic;
121 esn.u.generic = what;
122 elementSetNames = &esn;
125 if (!a_schema || !*a_schema) {
128 schema = oid_getvalbyname (a_schema);
129 if (schema == VAL_NONE) {
130 logf(LOG_WARN,"unknown schema '%s'",a_schema);
135 if (!a_format || !*a_format) {
136 recordsyntax = VAL_SUTRS;
138 recordsyntax = oid_getvalbyname (a_format);
139 if (recordsyntax == VAL_NONE) {
140 logf(LOG_WARN,"unknown record syntax '%s', using SUTRS",a_schema);
141 recordsyntax = VAL_SUTRS;
145 if (schema != VAL_NONE) {
148 prefschema.proto = PROTO_Z3950;
149 prefschema.oclass = CLASS_SCHEMA;
150 prefschema.value = schema;
152 compo.which = Z_RecordComp_complex;
153 compo.u.complex = (Z_CompSpec *)
154 odr_malloc(stream, sizeof(*compo.u.complex));
155 compo.u.complex->selectAlternativeSyntax = (bool_t *)
156 odr_malloc(stream, sizeof(bool_t));
157 *compo.u.complex->selectAlternativeSyntax = 0;
159 compo.u.complex->generic = (Z_Specification *)
160 odr_malloc(stream, sizeof(*compo.u.complex->generic));
161 compo.u.complex->generic->which = Z_Schema_oid;
162 compo.u.complex->generic->schema.oid = (Odr_oid *)
163 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
164 if (!compo.u.complex->generic->schema.oid)
166 /* OID wasn't a schema! Try record syntax instead. */
167 prefschema.oclass = CLASS_RECSYN;
168 compo.u.complex->generic->schema.oid = (Odr_oid *)
169 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
171 if (!elementSetNames)
172 compo.u.complex->generic->elementSpec = 0;
175 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
176 odr_malloc(stream, sizeof(Z_ElementSpec));
177 compo.u.complex->generic->elementSpec->which =
178 Z_ElementSpec_elementSetName;
179 compo.u.complex->generic->elementSpec->u.elementSetName =
180 elementSetNames->u.generic;
182 compo.u.complex->num_dbSpecific = 0;
183 compo.u.complex->dbSpecific = 0;
184 compo.u.complex->num_recordSyntax = 0;
185 compo.u.complex->recordSyntax = 0;
187 else if (elementSetNames) {
188 compo.which = Z_RecordComp_simple;
189 compo.u.simple = elementSetNames;
192 if (compo.which == -1) {
193 api_records_retrieve (zh, stream, setname,
196 res->noOfRecords, res->records);
198 api_records_retrieve (zh, stream, setname,
201 res->noOfRecords, res->records);
206 int zebra_cql2pqf (cql_transform_t ct,
207 const char *query, char *res, int len) {
210 const char *addinfo = "";
211 CQL_parser cp = cql_parser_create();
213 if (status = cql_parser_string(cp, query)) {
214 cql_parser_destroy(cp);
218 if (cql_transform_buf(ct, cql_parser_result(cp), res, len)) {
219 status = cql_transform_error(ct, &addinfo);
220 logf (LOG_WARN,"Transform error %d %s\n", status, addinfo ? addinfo : "");
221 cql_parser_destroy(cp);
225 cql_parser_destroy(cp);
229 void zebra_scan_PQF (ZebraHandle zh,
232 const char *pqf_query)
234 Z_AttributesPlusTerm *zapt;
237 oid_value attributeset;
238 ZebraScanEntry *entries;
242 "scan req: pos:%d, num:%d, partial:%d",
243 so->position, so->num_entries, so->is_partial);
245 zapt = p_query_scan (stream, PROTO_Z3950, &attrsetid, pqf_query);
247 oidname = yaz_z3950oid_to_str (attrsetid, &class);
248 logf (LOG_DEBUG, "Attributreset: %s", oidname);
249 attributeset = oid_getvalbyname(oidname);
252 logf (LOG_WARN, "bad query %s\n", pqf_query);
257 so->entries = (scanEntry *)
258 odr_malloc (stream, sizeof(so->entries) * (so->num_entries));
261 zebra_scan (zh, stream, zapt, attributeset,
262 &so->position, &so->num_entries,
263 (ZebraScanEntry **) &so->entries, &so->is_partial);
266 "scan res: pos:%d, num:%d, partial:%d",
267 so->position, so->num_entries, so->is_partial);
270 scanEntry *getScanEntry(ScanObj *so, int pos) {
271 return (&so->entries[pos-1]);