[英]How to escape table names in SqlAlchemy
我正在研究 Apache Drill 的 SQLAlchemy 方言,但遇到了一个我似乎无法弄清楚的问题。
基本问题是 SQLAlchemy 正在生成如下查询:
SELECT `field1`, `field2`
FROM dfs.test.data.csv LIMIT 100
失败是因为data.csv
需要在它周围data.csv
反引号,如下所示:
SELECT `field1`, `field2`
FROM dfs.test.`data.csv` LIMIT 100
我已经在方言的编译器中定义了各种visit_()
函数,但这些似乎没有效果。
这花了一些时间来弄清楚,我想我会发布结果,这样如果其他人遇到这个问题,他们就会有一个关于如何解决它的参考点。
这是最终的工作代码:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.