Пример #1
0
    def from_sql(cls, sql: str) -> "PGMaterializedView":
        """Create an instance from a SQL string"""

        # Strip optional semicolon and all whitespace from end of definition
        # because the "with data" clause is optional and the alternative would be to enumerate
        # every possibility in the templates
        sql = strip_terminating_semicolon(sql)

        templates = [
            # Enumerate maybe semicolon endings
            "create{}materialized{}view{:s}{schema}.{signature}{:s}as{:s}{definition}{:s}with{:s}data",
            "create{}materialized{}view{:s}{schema}.{signature}{:s}as{:s}{definition}{}with{:s}{no_data}{:s}data",
            "create{}materialized{}view{:s}{schema}.{signature}{:s}as{:s}{definition}",
        ]

        for template in templates:
            result = parse(template, sql, case_sensitive=False)

            if result is not None:
                with_data = not "no_data" in result

                # If the signature includes column e.g. my_view (col1, col2, col3) remove them
                signature = result["signature"].split("(")[0]

                return cls(
                    schema=result["schema"],
                    # strip quote characters
                    signature=signature.replace('"', ""),
                    definition=result["definition"],
                    with_data=with_data,
                )

        raise SQLParseFailure(f'Failed to parse SQL into PGView """{sql}"""')
Пример #2
0
    def from_sql(cls, sql: str) -> "PGTrigger":
        """Create an instance instance from a SQL string"""
        for template in cls._templates:
            result = parse(template, sql, case_sensitive=False)
            if result is not None:
                # remove possible quotes from signature
                signature = result["signature"]
                event = result["event"]
                on_entity = result["on_entity"]
                action = result["action"]
                is_constraint = "constraint" in template

                if "." not in on_entity:
                    on_entity = "public" + "." + on_entity

                schema = on_entity.split(".")[0]

                definition_template = " {event} ON {on_entity} {action}"
                definition = definition_template.format(event=event,
                                                        on_entity=on_entity,
                                                        action=action)

                return cls(
                    schema=schema,
                    signature=signature,
                    on_entity=on_entity,
                    definition=definition,
                    is_constraint=is_constraint,
                )
        raise SQLParseFailure(
            f'Failed to parse SQL into PGTrigger """{sql}"""')
Пример #3
0
    def to_sql_statement_create(self):
        """ Generates a SQL "create function" statement for PGFunction """

        # We need to parse and replace the schema qualifier on the table for simulate_entity to
        # operate
        _def = self.definition
        _template = "{event}{:s}ON{:s}{on_entity}{:s}{action}"
        match = parse(_template, _def)
        if not match:
            raise SQLParseFailure(
                f'Failed to parse SQL into PGTrigger.definition """{_def}"""')

        event = match["event"]
        action = match["action"]

        # Ensure entity is qualified with schema
        on_entity = match["on_entity"]
        if "." in on_entity:
            _, _, on_entity = on_entity.partition(".")
        on_entity = f"{self.schema}.{on_entity}"

        # Re-render the definition ensuring the table is qualified with
        def_rendered = _template.replace("{:s}",
                                         " ").format(event=event,
                                                     on_entity=on_entity,
                                                     action=action)

        return sql_text(
            f"CREATE{' CONSTRAINT ' if self.is_constraint else ' '}TRIGGER {self.signature} {def_rendered}"
        )
Пример #4
0
    def from_sql(cls, sql: str) -> PGView:
        """Create an instance from a SQL string"""
        template = "create{}view{:s}{schema}.{signature}{:s}as{:s}{definition}"
        result = parse(template, sql, case_sensitive=False)
        if result is not None:
            return cls(
                schema=result["schema"],
                signature=result["signature"],
                definition=result["definition"],
            )

        raise SQLParseFailure(f'Failed to parse SQL into PGView """{sql}"""')
Пример #5
0
    def from_sql(cls, sql: str) -> "PGView":
        """Create an instance from a SQL string"""
        template = "create{}view{:s}{schema}.{signature}{:s}as{:s}{definition}"
        result = parse(template, sql, case_sensitive=False)
        if result is not None:
            # If the signature includes column e.g. my_view (col1, col2, col3) remove them
            signature = result["signature"].split("(")[0]
            return cls(
                schema=result["schema"],
                # strip quote characters
                signature=signature.replace('"', ""),
                definition=strip_terminating_semicolon(result["definition"]),
            )

        raise SQLParseFailure(f'Failed to parse SQL into PGView """{sql}"""')
Пример #6
0
 def from_sql(cls, sql: str) -> "PGFunction":
     """Create an instance instance from a SQL string"""
     template = "create{}function{:s}{schema}.{signature}{:s}returns{:s}{definition}"
     result = parse(template, sql.strip(), case_sensitive=False)
     if result is not None:
         # remove possible quotes from signature
         raw_signature = result["signature"]
         signature = ("".join(raw_signature.split('"', 2))
                      if raw_signature.startswith('"') else raw_signature)
         return cls(
             schema=result["schema"],
             signature=signature,
             definition="returns " + result["definition"],
         )
     raise SQLParseFailure(
         f'Failed to parse SQL into PGFunction """{sql}"""')
Пример #7
0
    def from_sql(cls, sql: str) -> "PGPolicy":
        """Create an instance instance from a SQL string"""

        template = "create policy{:s}{signature}{:s}on{:s}{on_entity}{:s}{definition}"
        result = parse(template, sql.strip(), case_sensitive=False)

        if result is not None:

            on_entity = result["on_entity"]
            if "." not in on_entity:
                schema = "public"
                on_entity = schema + "." + on_entity
            schema, _, _ = on_entity.partition(".")

            return cls(
                schema=schema,
                signature=result["signature"],
                definition=result["definition"],
                on_entity=on_entity,
            )
        raise SQLParseFailure(f'Failed to parse SQL into PGPolicy """{sql}"""')