如何在 SqlAlchemy 中转义表名

cgi*_*vre 5 python sqlalchemy apache-drill

我正在研究 Apache Drill 的 SQLAlchemy 方言,但遇到了一个我似乎无法弄清楚的问题。

基本问题是 SQLAlchemy 正在生成如下查询:

SELECT `field1`, `field2`
FROM dfs.test.data.csv LIMIT 100
Run Code Online (Sandbox Code Playgroud)

失败是因为data.csv需要在它周围加上反引号,如下所示:

SELECT `field1`, `field2`
FROM dfs.test.`data.csv` LIMIT 100
Run Code Online (Sandbox Code Playgroud)

我已经visit_()在方言的编译器中定义了各种函数,但这些似乎没有效果。

cgi*_*vre 2

这花了一些时间来弄清楚,我想我应该发布结果,这样如果其他人遇到这个问题,他们就会有一个关于如何解决它的参考点。

这是最终的工作代码:

https://github.com/JohnOmernik/sqlalchemy-drill/blob/master/sqlalchemy_drill/base.py

最终解决这个问题的方法如下:

    def __init__(self, dialect):
        super(DrillIdentifierPreparer, self).__init__(dialect, initial_quote='`', final_quote='`')

    def format_drill_table(self, schema, isFile=True):
        formatted_schema = ""

        num_dots = schema.count(".")
        schema = schema.replace('`', '')

        # For a file, the last section will be the file extension
        schema_parts = schema.split('.')

        if isFile and num_dots == 3:
            # Case for File + Workspace
            plugin = schema_parts[0]
            workspace = schema_parts[1]
            table = schema_parts[2] + "." + schema_parts[3]
            formatted_schema = plugin + ".`" + workspace + "`.`" + table + "`"
        elif isFile and num_dots == 2:
            # Case for file and no workspace
            plugin = schema_parts[0]
            formatted_schema = plugin + "." + schema_parts[1] + ".`" + schema_parts[2] + "`"
        else:
            # Case for non-file plugins or incomplete schema parts
            for part in schema_parts:
                quoted_part = "`" + part + "`"
                if len(formatted_schema) > 0:
                    formatted_schema += "." + quoted_part
                else:
                    formatted_schema = quoted_part

        return formatted_schema
Run Code Online (Sandbox Code Playgroud)