zoukankan      html  css  js  c++  java
  • 使用sqlparse解析table_name,超级强大,支持子查询, left join等

    import sqlparse
    from sqlparse.sql import IdentifierList, Identifier
    from sqlparse.tokens import Keyword, DML
     
     
    # 支持的join方式
    ALL_JOIN_TYPE = ('LEFT JOIN', 'RIGHT JOIN', 'INNER JOIN', 'FULL JOIN', 'LEFT OUTER JOIN', 'FULL OUTER JOIN')
     
     
    def is_subselect(parsed):
        """
        是否子查询
        :param parsed:
        :return:
        """
        if not parsed.is_group:
            return False
        for item in parsed.tokens:
            if item.ttype is DML and item.value.upper() == 'SELECT':
                return True
        return False
     
     
    def extract_from_part(parsed):
        """
        提取from之后模块
        :param parsed:
        :return:
        """
        from_seen = False
        for item in parsed.tokens:
            if from_seen:
                if is_subselect(item):
                    for x in extract_from_part(item):
                        yield x
                elif item.ttype is Keyword:
                    from_seen = False
                    continue
                else:
                    yield item
            elif item.ttype is Keyword and item.value.upper() == 'FROM':
                from_seen = True
     
     
    def extract_join_part(parsed):
        """
        提交join之后模块
        :param parsed:
        :return:
        """
        flag = False
        for item in parsed.tokens:
            if flag:
                if item.ttype is Keyword:
                    flag = False
                    continue
                else:
                    yield item
            if item.ttype is Keyword and item.value.upper() in ALL_JOIN_TYPE:
                flag = True
     
     
    def extract_table_identifiers(token_stream):
        for item in token_stream:
            if isinstance(item, IdentifierList):
                for identifier in item.get_identifiers():
                    yield identifier.get_name()
            elif isinstance(item, Identifier):
                yield item.get_name()
            elif item.ttype is Keyword:
                yield item.value
     
     
    def extract_tables(sql):
        """
        提取sql中的表名(select语句)
        :param sql:
        :return:
        """
        from_stream = extract_from_part(sqlparse.parse(sql)[0])
        join_stream = extract_join_part(sqlparse.parse(sql)[0])
        return list(extract_table_identifiers(from_stream)) + list(extract_table_identifiers(join_stream))
     
     
     
    print(extract_tables("select x1, x2 from a left join b on a.id = b.id right join c on c.id = a.id"))
    print(extract_tables("select x1, x2 from (select x1, x2 from (select x1, x2 from a)) left join b on a.id=b.id"))
    

      

  • 相关阅读:
    截取UIImagePickerController的拍照事件
    Xcode报错:run custom shell script '[cp] copy pods resource
    XCode报错:Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1
    Mac环境下实现alias的重命名命令(永久生效)
    Swift 3.0在集合类数据结构上的一些新变化
    iOS几种简单有效的数组排序方法
    二分法查找、快速排序思想与实现
    iOS10 相册权限
    ios应用版本号设置规则
    iOS白名单设置
  • 原文地址:https://www.cnblogs.com/cherylgi/p/15222042.html
Copyright © 2011-2022 走看看