This commit is contained in:
yumoqing 2025-12-05 17:51:50 +08:00
parent d6a2098f13
commit 5629a79c48
4 changed files with 88 additions and 6 deletions

View File

@ -2,10 +2,14 @@
数据库表用一个json格式文件或数据来定义具体规范如下 数据库表用一个json格式文件或数据来定义具体规范如下
{ {
"name" "summary":[ # 仅一条记录
"title" {
"primary" "name" # 表名
"catelog" # 可选项entity relation dimession indication "title" # 表标题
"primary" # 主键,等于"id"所有表的主键均为id
"catelog" # 可选项entity relation dimession indication
}
]
"fields":[ # 字段 "fields":[ # 字段
{ {
"name" "name"

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name=xls2ddl name=xls2ddl
version = 1.0.4 version = 1.1.0
description = a xlsx file to database ddl converter description = a xlsx file to database ddl converter
author = "yu moqing" author = "yu moqing"
author_email = "yumoqing@gmail.com" author_email = "yumoqing@gmail.com"
@ -15,6 +15,7 @@ install_requires =
[options.entry_points] [options.entry_points]
console_scripts = console_scripts =
json2ddl = xls2ddl.json2ddl:main
xls2ddl = xls2ddl.xls2ddl:main xls2ddl = xls2ddl.xls2ddl:main
xls2ui = xls2ddl.xls2ui:main xls2ui = xls2ddl.xls2ui:main
json2xlsx = xls2ddl.json2xlsx:main json2xlsx = xls2ddl.json2xlsx:main

71
xls2ddl/json2ddl.py Normal file
View File

@ -0,0 +1,71 @@
# -*- coding:utf-8 -*-
import sys
import io
import sys
from traceback import print_exc
import codecs
import json
from sqlor.ddl_template_sqlserver import sqlserver_ddl_tmpl
from sqlor.ddl_template_mysql import mysql_ddl_tmpl
from sqlor.ddl_template_oracle import oracle_ddl_tmpl
from sqlor.ddl_template_postgresql import postgresql_ddl_tmpl
from appPublic.myTE import MyTemplateEngine
from appPublic.folderUtils import listFile
from xls2ddl.xlsxData import CRUDData, xlsxFactory
tmpls = {
"sqlserver":sqlserver_ddl_tmpl,
"mysql":mysql_ddl_tmpl,
"oracle":oracle_ddl_tmpl,
"postgresql":postgresql_ddl_tmpl
}
def xls2ddl(xlsfile,dbtype):
data = None
if xlsfile.endswith('.json'):
with codecs.open(xlsfile,'r','utf-8') as f:
data = json.load(f)
if data is None:
print(xlsfile, 'not data return')
return
tmpl = tmpls.get(dbtype.lower())
if tmpl is None:
raise Exception('%s database not implemented' % dbtype)
e = MyTemplateEngine([])
s = e.renders(tmpl,data)
return s
def model2ddl(folder,dbtype):
ddl_str = ''
for f in listFile(folder, suffixs=['json']):
try:
ddl_str += f'\n-- {f}\n'
s = xls2ddl(f,dbtype)
ddl_str = f"{ddl_str}\n{s}\n"
except Exception as e:
print('Exception:',e,'f=',f)
print_exc()
return ddl_str
def main():
##解决windows 终端中输出中文出现
# UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 20249
# 错误
# BEGIN
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
sys.stdout.reconfigure(encoding='utf-8')
#
# END
if len(sys.argv) < 3:
print('Usage:%s dbtype folder' % sys.argv[0])
sys.exit(1)
s = model2ddl(sys.argv[2], sys.argv[1])
print(s)
if __name__ == '__main__':
main()

View File

@ -23,7 +23,7 @@ usage:
xls2crud.py dbname models_dir uidir xls2crud.py dbname models_dir uidir
""" """
def build_dbdesc(models_dir: str) -> dict: def build_dbdesc(models_dir: list) -> dict:
print(f'{models_dir=}') print(f'{models_dir=}')
mdirs = [] mdirs = []
if isinstance(models_dir, list): if isinstance(models_dir, list):
@ -38,6 +38,12 @@ def build_dbdesc(models_dir: str) -> dict:
d = x.get_data() d = x.get_data()
tbname = d.summary[0].name tbname = d.summary[0].name
db_desc.update({tbname:d}) db_desc.update({tbname:d})
for f in listFile(models_dir, suffixs=['.json']):
print(f'{f} handle ...')
with codecs.open(f, 'r', 'utf-8') as fh:
d = json.load(fh)
tbname = d['summary'][0]['name']
db_desc.update({tbname:d})
return db_desc return db_desc
def subtable2toolbar(desc): def subtable2toolbar(desc):