使用 Python sqlparse 获取查询树/层次结构

Posted

技术标签:

【中文标题】使用 Python sqlparse 获取查询树/层次结构【英文标题】:get query tree/hierarchy using Python sqlparse 【发布时间】:2021-12-13 05:03:33 【问题描述】:

我正在开发一个客户数据库,其中包含从复杂查询创建的大量视图,其中包含大量的查询和连接,如下面的示例查询。

我已经在使用sqlparse 来获取表名,但现在我想知道是否可以使用查询树/层次结构获取嵌套字典,从而允许创建如下所示的树视图:

结果会是这样的:

+-FULL JOIN-+
            |
            +-- QUERY 01 +
            |            |
            |            +-- QUERY 01.01 + 
            |                            |
            |                            + -- QUERY 01.01.01
            |                            + -- QUERY 01.01.02
            +-- QUERY 02 +
etc...

对于每个 QUERY,在 FROM 子句中列出来源

示例查询:

SELECT --Locais Disponíveis para Abertura de Ordens de corte
 COALESCE(a."Chave", b."Chave") AS "Chave",
 COALESCE(a."Unidade", b."Unidade") AS "Unidade",
 COALESCE(a."Fazenda", b."Fazenda") AS "Fazenda",
 COALESCE(a."Talhao", b."Talhao") AS "Talhao",
 COALESCE(a."Participacao", b."Participacao") AS "Participacao",
 CASE
   WHEN a."Condicao" = 'Disponível Parcial (Moagem)' AND
        b."Condicao" = 'Disponível Parcial (Mudas)' THEN
    'Disponível (Safra+Mudas)'
   ELSE
    COALESCE(a."Condicao", b."Condicao")
 END AS "Condicao",
 COALESCE(a."Estagio", b."Estagio") AS "Estagio",
 COALESCE(a."Variedade", b."Variedade") AS "Variedade",
 COALESCE(a."Ciclo Maturacao", b."Ciclo Maturacao") AS "Ciclo Maturacao",
 COALESCE(a."Propriedade", b."Propriedade") AS "Propriedade",
 COALESCE(a."Proprietario", b."Proprietario") AS "Proprietario",
 COALESCE(a."No. Corte", b."No. Corte") AS "No. Corte",
 (CASE
   WHEN a."Area" IS NULL THEN
0
   ELSE
    a."Area"
 END + CASE
   WHEN b."Area" IS NULL THEN
0
   ELSE
    b."Area"
 END) AS "Area",
 CASE
   WHEN a."Condicao" = 'Disponível Parcial (Moagem)' AND
        b."Condicao" = 'Disponível Parcial (Mudas)' THEN
    ((CASE
      WHEN a."Area" IS NULL THEN
0
      ELSE
       a."Area"
    END + CASE
      WHEN b."Area" IS NULL THEN
0
      ELSE
       b."Area"
    END) * a."TCH")
   ELSE
    a."Toneladas"
 END AS "Toneladas",
 a."TCH",
 COALESCE(a."Distancia", b."Distancia") AS "Distancia"
  FROM (SELECT --Disponibilidade (Moagem)
         A.*,
         a."Area" * b."TCH" AS "Toneladas",
         b."TCH" AS "TCH",
         c."Dist. Terra" + c."Dist. Asfalto" AS "Distancia"
          FROM ((SELECT --ÁREAS DISPONÍVEIS PARA ABERTURA DE ORDEM CORTE DE SAFRA
                  a."Fazenda" * 1000 + a."Talhao" AS "Chave",
                  CASE
                    WHEN a."Unidade" = 15 THEN
                     'USF'
                    ELSE
                     'URD'
                  END AS "Unidade",
                  a."Fazenda",
                  a."Talhao",
                  a."Participacao",
                  CASE
                    WHEN a."Ocorrencia Cadastro" = 'C' THEN
                     'Disponível Total (Moagem)'
                    ELSE
                     'Disponível Parcial (Moagem)'
                  END AS "Condicao",
                  a."Estagio",
                  a."Variedade",
                  a."Ciclo Maturacao",
                  a."Propriedade",
                  a."Proprietario",
                  a."No. Corte",
                  (a."Area" - (CASE
                    WHEN b."Area Fechada" IS NULL THEN
0
                    ELSE
                     b."Area Fechada"
                  END)) AS "Area"
                   FROM (SELECT --ULTIMA ESTIMATIVA DO TALHAO A
                          OBJ.CD_UNID_IND AS "Unidade",
                          OBJ.CD_UPNIVEL1 AS "Fazenda",
                          OBJ.CD_UPNIVEL3 AS "Talhao",
                          OBJ.CD_UPNIVEL1 || ' - ' || F.DE_UPNIVEL1 AS "Propriedade",
                          G.DE_FORNEC AS "Proprietario",
                          CASE
                            WHEN UP3.CD_TP_PROPR IN (1, 2, 3, 11) THEN
                             'Parceria'
                            WHEN UP3.CD_TP_PROPR IN (5, 8) THEN
                             'Fornecedor'
                            WHEN UP3.CD_TP_PROPR = 6 THEN
                             'Fornecedor'
                            WHEN UP3.CD_TP_PROPR = 14 THEN
                             'Parceria'
                            ELSE
                             'Verificar'
                          END AS "Participacao",
                          C.FG_OCORREN AS "Ocorrencia Cadastro",
                          C.DT_OCORREN AS "Data Ocorrencia",
                          B.DA_ESTAGIO AS "Estagio",
                          B.NO_CORTE AS "No. Corte",
                          D.DE_VARIED AS "Variedade",
                          E.DE_MATURAC AS "Ciclo Maturacao",
                          (OBJ.QT_AREA_PROD * 1) AS "Area",
                          (OBJ.QT_CANA_ENTR / 1000) AS "Toneladas"
                           FROM PIMSCS.HISTPREPRO   OBJ,
                                PIMSCS.ESTAGios     B,
                                PIMSCS.UPNIVEL3     UP3,
                                PIMSCS.SAFRUPNIV3   C,
                                PIMSCS.VARIEDADES   D,
                                PIMSCS.TIPO_MATURAC E,
                                PIMSCS.UPNIVEL1     F,
                                PIMSCS.FORNECS      G
                          WHERE OBJ.CD_SAFRA =
                                (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                            AND OBJ.CD_UNID_IND IN (15, 19)
                            AND OBJ.CD_ESTAGIO = B.CD_ESTAGIO
                            AND OBJ.CD_UPNIVEL1 = UP3.CD_UPNIVEL1
                            AND OBJ.CD_UPNIVEL3 = UP3.CD_UPNIVEL3
                            AND OBJ.CD_SAFRA = UP3.CD_SAFRA
                            AND OBJ.CD_UPNIVEL1 = C.CD_UPNIVEL1
                            AND OBJ.CD_UPNIVEL3 = C.CD_UPNIVEL3
                            AND OBJ.CD_SAFRA = C.CD_SAFRA
                            AND UP3.CD_VARIED = D.CD_VARIED
                            AND E.FG_MATURAC = D.FG_MATURAC
                            AND OBJ.CD_UPNIVEL1 = F.CD_UPNIVEL1
                            AND F.CD_FORNEC = G.CD_FORNEC
                            AND C.DT_OCORREN =
                                (SELECT MAX(D.DT_OCORREN)
                                   FROM PIMSCS.SAFRUPNIV3 D
                                  WHERE D.CD_UPNIVEL1 = C.CD_UPNIVEL1
                                    AND D.CD_UPNIVEL3 = C.CD_UPNIVEL3
                                    AND D.CD_SAFRA = C.CD_SAFRA)
                            AND OBJ.CD_HIST =
                                (SELECT OBJ2.CD_HIST
                                   FROM PIMSCS.HISTPREPRO OBJ2
                                  WHERE OBJ2.CD_UPNIVEL1 = OBJ.CD_UPNIVEL1
                                    AND OBJ2.CD_UPNIVEL3 = OBJ.CD_UPNIVEL3
                                    AND OBJ2.CD_SAFRA =
                                        (SELECT MAX(CD_SAFRA)
                                           FROM PIMSCS.HISTPREPRO)
                                    AND OBJ2.CD_HIST NOT IN ('E', 'S')
                                    AND OBJ2.CD_EMPRESA IN (15, 19)
                                    AND OBJ2.DT_HISTORICO =
                                        (SELECT MAX(OBJ3.DT_HISTORICO)
                                           FROM PIMSCS.HISTPREPRO OBJ3
                                          WHERE OBJ3.CD_UPNIVEL1 =
                                                OBJ.CD_UPNIVEL1
                                            AND OBJ3.CD_UPNIVEL3 =
                                                OBJ.CD_UPNIVEL3
                                            AND OBJ3.CD_SAFRA =
                                                (SELECT MAX(CD_SAFRA)
                                                   FROM PIMSCS.HISTPREPRO)
                                            AND OBJ3.CD_HIST NOT IN ('E', 'S')
                                            AND OBJ3.CD_EMPRESA IN (15, 19)))) A,
                        (SELECT --ÁREA DE ORDEM DE CORTE DE SAFRA FECHADA B
                          QD.CD_UPNIVEL1 AS "Fazenda",
                          QD.CD_UPNIVEL3 AS "Talhao",
                          SUM(QD.QT_AREA) AS "Area Fechada"
                           FROM PIMSCS.QUEIMA_HE QH, PIMSCS.QUEIMA_DE QD
                          WHERE QH.NO_QUEIMA = QD.NO_QUEIMA
                            AND QD.CD_SAFRA =
                                (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                          GROUP BY QD.CD_UPNIVEL1, QD.CD_UPNIVEL3) B
                  WHERE a."Fazenda" = b."Fazenda"(+)
                    AND a."Talhao" = b."Talhao"(+)
                    AND a."Ocorrencia Cadastro" <> 'F'
                    AND (a."Area" - (CASE
                          WHEN b."Area Fechada" IS NULL THEN
0
                          ELSE
                           b."Area Fechada"
                        END)) > 0)) A
          LEFT JOIN (SELECT --Ultima Estimativa do Talhão
                     A.CD_HIST "Cod. Historico",
                     CASE
                       WHEN A.CD_UNID_IND = 15 THEN
                        'USF'
                       ELSE
                        'URD'
                     END AS "Unidade",
                     A.CD_UPNIVEL1 AS "Zona",
                     A.CD_UPNIVEL3 AS "Talhao",
                     A.DT_HISTORICO AS "Data",
                     A.QT_AREA_PROD AS "Area",
                     (A.QT_CANA_ENTR / 1000) AS "Toneladas",
                     A.QT_TCH AS "TCH"
                      FROM PIMSCS.HISTPREPRO A
                     WHERE A.CD_UNID_IND IN (15, 19)
                       AND A.CD_SAFRA =
                           (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                       AND A.CD_HIST NOT IN ('E', 'S')
                       AND A.QT_AREA_PROD <> 0
                       AND A.DT_HISTORICO =
                           (SELECT MAX(A2.DT_HISTORICO)
                              FROM PIMSCS.HISTPREPRO A2
                             WHERE A.CD_SAFRA = A2.CD_SAFRA
                               AND A.CD_UPNIVEL2 = A2.CD_UPNIVEL1
                               AND A.CD_UPNIVEL3 = A2.CD_UPNIVEL3
                               AND A2.CD_HIST NOT IN ('E', 'S'))) B
            ON a."Fazenda" = b."Zona"
           AND a."Talhao" = b."Talhao"
          LEFT JOIN (SELECT --Distancia Cadastrada 
                     A.CD_UPNIVEL1 AS "Zona",
                     A.CD_UPNIVEL3 AS "Talhao",
                     MAX(A.DS_TERRA) AS "Dist. Terra",
                     MAX(A.DS_ASFALTO) AS "Dist. Asfalto"
                      FROM PIMSCS.UPNIVEL3 A
                      LEFT JOIN PIMSCS.SAFRUPNIV3 B
                        ON A.CD_SAFRA = B.CD_SAFRA
                       AND A.CD_UPNIVEL1 = B.CD_UPNIVEL1
                       AND A.CD_UPNIVEL3 = B.CD_UPNIVEL3
                     WHERE A.CD_UNID_IND IN (15, 19)
                       AND A.CD_OCUP = 1
                       AND A.CD_SAFRA =
                           (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                       AND B.CD_SAFRA =
                           (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                       AND B.FG_OCORREN <> 'I'
                       AND B.DT_OCORREN =
                           (SELECT MAX(B2.DT_OCORREN)
                              FROM PIMSCS.SAFRUPNIV3 B2
                             WHERE B.CD_SAFRA = B2.CD_SAFRA
                               AND B.CD_UPNIVEL1 = B2.CD_UPNIVEL1
                               AND B.CD_UPNIVEL3 = B2.CD_UPNIVEL3)
                     GROUP BY A.CD_UPNIVEL1, A.CD_UPNIVEL3) C
            ON a."Fazenda" = c."Zona"
           AND a."Talhao" = c."Talhao") A
  FULL JOIN (SELECT --Disponibilidade (Mudas)
              A.*,
              a."Area" * b."TCH" AS "Toneladas",
              b."TCH" AS "TCH",
              c."Dist. Terra" + c."Dist. Asfalto" AS "Distancia"
               FROM ((SELECT --ÁREAS DISPONÍVEIS PARA ABERTURA DE ORDEM CORTE DE SAFRA
                       a."Fazenda" * 1000 + a."Talhao" AS "Chave",
                       CASE
                         WHEN a."Unidade" = 15 THEN
                          'USF'
                         ELSE
                          'URD'
                       END AS "Unidade",
                       a."Fazenda",
                       a."Talhao",
                       a."Participacao",
                       CASE
                         WHEN a."Ocorrencia Cadastro" = 'C' THEN
                          'Disponível Total (Mudas)'
                         ELSE
                          'Disponível Parcial (Mudas)'
                       END AS "Condicao",
                       a."Estagio",
                       a."Variedade",
                       a."Ciclo Maturacao",
                       a."Propriedade",
                       a."Proprietario",
                       a."No. Corte",
                       (a."Area" - (CASE
                         WHEN b."Area Fechada" IS NULL THEN
0
                         ELSE
                          b."Area Fechada"
                       END)) AS "Area"
                        FROM (SELECT --ULTIMA ESTIMATIVA DO TALHAO A
                               OBJ.CD_UNID_IND AS "Unidade",
                               OBJ.CD_UPNIVEL1 AS "Fazenda",
                               OBJ.CD_UPNIVEL3 AS "Talhao",
                               OBJ.CD_UPNIVEL1 || ' - ' || F.DE_UPNIVEL1 AS "Propriedade",
                               G.DE_FORNEC AS "Proprietario",
                               CASE
                                 WHEN UP3.CD_TP_PROPR IN (1, 2, 3, 11) THEN
                                  'Parceria'
                                 WHEN UP3.CD_TP_PROPR IN (5, 8) THEN
                                  'Fornecedor'
                                 WHEN UP3.CD_TP_PROPR = 6 THEN
                                  'Fornecedor'
                                 WHEN UP3.CD_TP_PROPR = 14 THEN
                                  'Parceria'
                                 ELSE
                                  'Verificar'
                               END AS "Participacao",
                               C.FG_OCORREN AS "Ocorrencia Cadastro",
                               C.DT_OCORREN AS "Data Ocorrencia",
                               B.DA_ESTAGIO AS "Estagio",
                               B.NO_CORTE AS "No. Corte",
                               D.DE_VARIED AS "Variedade",
                               E.DE_MATURAC AS "Ciclo Maturacao",
                               (OBJ.QT_AREA_PROD * 1) AS "Area",
                               (OBJ.QT_CANA_ENTR / 1000) AS "Toneladas"
                                FROM PIMSCS.HISTPREPRO   OBJ,
                                     PIMSCS.ESTAGIOS     B,
                                     PIMSCS.UPNIVEL3     UP3,
                                     PIMSCS.SAFRUPNIV3   C,
                                     PIMSCS.VARIEDADES   D,
                                     PIMSCS.TIPO_MATURAC E,
                                     PIMSCS.UPNIVEL1     F,
                                     PIMSCS.FORNECS      G
                               WHERE OBJ.CD_SAFRA =
                                     (SELECT MAX(CD_SAFRA)
                                        FROM PIMSCS.HISTPREPRO)
                                 AND OBJ.CD_UNID_IND IN (15, 19)
                                 AND OBJ.CD_ESTAGIO = B.CD_ESTAGIO
                                 AND OBJ.CD_UPNIVEL1 = UP3.CD_UPNIVEL1
                                 AND OBJ.CD_UPNIVEL3 = UP3.CD_UPNIVEL3
                                 AND OBJ.CD_SAFRA = UP3.CD_SAFRA
                                 AND OBJ.CD_UPNIVEL1 = C.CD_UPNIVEL1
                                 AND OBJ.CD_UPNIVEL3 = C.CD_UPNIVEL3
                                 AND OBJ.CD_SAFRA = C.CD_SAFRA
                                 AND UP3.CD_VARIED = D.CD_VARIED
                                 AND E.FG_MATURAC = D.FG_MATURAC
                                 AND OBJ.CD_UPNIVEL1 = F.CD_UPNIVEL1
                                 AND F.CD_FORNEC = G.CD_FORNEC
                                 AND C.DT_OCORREN =
                                     (SELECT MAX(D.DT_OCORREN)
                                        FROM PIMSCS.SAFRUPNIV3 D
                                       WHERE D.CD_UPNIVEL1 = C.CD_UPNIVEL1
                                         AND D.CD_UPNIVEL3 = C.CD_UPNIVEL3
                                         AND D.CD_SAFRA = C.CD_SAFRA)
                                 AND OBJ.CD_HIST =
                                     (SELECT OBJ2.CD_HIST
                                        FROM PIMSCS.HISTPREPRO OBJ2
                                       WHERE OBJ2.CD_UPNIVEL1 = OBJ.CD_UPNIVEL1
                                         AND OBJ2.CD_UPNIVEL3 = OBJ.CD_UPNIVEL3
                                         AND OBJ2.CD_SAFRA =
                                             (SELECT MAX(CD_SAFRA)
                                                FROM PIMSCS.HISTPREPRO)
                                         AND OBJ2.CD_HIST = 'S'
                                         AND OBJ2.CD_EMPRESA IN (15, 19)
                                         AND OBJ2.DT_HISTORICO =
                                             (SELECT MAX(OBJ3.DT_HISTORICO)
                                                FROM PIMSCS.HISTPREPRO OBJ3
                                               WHERE OBJ3.CD_UPNIVEL1 =
                                                     OBJ.CD_UPNIVEL1
                                                 AND OBJ3.CD_UPNIVEL3 =
                                                     OBJ.CD_UPNIVEL3
                                                 AND OBJ3.CD_SAFRA =
                                                     (SELECT MAX(CD_SAFRA)
                                                        FROM PIMSCS.HISTPREPRO)
                                                 AND OBJ3.CD_HIST = 'S'
                                                 AND OBJ3.CD_EMPRESA IN (15, 19)))) A,
                             (SELECT --ÁREA DE ORDEM DE CORTE DE MUDAS FECHADA B
                               A.CD_UPNIVEL1 AS "Fazenda",
                               A.CD_UPNIVEL3 AS "Talhao",
                               SUM(A.QT_AREA) AS "Area Fechada"
                                FROM  PIMSCS.OCORTEMD_DE A
                                JOIN PIMSCS.OCORTEMD_HE B
                                  ON A.NO_ORDEM = B.NO_ORDEM
                               WHERE A.CD_SAFRA =
                                     (SELECT MAX(CD_SAFRA)
                                        FROM PIMSCS.HISTPREPRO)
                                 AND B.FG_SITUACAO = 'F'
                               GROUP BY A.CD_UPNIVEL1, A.CD_UPNIVEL3) B
                       WHERE a."Fazenda" = b."Fazenda"(+)
                         AND a."Talhao" = b."Talhao"(+)
                         AND a."Ocorrencia Cadastro" <> 'F'
                         AND (a."Area" - (CASE
                               WHEN b."Area Fechada" IS NULL THEN
0
                               ELSE
                                b."Area Fechada"
                             END)) > 0)) A
               LEFT JOIN (SELECT --Ultima Estimativa do Talhão
                          A.CD_HIST "Cod. Historico",
                          CASE
                            WHEN A.CD_UNID_IND = 15 THEN
                             'USF'
                            ELSE
                             'URD'
                          END AS "Unidade",
                          A.CD_UPNIVEL1 AS "Zona",
                          A.CD_UPNIVEL3 AS "Talhao",
                          A.DT_HISTORICO AS "Data",
                          A.QT_AREA_PROD AS "Area",
                          (A.QT_CANA_ENTR / 1000) AS "Toneladas",
                          A.QT_TCH AS "TCH"
                           FROM PIMSCS.HISTPREPRO A
                          WHERE A.CD_UNID_IND IN (15, 19)
                            AND A.CD_SAFRA =
                                (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                            AND A.CD_HIST = 'S'
                            AND A.QT_AREA_PROD <> 0
                            AND A.DT_HISTORICO =
                                (SELECT MAX(A2.DT_HISTORICO)
                                   FROM PIMSCS.HISTPREPRO A2
                                  WHERE A.CD_SAFRA = A2.CD_SAFRA
                                    AND A.CD_UPNIVEL2 = A2.CD_UPNIVEL1
                                    AND A.CD_UPNIVEL3 = A2.CD_UPNIVEL3
                                    AND A2.CD_HIST = 'S')) B
                 ON a."Fazenda" = b."Zona"
                AND a."Talhao" = b."Talhao"
               LEFT JOIN (SELECT --Distancia Cadastrada 
                          A.CD_UPNIVEL1 AS "Zona",
                          A.CD_UPNIVEL3 AS "Talhao",
                          MAX(A.DS_TERRA) AS "Dist. Terra",
                          MAX(A.DS_ASFALTO) AS "Dist. Asfalto"
                           FROM PIMSCS.UPNIVEL3 A
                           LEFT JOIN PIMSCS.SAFRUPNIV3 B
                             ON A.CD_SAFRA = B.CD_SAFRA
                            AND A.CD_UPNIVEL1 = B.CD_UPNIVEL1
                            AND A.CD_UPNIVEL3 = B.CD_UPNIVEL3
                          WHERE A.CD_UNID_IND IN (15, 19)
                            AND A.CD_OCUP = 1
                            AND A.CD_SAFRA =
                                (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                            AND B.CD_SAFRA =
                                (SELECT MAX(CD_SAFRA) FROM PIMSCS.HISTPREPRO)
                            AND B.FG_OCORREN <> 'I'
                            AND B.DT_OCORREN =
                                (SELECT MAX(B2.DT_OCORREN)
                                   FROM PIMSCS.SAFRUPNIV3 B2
                                  WHERE B.CD_SAFRA = B2.CD_SAFRA
                                    AND B.CD_UPNIVEL1 = B2.CD_UPNIVEL1
                                    AND B.CD_UPNIVEL3 = B2.CD_UPNIVEL3)
                          GROUP BY A.CD_UPNIVEL1, A.CD_UPNIVEL3) C
                 ON a."Fazenda" = c."Zona"
                AND a."Talhao" = c."Talhao") B
    ON a."Chave" = b."Chave"

【问题讨论】:

【参考方案1】:

你可以使用递归:

import sqlparse, re
def has_select(s):
   if any(isinstance(i, sqlparse.sql.Token) and i.value.lower() == 'select' for i in getattr(s, 'tokens', [])):
      yield s
   else:
      yield from [j for k in getattr(s, 'tokens', []) for j in has_select(k)]

def q_tree(s, alias=None):
    c, f = [], None
    for i in s:
       if type(i) in (sqlparse.sql.Token,):
          if re.findall('^from$|join$', i.value, re.I):
             f = i.value
       elif type(i) in (sqlparse.sql.Identifier, sqlparse.sql.IdentifierList, sqlparse.sql.Parenthesis) and f is not None:
          if (s_stml:=next(has_select(i), None)) is not None:
             c.append((f, q_tree(s_stml, None if not isinstance(i, sqlparse.sql.Identifier) else list(i)[-1].value)))
          else:
             fr = ''.join(str(j) for j in i if j.value not in 'as', '\n')
             c.append((f, ['table':(t1:=t.split())[0], 'alias':None if len(t1) < 2 else t1[-1]
              for t in re.findall('(?:\w+\.\w+|\w+)\s+\w+|(?:\w+\.\w+|\w+)', fr)]))
          f = None
    return 'query':c, 'alias':alias

print(q_tree(sqlparse.parse(query_string)[0]))

输出:

'query': [('FROM', 'query': [('FROM', 'query': [('FROM', 'query': [('FROM', ['table': 'PIMSCS.HISTPREPRO', 'alias': 'OBJ', 'table': 'PIMSCS.ESTAGIOS', 'alias': 'B', 'table': 'PIMSCS.UPNIVEL3', 'alias': 'UP3', 'table': 'PIMSCS.SAFRUPNIV3', 'alias': 'C', 'table': 'PIMSCS.VARIEDADES', 'alias': 'D', 'table': 'PIMSCS.TIPO_MATURAC', 'alias': 'E', 'table': 'PIMSCS.UPNIVEL1', 'alias': 'F', 'table': 'PIMSCS.FORNECS', 'alias': 'G'])], 'alias': None)], 'alias': 'A'), ('LEFT JOIN', 'query': [('FROM', ['table': 'PIMSCS.HISTPREPRO', 'alias': 'A'])], 'alias': 'B'), ('LEFT JOIN', 'query': [('FROM', ['table': 'PIMSCS.UPNIVEL3', 'alias': 'A']), ('LEFT JOIN', ['table': 'PIMSCS.SAFRUPNIV3', 'alias': 'B'])], 'alias': 'C')], 'alias': 'A'), ('FULL JOIN', 'query': [('FROM', 'query': [('FROM', 'query': [('FROM', ['table': 'PIMSCS.HISTPREPRO', 'alias': 'OBJ', 'table': 'PIMSCS.ESTAGIOS', 'alias': 'B', 'table': 'PIMSCS.UPNIVEL3', 'alias': 'UP3', 'table': 'PIMSCS.SAFRUPNIV3', 'alias': 'C', 'table': 'PIMSCS.VARIEDADES', 'alias': 'D', 'table': 'PIMSCS.TIPO_MATURAC', 'alias': 'E', 'table': 'PIMSCS.UPNIVEL1', 'alias': 'F', 'table': 'PIMSCS.FORNECS', 'alias': 'G'])], 'alias': None)], 'alias': 'A'), ('LEFT JOIN', 'query': [('FROM', ['table': 'PIMSCS.HISTPREPRO', 'alias': 'A'])], 'alias': 'B'), ('LEFT JOIN', 'query': [('FROM', ['table': 'PIMSCS.UPNIVEL3', 'alias': 'A']), ('LEFT JOIN', ['table': 'PIMSCS.SAFRUPNIV3', 'alias': 'B'])], 'alias': 'C')], 'alias': 'B')], 'alias': None

结果是一个嵌套字典,用于存储基本查询信息,包括别名,以及所有后续的joins、子查询和表。

【讨论】:

以上是关于使用 Python sqlparse 获取查询树/层次结构的主要内容,如果未能解决你的问题,请参考以下文章

Python如何解析SQL语句并转换为Python对象,SQLParse类库的使用

python sqlparse模块

基于Python-sqlparse的SQL字段血缘追踪解析实现

python3.7.2 安装django 遇到的一些问题

EmEditor 宏汇集

Spark SQL源码解析Antlr4解析Sql并生成树