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.

118 lines
3.7 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/4/27 6:21
# @Author : old tom
# @File : pdman_reader.py
# @Project : py-xltpl
# @Desc : 读取pdman 文件
from futool.core.fh_file_path import exist
import json
class PDmanReader(object):
DATA_TYPE_MAPPING = {
'oracle': {
'DefaultString': 'VARCHAR2(255)',
'IdOrKey': 'VARCHAR2(32)',
'Name': 'VARCHAR2(90)',
'Int': 'NUMBER(38,0)',
'Double': 'NUMBER(24,6)',
'Money': 'NUMBER(24,6)',
'DateTime': 'DATE',
'YesNo': 'VARCHAR2(1)',
'Dict': 'VARCHAR2(32)',
'DescText': 'VARCHAR2(900)'
}
}
class PDfileNotExistError(Exception):
"""
PDMAN 文件不存在
"""
def __init__(self, msg):
Exception.__init__(self, msg)
class ModuleNotExistError(Exception):
"""
模块不存在
"""
def __init__(self, msg):
Exception.__init__(self, msg)
def __init__(self, pdman_file):
if not exist(pdman_file):
raise self.PDfileNotExistError(msg=f'{pdman_file}文件不存在')
with open(pdman_file, 'r', encoding='utf-8') as f:
self.json_reader = json.loads(f.read())
self.modules = self._load_all_module()
self.domains = self._load_data_domains()
def read_by_module(self, module_name):
"""
根据模块读取
:param module_name: 模块名称
:return:
"""
if module_name not in self.modules.keys():
raise self.ModuleNotExistError(f'{module_name}模块不存在')
# 获取模块与表关联关系
entry_ids = self.modules[module_name]
tables = {}
for entry in self.json_reader['entities']:
if entry['id'] in entry_ids:
# 读取表名,字段
tables[entry['defKey']] = {
'table_name': entry['defName'],
'table_en_name': entry['defKey'],
'rows': self._fields_convert(entry['fields'])
}
return tables
def _load_all_module(self):
"""
加载模块索引
:return:
"""
modules = self.json_reader['viewGroups']
module_dict = {}
for m in modules:
module_dict[m['defKey']] = m['refEntities']
return module_dict
def _load_data_domains(self, db_type='oracle'):
"""
加载数据域
:return:
"""
domains_dict = {}
domains = self.json_reader['domains']
for d in domains:
domains_dict[d['id']] = self.DATA_TYPE_MAPPING[db_type][d['defKey']]
return domains_dict
#
def _fields_convert(self, fields):
rt = []
for f in fields:
if len(f['domain']) > 0:
f['type'] = self.domains[f['domain']]
elif len(str(f['len'])) > 0 and len(str(f['scale'])) == 0:
f['type'] = f['type'] + '(' + str(f['len']) + ')'
elif len(str(f['len'])) > 0 and len(str(f['scale'])) > 0:
f['type'] = f['type'] + '(' + str(f['len']) + ',' + str(f['scale']) + ')'
rt.append({'enName': f['defKey'],
'chName': f['defName'] + ';' + f['comment'] if len(f['comment']) > 0 else f['defName'],
'type': f['type'], 'nullable': 'Y' if f['notNull'] else 'N'})
return rt
#
if __name__ == '__main__':
pdfile = r'D:\文档\工作\ATD\2023上半年\项目\昭通OA\表结构\昭通OA.pdma.json'
reader = PDmanReader(pdfile)
all_table = reader.read_by_module('TEST')
print(all_table['test'])