Exemplo n.º 1
0
    def test_check_invalid_string_2(self):
        text = "SELECT '\n"
        text += "-- this is not really a comment' AS c;\n"
        text += "SELECT '\n"
        text += "-- neither is this' AS c spam;"

        (success,msg) = pgsanity.check_string(text)
        self.assertFalse(success)
        self.assertEqual('line 4: ERROR: syntax error at or near "spam"', msg)
Exemplo n.º 2
0
    def validate(cls, sql: str, schema: Optional[str],
                 database: Database) -> List[SQLValidationAnnotation]:
        annotations: List[SQLValidationAnnotation] = []
        valid, error = check_string(sql, add_semicolon=True)
        if valid:
            return annotations

        match = re.match(r"^line (\d+): (.*)", error)
        line_number = int(match.group(1)) if match else None
        message = match.group(2) if match else error

        annotations.append(
            SQLValidationAnnotation(
                message=message,
                line_number=line_number,
                start_column=None,
                end_column=None,
            ))

        return annotations
Exemplo n.º 3
0
def print_formatted_current(current_log, next_log, sink, current_sync, next_sync, validate=False):
    if current_log is None or next_log is None:
        return

    if current_log["group"].endswith("LOG"):
        is_param_details = next_log["group"].endswith("DETAIL") and next_log["action"] == "parameters"
        is_conflict = (not current_sync) or (is_param_details and not next_sync)
        if is_param_details and next_sync:
            parsed_args = {}
            temp_sql = next_log["sql"]
            key_args = [int(x.strip('$')) for x in re.findall("\$\d+", current_log["sql"])]
            for idx, key in enumerate(key_args):
                current_arg = "${pos} = ".format(pos=key)
                next_arg = "${pos} = ".format(pos=(key + 1))
                start = "^(?P<start>\\{current_arg})".format(current_arg=current_arg)
                end = ("$" if idx == len(key_args) - 1 else "(?P<end>, \\{next_arg})".format(next_arg=next_arg))
                pattern = "{start}(?P<value>.*){end}".format(start=start, end=end)
                match = re.match(pattern, temp_sql)
                if match:
                    temp_sql = next_arg + re.split(pattern, temp_sql, 1)[-1]
                    parsed_args["$" + str(key)] = match.group("value")

            for key in sorted(key_args, reverse=True):
                arg = "$" + str(key)
                current_log["sql"] = current_log["sql"].replace(arg, parsed_args[arg])

        sql = current_log["sql"]
        sql = sql + ";" if sql[-1] != ";" else sql

        valid_stmt = True

        if validate:
            valid_stmt, _ = pgsanity.check_string(sql)

        if not valid_stmt:
            sql = '-- {}'.format(sql)
            log.warning("Skipping as identified invalid SQL syntax : %s", current_log)

        sql = merge_conflict_wrapper.format(sql) if is_conflict else sql
        print(sql, file=sink)
Exemplo n.º 4
0
 def test_check_invalid_string(self):
     text = "garbage select a from b;"
     msglist = pgsanity.check_string(text)
     self.assertTrue(msglist)
     self.assertEqual(
         ['line 1: ERROR: unrecognized data type name "garbage"'], msglist)
Exemplo n.º 5
0
 def test_check_valid_string(self):
     text = "select a from b;"
     msglist = pgsanity.check_string(text)
     self.assertFalse(msglist)
Exemplo n.º 6
0
 def test_check_invalid_string(self):
     text = "garbage select a from b;"
     (success, msg) = pgsanity.check_string(text)
     self.assertFalse(success)
     self.assertEqual(
         'line 1: ERROR: unrecognized data type name "garbage"', msg)
Exemplo n.º 7
0
 def test_check_valid_string(self):
     text = "select a from b;"
     (success, msg) = pgsanity.check_string(text)
     self.assertTrue(success)
Exemplo n.º 8
0
 def test_check_invalid_string(self):
     text = "garbage select a from b;"
     (success, msg) = pgsanity.check_string(text)
     self.assertFalse(success)
     self.assertEqual('line 1: ERROR: unrecognized data type name "garbage"', msg)
Exemplo n.º 9
0
 def test_check_valid_string(self):
     text = "select a from b;"
     (success, msg) = pgsanity.check_string(text)
     self.assertTrue(success)
Exemplo n.º 10
0
 def test_check_invalid_string(self):
     text = "garbage select a from b;"
     msglist = pgsanity.check_string(text)
     self.assertTrue(msglist)
     self.assertEqual(['line 1: ERROR: unrecognized data type name "garbage"'], msglist)
Exemplo n.º 11
0
 def test_check_valid_string(self):
     text = "select a from b;"
     msglist = pgsanity.check_string(text)
     self.assertFalse(msglist)
Exemplo n.º 12
0
 def test_sql_max_jerk_z_query(self):
     success, msg = check_string(sql_queries.max_jerk_z_query, True)
     self.assertTrue(success, msg)
Exemplo n.º 13
0
 def test_sql_create_jerked_truck_events_table(self):
     success, msg = check_string(sql_queries.average_acceleration_query,
                                 True)
     self.assertTrue(success, msg)
Exemplo n.º 14
0
 def test_sql_syntax_drop_jerked_truck_events_table(self):
     success, msg = check_string(sql_queries.drop_jerked_truck_events_table,
                                 True)
     self.assertTrue(success, msg)