mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-10 18:26:55 +08:00
feat: get fields api
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from fastapi import APIRouter
|
||||
from ..crud.datasource import get_datasource_list, check_status, create_ds, update_ds, delete_ds, getTables
|
||||
from ..crud.datasource import get_datasource_list, check_status, create_ds, update_ds, delete_ds, getTables, getFields
|
||||
from common.core.deps import SessionDep
|
||||
from ..models.datasource import CoreDatasource
|
||||
|
||||
@@ -34,3 +34,8 @@ async def delete(session: SessionDep, id: int):
|
||||
@router.post("/getTables/{id}")
|
||||
async def get_tables(session: SessionDep, id: int):
|
||||
return getTables(session, id)
|
||||
|
||||
|
||||
@router.post("/getFields/{id}/{table_name}")
|
||||
async def get_tables(session: SessionDep, id: int, table_name: str):
|
||||
return getFields(session, id, table_name)
|
||||
|
||||
@@ -4,7 +4,7 @@ import datetime
|
||||
from common.core.deps import SessionDep
|
||||
import json
|
||||
from ..utils.utils import aes_decrypt
|
||||
from apps.db.db import get_session, get_tables
|
||||
from apps.db.db import get_session, get_tables, get_fields
|
||||
from sqlalchemy import text
|
||||
|
||||
|
||||
@@ -62,3 +62,10 @@ def getTables(session: SessionDep, id: int):
|
||||
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration)))
|
||||
tables = get_tables(conf, ds)
|
||||
return tables
|
||||
|
||||
|
||||
def getFields(session: SessionDep, id: int, table_name: str):
|
||||
ds = session.exec(select(CoreDatasource).where(CoreDatasource.id == id)).first()
|
||||
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration)))
|
||||
fields = get_fields(conf, ds, table_name)
|
||||
return fields
|
||||
|
||||
@@ -32,3 +32,14 @@ class TableSchema:
|
||||
|
||||
tableName: str
|
||||
tableRemark: str
|
||||
|
||||
|
||||
class ColumnSchema:
|
||||
def __init__(self, attr1, attr2, attr3):
|
||||
self.fieldName = attr1
|
||||
self.fieldType = attr2
|
||||
self.fieldRemark = attr3
|
||||
|
||||
fieldName: str
|
||||
fieldType: str
|
||||
fieldRemark: str
|
||||
|
||||
+19
-5
@@ -1,6 +1,6 @@
|
||||
from sqlalchemy import create_engine, text, Result
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from apps.datasource.models.datasource import DatasourceConf, CoreDatasource, TableSchema
|
||||
from apps.datasource.models.datasource import DatasourceConf, CoreDatasource, TableSchema, ColumnSchema
|
||||
import urllib.parse
|
||||
from typing import Any
|
||||
|
||||
@@ -22,8 +22,8 @@ def get_tables(conf: DatasourceConf, ds: CoreDatasource):
|
||||
result: Result[Any]
|
||||
if ds.type == "mysql":
|
||||
sql = f"""SELECT
|
||||
TABLE_NAME AS `Table Name`,
|
||||
TABLE_COMMENT AS `Table Comment`
|
||||
TABLE_NAME,
|
||||
TABLE_COMMENT
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
@@ -35,7 +35,21 @@ def get_tables(conf: DatasourceConf, ds: CoreDatasource):
|
||||
return res_list
|
||||
|
||||
|
||||
def get_fields(conf: DatasourceConf, ds: CoreDatasource):
|
||||
def get_fields(conf: DatasourceConf, ds: CoreDatasource, table_name: str):
|
||||
session = get_session(conf, ds)
|
||||
result: Result[Any]
|
||||
pass
|
||||
if ds.type == "mysql":
|
||||
sql = f"""SELECT
|
||||
COLUMN_NAME,
|
||||
DATA_TYPE,
|
||||
COLUMN_COMMENT
|
||||
FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE
|
||||
TABLE_SCHEMA = '{conf.database}' AND
|
||||
TABLE_NAME = '{table_name}';"""
|
||||
result = session.execute(text(sql))
|
||||
|
||||
res = result.fetchall()
|
||||
res_list = [ColumnSchema(*item) for item in res]
|
||||
return res_list
|
||||
|
||||
Reference in New Issue
Block a user