#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2023/4/9 14:41 # @Author : old tom # @File : metadata_warehouse.py # @Project : futool-tiny-datahub # @Desc : 元数据存储 from common.futool.core.fu_id import id_gen from datahub.metadata.metadatadao.metadata_dao import MetadataDao from datahub.local_db_conf import local_conn from datahub.metadata.constant.metadata_constant import MetaDataObjType class MetadataWareHouse(object): """ 元数据仓库 """ def __init__(self, source_id): self.source_id = source_id self.dao = MetadataDao(local_conn) def save_metadata_obj(self, objs, obj_type): """ 保存元数据对象 :param obj_type: 元数据类型 :param objs: :return: """ data = [(id_gen.get_id(), self.source_id, obj_type, x[0], x[1]) for x in objs] return self.dao.save_metadata_obj(data) def save_metadata_obj_detail(self, details): """ 保存元数据对象明细 :param details: :return: """ return self.dao.save_metadata_create(details) def save_metadata_obj_field(self, fields): """ 保存字段 :param fields: :return: """ return self.dao.save_table_fields(fields) def query_metadata(self, obj_type=None): """ 查询元数据 :param obj_type: :return: """ return self.dao.query_metadata_by_type(obj_type) def query_metadata_name(self, obj_type=None): """ 查询元数据名称 :param obj_type: :return: """ return self.dao.query_metadata_name_by_type(obj_type) def query_metadata_id_name(self, obj_type=None): """ 查询元数据ID及名称 :param obj_type: :return: """ return self.dao.query_metadata_id(obj_type) def query_view_create(self, meta_id): """ 查询视图创建语句 :param meta_id: :return: """ return self.dao.query_metadata_create(MetaDataObjType.View.value, meta_id, self.source_id) def query_view_create_by_name(self, view_name): """ 视图查询创建语句 :param view_name: :return: """ return self.dao.query_metadata_create_by_name(view_name, self.source_id) def query_procedure_create(self, meta_id): """ 查询存储过程创建语句 :param meta_id: :return: """ return self.dao.query_metadata_create(MetaDataObjType.Procedure.value, meta_id, self.source_id) def query_metadata_type(self, meta_id): """ 查询元数据类型 :param meta_id: :return: """ return self.dao.query_metadata_type(meta_id, self.source_id) def query_metadata_type_by_name(self, meta_name): """ 根据名称查询元数据类型 :param meta_name: 表名、视图名、存储过程名 :return: """ return self.dao.query_metadata_type_by_name(meta_name, self.source_id) def is_table(self, table_name): return self.query_metadata_type_by_name(table_name) == MetaDataObjType.Table.value def is_view(self, view_name): return self.query_metadata_type_by_name(view_name) == MetaDataObjType.View.value