cql2pqf allows RPN to Solr conversion
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 10 Apr 2014 09:49:33 +0000 (11:49 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 10 Apr 2014 09:49:33 +0000 (11:49 +0200)
util/cql2pqf.c

index eec4767..0baa175 100644 (file)
 #include <stdio.h>
 
 #include <yaz/rpn2cql.h>
+#include <yaz/rpn2solr.h>
 #include <yaz/pquery.h>
 #include <yaz/options.h>
 
 static void usage(void)
 {
-    fprintf(stderr, "usage\n cql2pqf [-n <n>] [-r] [-S] <properties> "
+    fprintf(stderr, "usage\n cql2pqf [-n <n>] [-r] [-s] [-S] <properties> "
             "[<query>]\n");
+    fprintf(stderr, " -r  reverse conversion (RPN to Solr/CQL)\n");
+    fprintf(stderr, " -s  Solr instead of CQL\n");
+    fprintf(stderr, " -S  strict CQL 2.0\n");
     exit(1);
 }
 
@@ -27,13 +31,14 @@ int main(int argc, char **argv)
     char *query = 0;
     char *fname = 0;
     int reverse = 0;
+    int solr = 0;
     int verbose = 0;
     int do_strict = 0;
 
     int ret;
     char *arg;
 
-    while ((ret = options("n:rSv", argv, argc, &arg)) != -2)
+    while ((ret = options("n:rsSv", argv, argc, &arg)) != -2)
     {
         switch (ret)
         {
@@ -52,6 +57,9 @@ int main(int argc, char **argv)
         case 'S':
             do_strict = 1;
             break;
+        case 's':
+            solr = 1;
+            break;
         case 'v':
             verbose = 1;
             break;
@@ -59,17 +67,17 @@ int main(int argc, char **argv)
             usage();
         }
     }
-    if (!fname)
-        usage();
-    if (!strcmp(fname, "-"))
-        ct = cql_transform_create();
-    else
-        ct = cql_transform_open_fname(fname);
-    if (!ct)
+    if (fname)
     {
-        fprintf(stderr, "failed to read properties %s\n", fname);
-        exit(1);
+        ct = cql_transform_open_fname(fname);
+        if (!ct)
+        {
+            fprintf(stderr, "failed to read properties %s\n", fname);
+            exit(1);
+        }
     }
+    else
+        ct = cql_transform_create();
 
     if (reverse)
     {
@@ -78,7 +86,11 @@ int main(int argc, char **argv)
         if (!query)
         {
             if (fgets(buf, sizeof buf, stdin))
+            {
+                if (*buf && buf[strlen(buf)-1] == '\n')
+                    buf[strlen(buf)-1] = '\0';
                 query = buf;
+            }
         }
         if (query)
         {
@@ -91,9 +103,13 @@ int main(int argc, char **argv)
             }
             else
             {
-                int ret = cql_transform_rpn2cql_stream(ct, cql_fputs,
+                int ret = 0;
+                if (solr)
+                    ret = solr_transform_rpn2solr_stream(ct, cql_fputs,
+                                                         stdout, rpn);
+                else
+                    ret = cql_transform_rpn2cql_stream(ct, cql_fputs,
                                                        stdout, rpn);
-
                 if (ret)
                 {
                     const char *addinfo;
@@ -125,6 +141,10 @@ int main(int argc, char **argv)
 
         if (r)
             fprintf(stderr, "Syntax error\n");
+        else if (solr)
+        {
+            printf("CQL to Solr not supported (supported is reverse -r)\n");
+        }
         else
         {
             r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);