rpn2cql: less quotes in generated terms
[yaz-moved-to-github.git] / src / rpn2cql.c
index 9fedba6..25ee138 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2011 Index Data
+ * Copyright (C) 1995-2012 Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -69,27 +69,27 @@ static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
                 switch (*relation)
                 {
                     /* Unsure on whether this is the relation attribute constants? */
-                case Z_ProximityOperator_Prox_lessThan: 
+                case Z_ProximityOperator_Prox_lessThan:
                     return "<";
-                case Z_ProximityOperator_Prox_lessThanOrEqual: 
-                    return "<="; 
-                case Z_ProximityOperator_Prox_equal: 
-                    return "="; 
-                case Z_ProximityOperator_Prox_greaterThanOrEqual: 
-                    return ">="; 
-                case Z_ProximityOperator_Prox_greaterThan: 
-                    return ">"; 
-                case Z_ProximityOperator_Prox_notEqual: 
-                    return "<>"; 
-                case 100: 
+                case Z_ProximityOperator_Prox_lessThanOrEqual:
+                    return "<=";
+                case Z_ProximityOperator_Prox_equal:
+                    return "=";
+                case Z_ProximityOperator_Prox_greaterThanOrEqual:
+                    return ">=";
+                case Z_ProximityOperator_Prox_greaterThan:
+                    return ">";
+                case Z_ProximityOperator_Prox_notEqual:
+                    return "<>";
+                case 100:
                     /* phonetic is not supported in CQL */
-                    return 0; 
-                case 101: 
+                    return 0;
+                case 101:
                     /* stem is not supported in CQL */
-                    return 0; 
-                case 102: 
+                    return 0;
+                case 102:
                     /* relevance is supported in CQL, but not implemented yet */
-                    return 0; 
+                    return 0;
                 default:
                     /* Invalid relation */
                     return 0;
@@ -117,7 +117,7 @@ static int rpn2cql_attr(cql_transform_t ct,
         index = lookup_index_from_string_attr(attributes);
 
     /* Attempt to fix bug #2978: Look for a relation attribute */
-    if (!relation) 
+    if (!relation)
         relation = lookup_relation_index_from_attr(attributes);
 
     if (!index)
@@ -194,7 +194,6 @@ static int rpn2cql_simple(cql_transform_t ct,
         size_t lterm = 0;
         Odr_int trunc = lookup_truncation(apt->attributes);
         size_t i;
-        int must_quote = 0;
 
         wrbuf_rewind(w);
         ret = rpn2cql_attr(ct, apt->attributes, w);
@@ -219,12 +218,16 @@ static int rpn2cql_simple(cql_transform_t ct,
 
         if (trunc <= 3 || trunc == 100 || trunc == 102 || trunc == 104)
         {
+            int quote_it = 0;
             for (i = 0 ; i < lterm; i++)
                 if (strchr(" ()=></", sterm[i]))
+                {
+                    quote_it = 1;
                     break;
-            if (i < lterm || lterm == 0)
-                must_quote = 1;
-            if (must_quote)
+                }
+            if (lterm == 0)
+                quote_it = 1;
+            if (quote_it)
                 wrbuf_puts(w, "\"");
             if (trunc == 2 || trunc == 3)
                 wrbuf_puts(w, "*");
@@ -258,7 +261,7 @@ static int rpn2cql_simple(cql_transform_t ct,
             }
             if (trunc == 1 || trunc == 3)
                 wrbuf_puts(w, "*");
-            if (must_quote)
+            if (quote_it)
                 wrbuf_puts(w, "\"");
         }
         else