database_info.py 3.4 KB
# coding=utf-8
#author:        4N
#createtime:    2021/3/9
#email:         nheweijun@sina.com
from app.models import Database,DES
from contextlib import closing
from sqlalchemy import create_engine,or_

from app.models import Database,Catalog,Table


from app.util.component.ApiTemplate import ApiTemplate


class Api(ApiTemplate):
    api_name = "获取数据库信息"
    def process(self):
        # 返回结果
        res = {}
        try:
            # 业务逻辑
            database_guid = self.para.get("guid")
            table_type = self.para.get("table_type")
            catalogs = Catalog.query.filter_by(database_guid=database_guid).all()

            tree_origin = []
            for cata in catalogs:
                cata_json = {}
                cata_json["type"] = "catalog"
                cata_json["guid"] = cata.guid
                cata_json["name"] = cata.name
                cata_json["pguid"] = cata.pguid
                cata_json["children"] = []
                tree_origin.append(cata_json)

                #挂接表
                tables =Table.query.filter_by(database_guid=database_guid,catalog_guid=cata.guid).filter(Table.table_type != 0)
                if table_type:
                    tables = tables.filter_by(table_type=table_type)
                tables = tables.order_by(Table.name).all()

                for table in tables:
                    table_json= {}
                    table_json["name"]=table.name
                    table_json["alias"] = table.alias
                    table_json["guid"] = table.guid
                    table_json["type"] = "table"
                    table_json["table_type"] = table.table_type
                    cata_json["children"].append(table_json)

            for cata in tree_origin:
                cata_pguid = cata["pguid"]
                if not cata_pguid == "0":
                    for c in tree_origin:
                        if c["guid"].__eq__(cata_pguid):
                            c["children"].append(cata)
            res["data"] = [cata for cata in tree_origin if cata["pguid"].__eq__("0")]

            # 挂接表
            tables = Table.query.filter_by(database_guid=database_guid, catalog_guid=None).filter(
                Table.table_type != 0)
            if table_type:
                tables = tables.filter_by(table_type=table_type)
            tables = tables.order_by(Table.name).all()

            for table in tables:
                table_json = {}
                table_json["name"] = table.name
                table_json["alias"] = table.alias
                table_json["guid"] = table.guid
                table_json["type"] = "table"
                table_json["table_type"] = table.table_type
                res["data"].append(table_json)

            res["result"] = True
        except Exception as e:
            raise e
        return res

    api_doc={
    "tags":["数据库接口"],
    "parameters":[
        {"name": "guid",
         "in": "formData",
         "type": "string", "description": "数据库guid"},
        {"name": "table_type",
         "in": "formData",
         "type": "int", "description": "表类型","enum":[1,2,3]}
    ],
    "responses":{
        200:{
            "schema":{
                "properties":{
                }
            }
            }
        }
    }