#!/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 )
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) 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 )
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 ) :
"""
查询元数据名称
: param obj_type :
: return :
"""
sql = f " select meta_name from metadata_object where meta_type= ' { obj_type } ' " if obj_type else ' select meta_name from metadata_object '
return [ x [ 0 ] for x in self . query_all ( sql ) ]
def query_metadata_id ( self , obj_type ) :
return self . query_all (
f " select meta_id,meta_name from metadata_object where meta_type= ' { obj_type } ' " ) if obj_type else self . query_all (
' select meta_id,meta_name from metadata_object ' )
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保证查询结果正确
create_sql = self . query_one (
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 } ' ) " )
return create_sql [ 0 ] if create_sql and len ( create_sql ) > 0 else ' '
def query_metadata_create_by_name ( self , meta_name , source_id ) :
"""
名称查询创建语句
: param meta_name :
: param source_id :
: return :
"""
create_sql = self . query_one (
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 } ' ) " )
return create_sql [ 0 ] if create_sql and len ( create_sql ) > 0 else ' '
def query_metadata_type ( self , meta_id , source_id ) :
"""
查询元数据类型
: param meta_id :
: param source_id :
: return :
"""
rt = self . query_one (
f " select meta_type from metadata_object where meta_id= { meta_id } and source_id= ' { source_id } ' " )
return rt [ 0 ] if rt and len ( rt ) > 0 else None
def query_metadata_type_by_name ( self , meta_name , source_id ) :
"""
根据元数据名称查询类型
: param meta_name : 元数据名
: param source_id : 源ID
: return :
"""
rt = self . query_one (
f " select meta_type from metadata_object where meta_name= ' { meta_name } ' and source_id= ' { source_id } ' limit 1 " )
return rt [ 0 ] if rt and len ( rt ) > 0 else None