+ * Admin commands
+ */
+
+static int cmd_init ( char *args[], WRBUF outbuff)
+{
+ zebra_init(zh);
+ return 0; /* ok */
+}
+
+static int cmd_select_database ( char *args[], WRBUF outbuff)
+{
+ char *db=defarg(args[1],DEFAULTDATABASE);
+ wrbuf_puts(outbuff,"Selecting database 'Default'\n");
+ return zebra_select_database(zh, db);
+}
+
+static int cmd_create_database( char *args[], WRBUF outbuff)
+{
+ char *db=defarg(args[1],DEFAULTDATABASE);
+ wrbuf_printf(outbuff,"Creating database '%s'\n",db);
+
+ return zebra_create_database(zh, db);
+}
+
+static int cmd_drop_database( char *args[], WRBUF outbuff)
+{
+ char *db=args[1];
+ if (!db)
+ db="Default";
+ wrbuf_printf(outbuff,"Dropping database '%s'\n",db);
+ return zebra_drop_database(zh, db);
+}
+
+static int cmd_begin_trans( char *args[], WRBUF outbuff)
+{
+ int rw=0;
+ if (args[1] && ( (args[1][0]=='1') || (args[1][0]=='w') ))
+ rw=1;
+ return zebra_begin_trans(zh,rw);
+}
+
+static int cmd_end_trans( char *args[], WRBUF outbuff)
+{
+ return zebra_end_trans(zh);
+}
+/*************************************
+ * Inserting and deleting
+ */
+
+static int cmd_record_insert( char *args[], WRBUF outbuff)
+{
+ int sysno=0;
+ int rc;
+ char *rec=restargs(args,1);
+
+ rc=zebra_record_insert(zh,rec, strlen(rec), &sysno);
+ if (0==rc)
+ {
+ wrbuf_printf(outbuff,"ok sysno=%d\n",sysno);
+ }
+ return rc;
+}
+
+
+static int cmd_exchange_record( char *args[], WRBUF outbuff)
+{
+ char *base=args[1];
+ char *id = args[2];
+ char *action = args[3];
+ int rc;
+ char *rec=restargs(args,4);
+ if (!(base && id && action && args[4] ))
+ {
+ wrbuf_puts(outbuff,"Missing arguments!\n");
+ onecommand("help exchange_record", outbuff, "");
+ return -90;
+ }
+ rc=zebra_admin_exchange_record(zh, base, rec, strlen(rec),
+ id, strlen(id), atoi(action));
+ return rc;
+}
+
+/**********************************
+ * Searching and retrieving
+ */
+
+static int cmd_search_pqf( char *args[], WRBUF outbuff)
+{
+ int hits=0;
+ char *set=args[1];
+ char *qry=restargs(args,2);
+ int rc;
+ rc=zebra_search_PQF(zh, qry, set, &hits);
+ if (0==rc)
+ wrbuf_printf(outbuff,"%d hits found\n",hits);
+ return rc;
+}
+
+static int cmd_find( char *args[], WRBUF outbuff)
+{
+ char *setname=DEFAULTRESULTSET;
+ int rc;
+ int hits=0;
+ WRBUF qry=wrbuf_alloc();
+ if (0==strstr(args[0],"@attr"))
+ wrbuf_puts(qry, "@attr 1=/ ");
+ wrbuf_puts(qry,restargs(args,1));
+ if (!zh)
+ onecommand("quickstart", outbuff, "");
+ wrbuf_printf(outbuff, "find %s\n",wrbuf_buf(qry));
+ rc=zebra_search_PQF(zh, wrbuf_buf(qry), setname, &hits);
+ if (0==rc)
+ {
+ wrbuf_printf(outbuff,"%d hits found\n",hits);
+ nextrecno=1;
+ }
+ wrbuf_free(qry,1);
+ return rc;
+}
+
+static int cmd_show( char *args[], WRBUF outbuff)
+{
+ int start=defargint(args[1], nextrecno);
+ int nrecs=defargint(args[2],1);
+ char *setname=defarg(args[3],DEFAULTRESULTSET);
+ int rc=0;
+ ZebraRetrievalRecord *recs;
+ ODR odr;
+ Z_RecordComposition *pcomp=0;
+ int i;
+ oid_value format;
+
+ odr=odr_createmem(ODR_ENCODE);
+ recs= odr_malloc(odr,sizeof(ZebraRetrievalRecord)*nrecs);
+ rc =z_RecordComposition(odr, &pcomp, 0,"recordComposition");
+ format=oid_getvalbyname ("xml"); /*FIXME - let the user specify*/
+ for (i=0;i<nrecs;i++)
+ recs[i].position=start+i;
+
+ rc = zebra_records_retrieve (zh, odr, setname,
+ pcomp, format, nrecs,recs);
+ if (0==rc)
+ {
+ for (i=0;i<nrecs;i++)
+ {
+ printf("Err %d: %d\n",i,recs[i].errCode);
+ if (recs[i].buf)
+ {
+ wrbuf_printf(outbuff,"Record %d\n", recs[i].position);
+ wrbuf_write(outbuff, recs[i].buf, recs[i].len);
+ wrbuf_puts(outbuff, "\n");
+ } else
+ wrbuf_printf(outbuff,"NO Record %d\n", recs[i].position);
+ }
+ nextrecno=start+nrecs+1;
+ }
+ odr_destroy(odr);
+ return rc;
+}
+/**************************************)