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.

192 lines
7.1 KiB

2 years ago
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/4/9 8:27
# @Author : old tom
# @File : metadata_dao.py
# @Project : futool-tiny-datahub
# @Desc :
from common.fudb.connectors.connector_factory import ConnFactory
from common.fudb.dbapis.fu_dao import BaseDao
class MetadataDao(BaseDao):
def __init__(self, connector: ConnFactory):
super().__init__(connector)
def query_all_tables(self, sql):
"""
查询所有表
:param sql:
:return:
"""
return self.query_all(sql)
2 years ago
def query_all_views(self, sql):
"""
查询所有视图
:param sql:
:return:
"""
return self.query_all(sql)
def query_all_procedure(self, sql):
"""
查询所有存储过程
:param sql:
:return:
"""
return self.query_all(sql)
def query_procedure_detail(self, sql):
"""
查询视图及存储过程明细
:param sql:
:return:
"""
rt = self.query_all(sql)
# 合并为完整SQL
return 'create or replace ' + (''.join([str(x[0]).replace('\n', '\r') for x in rt]))
def query_view_detail(self, view_name, sql):
"""
查询视图创建语句
:param view_name:
:param sql:
:return:
"""
return f'CREATE OR REPLACE VIEW {view_name} AS ' + '\r\n' + self.query_one(sql)[0]
def query_table_field(self, sql):
"""
查询表字段
:param sql:
:return:
"""
return self.query_all(sql)
def save_table_fields(self, data):
"""
保存表字段
:param data:
:return:
"""
return self.batch_insert(
sql_tpl='insert into metadata_object_field(meta_id,field_name,field_ch_name,order_num,field_type,nullable,default_value,field_length) values %s',
data=data, batch_size=1500)
def save_metadata_obj(self, objs):
"""
保存元数据
:param objs:
:return:
"""
return self.batch_insert(
sql_tpl='insert into metadata_object(meta_id,source_id,meta_type,meta_name,meta_ch_name,version_code) values %s',
data=objs, batch_size=1500)
def save_metadata_create(self, details):
return self.batch_insert(
sql_tpl='insert into metadata_object_create(meta_id,create_sql) values %s',
data=details, batch_size=1500)
@staticmethod
def _get_version_sql(source_id, version_code=None):
return ' and ' + (f'version_code={version_code}' if version_code \
else f"version_code=(select max(version_code) from metadata_object_version_record where source_id='{source_id}')")
def query_metadata_by_type(self, obj_type):
return self.query_all(
f"select * from metadata_object where meta_type='{obj_type}'") if obj_type else self.query_all(
'select * from metadata_object')
def query_metadata_name_by_type(self, obj_type, source_id, version_code=None):
"""
查询元数据名称
:param version_code: 版本号,默认查最新
:param source_id: 源ID
:param obj_type: 类型
:return:
"""
sql = f"select meta_name from metadata_object where source_id='{source_id}' and meta_type='{obj_type}'" + self._get_version_sql(
source_id, version_code)
return [x[0] for x in self.query_all(sql)]
def query_metadata_id_name(self, obj_type, source_id, version_code=None):
"""
查询元数据meta_id及meta_name
:param obj_type:
:param source_id:
:param version_code: 版本号
:return:
"""
sql = f"select meta_id,meta_name from metadata_object where meta_type='{obj_type}'" \
+ self._get_version_sql(source_id, version_code)
return self.query_all(sql)
def query_metadata_create(self, obj_type, meta_id, source_id):
"""
查询创建语句
:param obj_type:
:param meta_id:
:param source_id:
:return:
"""
# metadata_object_create与metadata_object非强外键关系所以加入source_id保证查询结果正确
sql = f"select create_sql from metadata_object_create where meta_id in (select meta_id from metadata_object where meta_id={meta_id} and source_id='{source_id}' and meta_type='{obj_type}')"
create_sql = self.query_one(sql)
return create_sql[0] if create_sql and len(create_sql) > 0 else ''
def query_metadata_create_by_name(self, meta_name, source_id, version_code=None):
"""
名称查询创建语句
:param version_code: 版本号
:param meta_name:
:param source_id:
:return:
"""
sql = f"select create_sql from metadata_object_create where meta_id=(select meta_id from metadata_object where meta_name='{meta_name}' and source_id='{source_id}'" + self._get_version_sql(
source_id, version_code) + ")"
create_sql = self.query_one(sql)
return create_sql[0] if create_sql and len(create_sql) > 0 else ''
def query_metadata_type(self, meta_id, source_id, version_code=None):
"""
查询元数据类型
:param version_code: 版本号
:param meta_id:
:param source_id:
:return:
"""
sql = f"select meta_type from metadata_object where meta_id={meta_id} and source_id='{source_id}'" \
+ self._get_version_sql(source_id, version_code)
rt = self.query_one(sql)
return rt[0] if rt and len(rt) > 0 else None
def query_metadata_type_by_name(self, meta_name, source_id, version_code=None):
"""
根据元数据名称查询类型
:param version_code: 版本号
:param meta_name: 元数据名
:param source_id: 源ID
:return:
"""
sql = f"select meta_type from metadata_object where meta_name='{meta_name}' and source_id='{source_id}'" \
+ self._get_version_sql(source_id, version_code)
rt = self.query_one(sql)
return rt[0] if rt and len(rt) > 0 else None
def remove_metadata_by_version(self, source_id, version_code):
"""
根据版本号删除元数据
:param source_id:
:param version_code:
:return:
"""
del_version_sql = f"delete from metadata_object_version_record where source_id = '{source_id}' AND version_code = {version_code}"
del_metadata_obj_field_detail = f"delete from metadata_object_field where meta_id in (SELECT meta_id FROM metadata_object WHERE source_id = '{source_id}' AND version_code = {version_code})"
del_metadata_obj_create_detail = f"delete from metadata_object_create where meta_id in (SELECT meta_id FROM metadata_object WHERE source_id = '{source_id}' AND version_code = {version_code})"
del_metadata_obj = f"delete from metadata_object where source_id = '{source_id}' AND version_code = {version_code}"
self.execute_update_tx(
[del_version_sql, del_metadata_obj_field_detail, del_metadata_obj_create_detail, del_metadata_obj])