在 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 使用。