0

我专注于 SQL 语句的 Where 子句,并试图撤回 Where 子句中使用的所有列。下面是一个示例 SQL:

sql_2 = """Select
PERS_ID
, STF_NO
, NAME 
FROM 
TEST T

WHERE T.JOIN_DT >= T.POSTING_DT'
AND T.PERS_ID LIKE '%123%'
AND T.LEAVE_DT BETWEEN CURRENT_DATE - 20 AND CURRENT_DATE - 1"""

我期待的列是 T.JOIN_DT、T.POSTING_DT、T.PERS_ID 和 T.LEAVE_DT。下面的代码能够提取除 T.PERS_ID 之外的所有内容

from __future__ import print_function
import re
import sqlparse 
import numpy as np
from sqlparse.sql import IdentifierList, Identifier, Function, Where, Parenthesis, TokenList, Comparison, Operation
from sqlparse.tokens import Keyword, DML, Punctuation

sql_2 = """Select
    PERS_ID
    , STF_NO
    , NAME 
    FROM 
    TEST T
    
    WHERE T.JOIN_DT >= T.POSTING_DT'
    AND T.PERS_ID LIKE '%123%'
    AND T.LEAVE_DT BETWEEN CURRENT_DATE - 20 AND CURRENT_DATE - 1"""

parsed = sqlparse.parse(sql_2)[0]

where_columns = []
full_columns = []

for item in parsed.tokens:
    if isinstance(item, Where):

        
        for condition in item.tokens:
            
            if isinstance(condition, Identifier):
                where_columns.append(condition.get_parent_name())
                where_columns.append(condition.get_real_name())
                full_columns.append(where_columns)
                where_columns = []
        
                       
            if isinstance(condition, Comparison):
                for breakdown in condition.tokens:
                    if isinstance(breakdown, Identifier):
                        where_columns.append(breakdown.get_parent_name())
                        where_columns.append(breakdown.get_real_name())
                        full_columns.append(where_columns)
                        where_columns = []
                              

print(full_columns)

似乎将 'AND P.PERS_ID LIKE '%123' 分组为一个令牌,我不确定如何进一步分解它以获得标识符 P.PERS_ID。如果你添加print(condition)之后for condition in item.tokens:你会明白我的意思。

4

1 回答 1

0

问题是您在 T.POSTING_DT 之后有一个尾随引号

然而,我的库SQLGlot能够轻松提取列。

import sqlglot
import sqlglot.expressions as exp

sql = """
Select
PERS_ID
, STF_NO
, NAME
FROM
TEST T

WHERE T.JOIN_DT >= T.POSTING_DT'
AND T.PERS_ID LIKE '%123%'
AND T.LEAVE_DT BETWEEN CURRENT_DATE - 20 AND CURRENT_DATE - 1
"""

for column in sqlglot.parse_one(sql).find(exp.Where).find_all(exp.Column):
    print(column.text("this"))


CURRENT_DATE
CURRENT_DATE
LEAVE_DT
PERS_ID
POSTING_DT'
JOIN_DT
于 2021-11-17T05:17:49.460 回答