Python替换||具有CONCAT功能的操作员

时间:2018-04-24 12:55:05

标签: python regex

我正在尝试解析SQL语句,如下所示。

SELECT '-' || IPC.ITEM, IPC.first_name||' '||ISA.last_name AS DEA FROM IpcDetails IPC JOIN IsaDetails ISA ON IPC.ItemId = ISA.ItemId

我要求更换||具有CONCAT功能的运算符如下,

SELECT CONCAT('-',IPC.ITEM), CONCAT(IPC.first_name,' ',ISA.last_name) AS DEA FROM IpcDetails IPC JOIN IsaDetails ISA ON IPC.ItemId = ISA.ItemId

我想过使用正则表达式将表达式拆分为||并且用功能取代操作员,有人可以帮助我实现同样的目标吗?

1 个答案:

答案 0 :(得分:0)

可能你应该为此使用一些SQL解析器。但是你可以尝试使用正则表达式来解决这个问题,这些表达式可能不是解决此类问题的最佳工具。

这是一个示例(检查注释以查看代码正在执行的操作):

EnsureCreated

如果你运行这个脚本,你会在控制台上看到这个:

import re

sql = "SELECT '-' || IPC.ITEM AS ITEM, IPC.first_name||' '||ISA.last_name, IPC.NAME, '0' || IPC.BAR AS DEA " \
      "FROM IpcDetails IPC JOIN IsaDetails ISA ON IPC.ItemId = ISA.ItemId;" \
      "SELECT '-' || IPC.ITEM, IPC.first_name||' '||ISA.last_name, IPC.NAME, '0' || IPC.BAR, IPC.FOO || '_BAR' || 'TEST' AS DEA " \
      "FROM IpcDetails IPC JOIN IsaDetails ISA ON IPC.ItemId = ISA.ItemId"

# Split all selects
selects = (re.findall(r'SELECT(.+?)FROM(.+?)(;|$)', sql, re.IGNORECASE))
for select in selects:
    origFields = select[0]
    fieldList = []
    # Split fields : not great to use split here as there might be commas inside of strings
    for field in origFields.split(","):
        # Split the field and the alias
        fieldAndAlias = re.search('(.+?)(\sAS.+|$)', field, re.IGNORECASE)
        if fieldAndAlias.group(1).find("||") > -1: # Check if we should do the transformation from || to CONCAT()
            concat = "CONCAT(%s)" % ",".join(fieldAndAlias.group(1).split("||"))
            concat += fieldAndAlias.group(2)
            fieldList.append(concat)
        else: # Field where no concat to be done
            fieldList.append(field)
    # Put everthing back again.
    newSQL = "SELECT %s FROM %s" % (",".join(fieldList), select[1])
    print(newSQL)

此解决方案不处理子选择以及没有" AS"的别名。关键词。所以是的:你需要一个合适的SQL解析器来完成一个非常专业的工作。正则表达式不足以完成这项工作。