[Formed-commits] r426 - in trunk: . formed/formed/plugins/export

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 24 17:58:10 CET 2010


Author: bh
Date: 2010-11-24 17:58:10 +0100 (Wed, 24 Nov 2010)
New Revision: 426

Modified:
   trunk/ChangeLog
   trunk/formed/formed/plugins/export/rules_sh.py
Log:
* formed/formed/plugins/export/rules_sh.py (binary_operator_map):
New.  Map binary formed expr operators to their SQL counterparts.
(sql_OperatorBinary): Use binary_operator_map instead of a cascade
of if-statements with lots of code duplication.  Also, put
parentheses around the resulting SQL expression to make sure the
expression is correctly.  This fixes a problem with WASKO tagging
rules, where the a FormEd expression of the form like "2 1 1980
date 1 1 1980 date - 93 *" would be incorrectly converted to the
SQL expression "93 * '1980-01-02'::date - '1980-01-01'::date"


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2010-11-24 16:33:14 UTC (rev 425)
+++ trunk/ChangeLog	2010-11-24 16:58:10 UTC (rev 426)
@@ -1,5 +1,17 @@
 2010-11-24  Bernhard Herzog  <bh at intevation.de>
 
+	* formed/formed/plugins/export/rules_sh.py (binary_operator_map):
+	New.  Map binary formed expr operators to their SQL counterparts.
+	(sql_OperatorBinary): Use binary_operator_map instead of a cascade
+	of if-statements with lots of code duplication.  Also, put
+	parentheses around the resulting SQL expression to make sure the
+	expression is correctly.  This fixes a problem with WASKO tagging
+	rules, where the a FormEd expression of the form like "2 1 1980
+	date 1 1 1980 date - 93 *" would be incorrectly converted to the
+	SQL expression "93 * '1980-01-02'::date - '1980-01-01'::date"
+
+2010-11-24  Bernhard Herzog  <bh at intevation.de>
+
 	* formed/formed/plugins/export/rules_sh.py: Fix formatting.
 
 2010-11-15  Torsten Irlaender <torsten at intevation.de>

Modified: trunk/formed/formed/plugins/export/rules_sh.py
===================================================================
--- trunk/formed/formed/plugins/export/rules_sh.py	2010-11-24 16:33:14 UTC (rev 425)
+++ trunk/formed/formed/plugins/export/rules_sh.py	2010-11-24 16:58:10 UTC (rev 426)
@@ -57,54 +57,31 @@
     out.append(")")
     return " ".join(out)
 
+binary_operator_map = {
+    "GT": ">",
+    "LT": "<",
+    "GE": ">=",
+    "LE": "<=",
+    "EQ": "=",
+    "NE": "!=",
+    "ADD": "+",
+    "MINUS": "-",
+    "MUL": "*",
+    "DAYS": "-",
+    }
+
 def sql_OperatorBinary(node, document):
-    out = []
-    operator = ""
-    if node.operator.__name__ == "GT":
-        operator = ">"
-        out.append("%s %s %s" % (recursive_parse(node.a, document),
-                                 operator, recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "LT":
-        operator = "<"
-        out.append("%s %s %s" % (recursive_parse(node.a, document),
-                                 operator, recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "LE":
-        operator = "<="
-        out.append("%s %s %s" % (recursive_parse(node.a, document), operator,
-                                 recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "EQ":
-        operator = "="
-        out.append("%s %s %s" % (recursive_parse(node.a, document), operator,
-                                 recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "NE":
-        operator = "!="
-        out.append("%s %s %s" % (recursive_parse(node.a, document), operator,
-                                 recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "GE":
-        operator = ">="
-        out.append("%s %s %s" % (recursive_parse(node.a, document), operator,
-                                 recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "DAYS":
-        operator = "-"
-        out.append("abs(%s %s %s)" % (recursive_parse(node.a, document),
-                                      operator, recursive_parse(node.b,
-                                                                document)))
-    elif node.operator.__name__ == "ADD":
-        operator = "+"
-        out.append("%s %s %s" % (recursive_parse(node.a, document), operator,
-                                 recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "MINUS":
-        operator = "-"
-        out.append("%s %s %s" % (recursive_parse(node.a, document), operator,
-                                 recursive_parse(node.b, document)))
-    elif node.operator.__name__ == "MUL":
-        operator = "*"
-        out.append("%s %s %s" % (recursive_parse(node.a, document), operator,
-                                 recursive_parse(node.b, document)))
-    else:
-        raise Exception('Can not parse %s' % node)
-    return " ".join(out)
+    operator = binary_operator_map.get(node.operator.__name__)
+    if operator is None:
+        raise Exception('Cannot convert operator %s' % node)
 
+    template = "(%s %s %s)"
+    if node.operator.__name__ == "DAYS":
+        template = "abs(%s %s %s)"
+
+    return template % (recursive_parse(node.a, document),
+                       operator, recursive_parse(node.b, document))
+
 def sql_OperatorUnary(node, document):
     out = []
     if node.operator.__name__ == "NOT":



More information about the Formed-commits mailing list