【问题标题】:SQLParse - issue with the wildcard LIKE conditionSQLParse - 通配符 LIKE 条件问题
【发布时间】:2021-05-26 12:11:17
【问题描述】:

我专注于 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。如果你在for condition in item.tokens: 之后添加print(condition),你就会明白我的意思了。

【问题讨论】:

    标签: python sql-like identifier sql-parser


    【解决方案1】:

    问题是您在 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
    

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 2013-04-20
      相关资源
      最近更新 更多