Commit 7ca2d604 authored by Pierre Dittgen's avatar Pierre Dittgen

Harden code to handle non string values

parent 8092a314
......@@ -101,20 +101,40 @@ class CompareColumnsValue(object):
.format(self.column, value1, OP_LABELS[self.op], self.column2, value2),
{'column1': self.column, 'value1': value1, 'column2': self.column2, 'value2': value2, 'op': OP_LABELS[self.op]})
@staticmethod
def is_a_number(value):
"""Return True if value is an int, a float or a string representation of a number."""
if type(value) in (int, float):
return True
if not isinstance(value, str):
return False
if value.isnumeric():
return True
try:
float(value)
return True
except ValueError:
return False
@staticmethod
def compute_comparison_str(value1, op, value2):
""" Computes comparison_str """
# number vs number
if value1.isnumeric() and value2.isnumeric():
if CompareColumnsValue.is_a_number(value1) and CompareColumnsValue.is_a_number(value2):
return '{} {} {}'.format(value1, op, value2)
# string vs string
if not value1.isnumeric() and not value2.isnumeric():
if isinstance(value1, str) and isinstance(value2, str):
n_value1 = value1.replace('"', '\\"')
n_value2 = value2.replace('"', '\\"')
return '"{}" {} "{}"'.format(n_value1, op, n_value2)
# thing vs thing, compare string repr
if type(value1) == type(value2):
return f"'{value1}' {op} '{value2}'"
# potato vs cabbage?
return None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment