SQL里提取依赖表-py

一个路径可能会产出多个表,1->n
输入:带sql的文件绝对路径,一行一个路径
输出:文件名\t依赖的表*

# coding=utf-8
import re

def getTable(shName,linesql):
    search_index = 0
    while len(linesql) > 12 and '_' in linesql and ('from ' in linesql or 'join ' in linesql):
        # 从第一个from或join截取
        if linesql.find('from ') >= 0 and linesql.find('join ') >= 0:
            from_index = linesql.index('from ')
            join_index = linesql.index('join ')
            search_index = join_index if join_index < from_index else from_index
        elif linesql.find('from ') >= 0:
            search_index = linesql.index('from ')
        elif linesql.find('join ') >= 0:
            search_index = linesql.index('join ')
        else:
            return
        # print(search_index)
        linesql = linesql[search_index:]
        # 判断是否空格后是表
        pattern = re.compile(r'(?:from|join)\s+(\S+\_*)\s*')
        linesqlli = pattern.findall(linesql)
        for table in linesqlli:
            if table.find('(') == -1 and table.find('_') >=0:
                if table.find('.') == -1:
                    table = 'portal.'+table
                # print(table.lower())
                with open(r'D:\MyDesk\已迁移的文件依赖.txt', encoding='utf-8', mode='a') as rf:
                    rf.write(shName+'\t'+table.lower()+'\n')
            else:continue
        linesql = linesql[linesql.find(table)+len(table)+1:]
        rf.close()

def catFile(filePath):
    linestr = ''
    # 读取每一行 合为一行
    break_pattern = r'(\s|\n|\r|\t)+'
    sh_name = 'portal.'+re.sub(r'D:\\GitProjects\\offlineexperiment\\dev\\hive.*\\sh_','',filePath).replace('.sh','').lower()
    with open(filePath, encoding='utf-8', mode='r') as sf:
        for line in sf:
            line = re.sub(break_pattern,' ',line.replace('\n', ' '))
            linestr += line
    getTable(sh_name,linestr)


if __name__ == '__main__':
    # 准备文件路径列表
    sh_list = []
    with open(r'D:\MyDesk\已迁移的文件.txt', encoding='utf-8', mode='r') as path_file:
        for path in path_file:
            sh_list.append(path.replace('\n', ''))
    path_file.close()
    for sh in sh_list:
        catFile(sh)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春马与夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值