mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-08 01:06:59 +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
|
||||
|
||||
@@ -7,4 +7,5 @@ export const datasourceApi = {
|
||||
update: (data: any) => request.post('/datasource/update', data),
|
||||
delete: (id: Number) => request.post(`/datasource/delete/${id}`),
|
||||
getTables: (id: Number) => request.post(`/datasource/getTables/${id}`),
|
||||
getFields: (id: Number, table_name: string) => request.post(`/datasource/getFields/${id}/${table_name}`),
|
||||
}
|
||||
@@ -35,7 +35,7 @@
|
||||
</div>
|
||||
<div class="connection-status" :class="`${getStatus(ds.status)}`">{{ ds.status }}</div>
|
||||
<div class="connection-actions">
|
||||
<el-button class="action-btn" circle @click="getTables(ds.id)"/>
|
||||
<el-button class="action-btn" @click="getTables(ds.id)">Info</el-button>
|
||||
<el-button type="primary" class="action-btn" circle @click="editDs(ds)" :icon="IconOpeEdit"/>
|
||||
<el-button type="danger" class="action-btn" circle @click="deleteDs(ds)" :icon="IconOpeDelete"/>
|
||||
</div>
|
||||
@@ -116,7 +116,11 @@ const deleteDs = (item: any) => {
|
||||
}
|
||||
|
||||
const getTables = (id: Number) => {
|
||||
datasourceApi.getTables(id).then((res) => {
|
||||
// datasourceApi.getTables(id).then((res) => {
|
||||
// console.log(res)
|
||||
// })
|
||||
|
||||
datasourceApi.getFields(id,'core_dataset_table').then((res) => {
|
||||
console.log(res)
|
||||
})
|
||||
}
|
||||
@@ -233,6 +237,7 @@ onMounted(() => {
|
||||
right: 20px;
|
||||
display: flex;
|
||||
.action-btn {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
:hover .action-btn{
|
||||
|
||||
Reference in New Issue
Block a user