在SQLAlchemy中,我试图在自定义构建的group_concat调用中嵌入一个内置的“concat”调用。两者都作为参数传递字符串,但在呈现/执行查询时字符串正在反转。
示例:
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import expression
from sqlalchemy import create_engine, MetaData
from sqlalchemy import Table, Column, Integer, String
from sqlalchemy.sql import select
from sqlalchemy.sql.functions import concat
class group_concat(expression.FunctionElement):
name = "group_concat"
@compiles(group_concat, 'mysql')
def _group_concat_mysql(element, compiler, **kw):
separator = compiler.process(element.clauses.clauses[1]) \
if len(element.clauses) == 2 else "','"
return "GROUP_CONCAT(%s SEPARATOR %s)" % (
compiler.process(element.clauses.clauses[0]),
separator,
)
table = Table(
'test_constructs', MetaData(),
Column('id', Integer, primary_key=True),
Column('column1', String(255)),
Column('column2', String(255)),
Column('foreign_id', Integer(11)),
)
def select_records():
column1 = table.c.column1
column2 = table.c.column2
test_query = select([
table.c.foreign_id,
expression.label('clean_test_column',
group_concat(concat(column1,
',',
column2),
'|')),
]).group_by(table.c.foreign_id)
res = engine.execute(test_query)
此日志输出是:
2012-06-01 09:48:29,552 INFO sqlalchemy.engine.base.Engine SELECT test_constructs.foreign_id, GROUP_CONCAT(concat(test_constructs.column1, %s, test_constructs.column2) SEPARATOR %s) AS clean_test_column FROM test_constructs GROUP BY test_constructs.foreign_id
2012-06-01 09:48:29,552 INFO sqlalchemy.engine.base.Engine ('|', ',')
...请注意,正在替换到查询中的参数的顺序是相反的。 '|'被替换为CONCAT并且','正被GROUP_CONCAT使用。
答案 0 :(得分:0)
这个问题是因为分隔符首先被“编译”,但是第二个被添加到字符串中。因此,它在声明中以正确的顺序结束,但在参数中反向顺序。
相反,我正在做:
@compiles(group_concat, 'mysql')
def _group_concat_mysql(element, compiler, **kw):
return "GROUP_CONCAT({0} SEPARATOR {1})".format(
compiler.process(element.clauses.clauses[0]),
compiler.process(element.clauses.clauses[1]) \
if len(element.clauses) == 2 else ",",
)
......似乎工作正常。