You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9 KiB

2 years ago
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/4/8 9:05
# @Author : old tom
# @File : metadata_reader.py
# @Project : futool-tiny-datahub
# @Desc : 元数据读取
import abc
import os
2 years ago
from configparser import ConfigParser
from datahub.datasource.datasource_manage import DataSource
2 years ago
from datahub.metadata.metadatadao.metadata_dao import MetadataDao
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
2 years ago
reader_conf = ConfigParser()
reader_conf.read(os.path.join(BASE_DIR, 'reader_conf.ini'))
2 years ago
class AbsMetadataReader(metaclass=abc.ABCMeta):
"""
抽象元数据读取器
"""
def __init__(self, datasource):
self.datasource = datasource
@abc.abstractmethod
def query_tables(self):
"""
查询当前连接下所有表
:return:
"""
pass
@abc.abstractmethod
def query_views(self):
"""
查询当前连接下所有视图
:return:
"""
pass
@abc.abstractmethod
def query_procedure(self):
"""
查询当前连接下所有存储过程
:return:
"""
pass
@abc.abstractmethod
def query_table_fields(self, table):
"""
查询字段
:param table:
:return:
"""
pass
class MetadataReader(AbsMetadataReader):
def __init__(self, datasource: DataSource):
super().__init__(datasource)
self.db_type = datasource.connector.db_type
self.dao = MetadataDao(datasource.connector)
def query_tables(self):
return self.dao.query_all_tables(reader_conf[self.db_type]['tables'])
def query_views(self):
return self.dao.query_all_views(reader_conf[self.db_type]['views'])
def query_procedure(self):
return self.dao.query_all_procedure(reader_conf[self.db_type]['procedure'])
2 years ago
def query_table_fields(self, table):
return self.dao.query_table_field(reader_conf[self.db_type]['table_field'].replace('#$#', table))
def query_metadata_detail(self, obj_name, obj_type):
"""
查询元数据明细
obj_type 为view或procedure 则查询对应创建语句
为table 则查询中文注释
:param obj_name: 对象名称
:param obj_type: 对象类型
:return:
"""
return {
'table': self.dao.query_table_field(
reader_conf[self.db_type][obj_type + '_detail'].replace('#$#', obj_name)),
'view': self.dao.query_view_detail(obj_name,
reader_conf[self.db_type][obj_type + '_detail'].replace('#$#',
obj_name)),
'procedure': self.dao.query_procedure_detail(
reader_conf[self.db_type][obj_type + '_detail'].replace('#$#', obj_name))
}[obj_type]