在函数名上拆分SQL语句,但在Python中保留分隔符

2024-10-01 11:37:51 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有下面的string,其中包含从SELECT子句中提取的SQL语句(实际上,这是一个包含数百个这样的语句的巨大SQL语句)

  SUM(case when(A.money-B.money>1000
                and A.unixtime-B.unixtime<=890769
                and B.col10 = "A"
                and B.col11 = "12"
                and B.col12 = "V") then 10
      end) as finalCond0,
  MAX(case when(A.money-B.money<0
                and A.unixtime-B.unixtime<=6786000
                and B.cond1 = "A"
                and B.cond2 = "4321"
                and B.cond3 in ("E", "F", "G")) then A.col10
        end) as finalCond1,
  SUM(case when(A.money-B.money>0
                and A.unixtime-B.unixtime<=6786000
                and B.cond1 = "A"
                and B.cond2 = "1234"
                and B.cond3 in ("A", "B", "C")) then 2
      end) as finalCond2    

如何在函数(即SUMMAXMINMEAN等)上拆分此查询,以便在不删除分隔符的情况下提取最后一个查询(在本例中是SUM)?在

因此,所需的输出将是一个如下所示的字符串:

^{pr2}$

PS:出于表示目的,我提供了一些缩进,但实际上这些语句是用逗号分隔的,这意味着原始格式中没有空白或新行。在


Tags: andsqlas语句maxendwhensum
3条回答

在这里不能使用正则表达式,因为SQL语法不形成可以与Pythonre引擎匹配的正则模式。实际上,您必须将字符串解析为令牌流或语法树;毕竟,SUM(...)可以包含大量语法,包括子选择。在

^{} library可以这样做,即使它是bit underdocumented and not that friendly to external uses。在

重新使用我在链接到的另一篇文章中定义的walk_tokens函数:

from collections import deque
from sqlparse.sql import TokenList

def walk_tokens(token):
    queue = deque([token])
    while queue:
        token = queue.popleft()
        if isinstance(token, TokenList):
            queue.extend(token)
        yield token

SELECT标识符列表中提取最后一个元素是:

^{pr2}$

演示:

>>> sql = '''\
...   SUM(case when(A.money-B.money>1000
...                 and A.unixtime-B.unixtime<=890769
...                 and B.col10 = "A"
...                 and B.col11 = "12"
...                 and B.col12 = "V") then 10
...       end) as finalCond0,
...   MAX(case when(A.money-B.money<0
...                 and A.unixtime-B.unixtime<=6786000
...                 and B.cond1 = "A"
...                 and B.cond2 = "4321"
...                 and B.cond3 in ("E", "F", "G")) then A.col10
...         end) as finalCond1,
...   SUM(case when(A.money-B.money>0
...                 and A.unixtime-B.unixtime<=6786000
...                 and B.cond1 = "A"
...                 and B.cond2 = "1234"
...                 and B.cond3 in ("A", "B", "C")) then 2
...       end) as finalCond2
... '''
>>> tokens = sqlparse.parse(sql)[0]
>>> for tok in walk_tokens(tokens):
...     if isinstance(tok, IdentifierList):
...         # iterate to leave the last assigned to `identifier`
...         for identifier in tok.get_identifiers():
...             pass
...         break
...
>>> print(identifier)
SUM(case when(A.money-B.money>0
                and A.unixtime-B.unixtime<=6786000
                and B.cond1 = "A"
                and B.cond2 = "1234"
                and B.cond3 in ("A", "B", "C")) then 2
      end) as finalCond2

identifier是一个sqlparse.sql.Identifier实例,但是再次将其转换为字符串(这是print()所做的,或者您可以只使用str())为该部分再次提供输入SQL字符串。在

您可以使用类似于:

import re

str = 'SUM(case when(A.money-B.money>1000 and A.unixtime-B.unixtime<=890769 and B.col10 = "A" and B.col11 = "12" and B.col12 = "V") then 10 end) as finalCond0, MAX(case when(A.money-B.money<0 and A.unixtime-B.unixtime<=6786000 and B.cond1 = "A" and B.cond2 = "4321" and B.cond3 in ("E", "F", "G")) then A.col10 end) as finalCond1, SUM(case when(A.money-B.money>0 and A.unixtime-B.unixtime<=6786000 and B.cond1 = "A" and B.cond2 = "1234" and B.cond3 in ("A", "B", "C")) then 2 end) as finalCond2'

result = re.finditer('as\s+[a-zA-Z0-9]+', str);

commas = []
parts = []

for reg in result:
    end = reg.end()
    if(len(str) > end and str[end] == ','):
        commas.append(end)

idx = 0
for comma in commas:
    parts.append(str[idx:comma])
    idx = comma + 1
parts.append(str[idx:])

print(parts)

commas数组中,将有需要拆分的逗号。输出将是:

^{pr2}$

在parts中,您将拥有包含这些部分的最终数组(不确定此实现是否是最佳方法):

[
    'SUM(case when(A.money-B.money>1000 and A.unixtime-B.unixtime<=890769 and B.col10 = "A" and B.col11 = "12" and B.col12 = "V") then 10 end) as finalCond0',
    ' MAX(case when(A.money-B.money<0 and A.unixtime-B.unixtime<=6786000 and B.cond1 = "A" and B.cond2 = "4321" and B.cond3 in ("E", "F", "G")) then A.col10 end) as finalCond1',
    ' SUM(case when(A.money-B.money>0 and A.unixtime-B.unixtime<=6786000 and B.cond1 = "A" and B.cond2 = "1234" and B.cond3 in ("A", "B", "C")) then 2 end) as finalCond2'
]

我有一个解决方案,但代码有点多。这没有使用regex,只是对关键字进行多次拆分。在

s = """
SUM(case when(A.money-B.money>1000
                and A.unixtime-B.unixtime<=890769
                and B.col10 = "A"
                and B.col11 = "12"
                and B.col12 = "V") then 10
      end) as finalCond0,
MAX(case when(A.money-B.money<0
                and A.unixtime-B.unixtime<=6786000
                and B.cond1 = "A"
                and B.cond2 = "4321"
                and B.cond3 in ("E", "F", "G")) then A.col10
      end) as finalCond1,
SUM(case when(A.money-B.money>0
                and A.unixtime-B.unixtime<=6786000
                and B.cond1 = "A"
                and B.cond2 = "1234"
                and B.cond3 in ("A", "B", "C")) then 2
      end) as finalCond2 
"""

# remove newlines and doble spaces
s = s.replace('\n', ' ')
while '  ' in s:
    s = s.replace('  ', ' ')
s = s.strip()

# split on keywords, starting with the original string
current_parts = [s, ]
for kw in ['SUM', 'MAX', 'MIN']:
    new_parts = []
    for part in current_parts:
        for i, new_part in enumerate(part.split(kw)):
            if i > 0:
                # add keyword to the start of this substring
                new_part = '{}{}'.format(kw, new_part)

            new_part = new_part.strip()
            if len(new_part) > 0:
                new_parts.append(new_part.strip())

    current_parts = new_parts

print()
print('current_parts:')
for s in current_parts:
    print(s)

我得到的输出是:

^{pr2}$

对你有用吗?它似乎适用于你在问题中输入的示例字符串。在

相关问题 更多 >