def __check_template(self, string: str) -> str: """ Compares given query with known templates. """ str_0 = str(string).strip() if len(str_0) == 0: return "Empty" # Fix keywords to upper str_0 = StringUtil.replace_with_uppercase(str_0, 'select') str_0 = StringUtil.replace_with_uppercase(str_0, 'where') str_0 = StringUtil.replace_with_uppercase(str_0, 'from') str_0 = StringUtil.replace_with_uppercase(str_0, 'in') str_0 = StringUtil.replace_with_uppercase(str_0, 'count') cnt_where = str_0.count("WHERE") cnt_from = str_0.count("FROM") cnt_select = str_0.count("SELECT") cnt_in = str_0.count("IN") cnt_count = str_0.count("COUNT") # If it is a select if not str_0.startswith("SELECT "): return "NotASelect" # It is a select if cnt_count == 1 and cnt_select == 1 and cnt_from == 1 and cnt_where == 1: return "S2" elif cnt_select == 2 and cnt_from == 2 and cnt_where == 2 and cnt_in == 1: return "S3" elif cnt_select == 1 and cnt_from == 1 and cnt_where == 1: return "S4" if cnt_in == 1 else "S1" return "UnknownSelect"
def test_has_content_with_something(self): # Assure tested_value = 'something' # Act result = StringUtil.has_content(tested_value) # Assert self.assertTrue(result)
def test_is_none_with_none(self): # Assure tested_value = None # Act result = StringUtil.is_none(tested_value) # Assert self.assertTrue(result)
def test_has_content_with_whitespaces(self): # Assure tested_value = ' ' # Act result = StringUtil.has_content(tested_value) # Assert self.assertFalse(result)
def test_has_content_with_none(self): # Assure tested_value = None # Act result = StringUtil.has_content(tested_value) # Assert self.assertFalse(result)
def test_is_none_with_something(self): # Assure tested_value = 'something' # Act result = StringUtil.is_none(tested_value) # Assert self.assertFalse(result)
def test_is_none_with_whitespaces(self): # Assure tested_value = ' ' # Act result = StringUtil.is_none(tested_value) # Assert self.assertTrue(result)
def is_valid_json(params: str) -> bool: """ Checks, if given string is a valid JSON. """ if StringUtil.is_none(params) or params.strip() == '{}': return False try: json_object = json.loads(params) except ValueError as e: return False return True
def test_nvl_with_empty(self): # Assure tested_value = '' tested_replacer = 'another' # Act result = StringUtil.nvl(tested_value, tested_replacer) # Assert self.assertEqual(tested_replacer, result)
def test_nvl_with_something(self): # Assure tested_value = 'something' tested_replacer = 'another' # Act result = StringUtil.nvl(tested_value, tested_replacer) # Assert self.assertNotEqual(tested_replacer, result)
def test_replace_with_lowercase_with_other(self): # Assure tested_value = 'This cOdE is funny.' tested_to_replace = 'CoDe' expected = 'This code is funny.' # Act result = StringUtil.replace_with_lowercase(tested_value, tested_to_replace) # Assert self.assertEqual(expected, result)
def test_replace_with_uppercase_with_upper(self): # Assure tested_value = 'This CODE is funny.' tested_to_replace = 'CODE' expected = 'This CODE is funny.' # Act result = StringUtil.replace_with_uppercase(tested_value, tested_to_replace) # Assert self.assertEqual(expected, result)
def test_replace_case_insensitive_with_no_replacer(self): # Assure tested_value = 'This cOdE is funny.' tested_to_replace = 'CoDe' expected = 'This is funny.' # Act result = StringUtil.replace_case_insensitive(tested_value, tested_to_replace) # Assert self.assertEqual(expected, result)
def test_replace_case_insensitive_with_other(self): # Assure tested_value = 'This cOdE is funny.' tested_to_replace = 'CoDe' tested_replacer = 'JOKE' # Act result = StringUtil.replace_case_insensitive(tested_value, tested_to_replace, tested_replacer) # Assert self.assertTrue(tested_replacer in result)
def conv_json_to_where(params: str) -> str: """ Converts JSON to SQL SELECT's WHERE clause. """ if params is not None and len(params.strip()) == 0: return "1 = 1" if not nns_converters.is_valid_json(params): return None result = '' parsed = json.loads(params) for key in parsed: print(key, ":", parsed[key]) if StringUtil.has_content(result): result += ' AND ' if str(parsed[key]).isnumeric(): result += key + ' = ' + str(parsed[key]) else: result += key + ' LIKE \'' + parsed[key] + '\'' return result