Skip to content

Commit 273479a

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 8acadf7 + cfffafa commit 273479a

File tree

5 files changed

+92
-50
lines changed

5 files changed

+92
-50
lines changed

backend/apps/datasource/api/datasource.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from fastapi import APIRouter
22
from ..crud.datasource import get_datasource_list, check_status, create_ds, update_ds, delete_ds, getTables, getFields, \
3-
execSql, update_table_and_fields, getTablesByDs
3+
execSql, update_table_and_fields, getTablesByDs, chooseTables
44
from common.core.deps import SessionDep
5-
from ..models.datasource import CoreDatasource, CreateDatasource, EditObj
5+
from ..models.datasource import CoreDatasource, CreateDatasource, EditObj, CoreTable
66
from ..crud.table import get_tables_by_ds_id
77
from ..crud.field import get_fields_by_table_id
8+
from typing import List
89

910
router = APIRouter(tags=["datasource"], prefix="/datasource")
1011

@@ -24,6 +25,11 @@ async def add(session: SessionDep, ds: CreateDatasource):
2425
return create_ds(session, ds)
2526

2627

28+
@router.post("/chooseTables/{id}")
29+
async def choose_tables(session: SessionDep, id: int, tables: List[CoreTable]):
30+
chooseTables(session, id, tables)
31+
32+
2733
@router.post("/update", response_model=CoreDatasource)
2834
async def update(session: SessionDep, ds: CoreDatasource):
2935
return update_ds(session, ds)
@@ -54,12 +60,12 @@ async def exec_sql(session: SessionDep, id: int, sql: str):
5460
return execSql(session, id, sql)
5561

5662

57-
@router.get("/tableList/{id}")
63+
@router.post("/tableList/{id}")
5864
async def table_list(session: SessionDep, id: int):
5965
return get_tables_by_ds_id(session, id)
6066

6167

62-
@router.get("/fieldList/{id}")
68+
@router.post("/fieldList/{id}")
6369
async def field_list(session: SessionDep, id: int):
6470
return get_fields_by_table_id(session, id)
6571

backend/apps/datasource/crud/datasource.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def create_ds(session: SessionDep, create_ds: CreateDatasource):
5050
return ds
5151

5252

53+
def chooseTables(session: SessionDep, id: int, tables: List[CoreTable]):
54+
ds = session.query(CoreDatasource).filter(CoreDatasource.id == id).first()
55+
sync_table(session, ds, tables)
56+
57+
5358
def update_ds(session: SessionDep, ds: CoreDatasource):
5459
ds.id = int(ds.id)
5560
status = check_status(session, ds)

frontend/src/api/datasource.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export const datasourceApi = {
1010
getTablesByConf: (data: any) => request.post('/datasource/getTablesByConf', data),
1111
getFields: (id: Number, table_name: string) => request.post(`/datasource/getFields/${id}/${table_name}`),
1212
execSql: (id: Number, sql: string) => request.post(`/datasource/execSql/${id}/${sql}`),
13+
chooseTables: (id: Number, data: any) => request.post(`/datasource/chooseTables/${id}`, data),
14+
tableList: (id: Number) => request.post(`/datasource/tableList/${id}`)
1315
}

frontend/src/views/ds/form.vue

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<div style="display: flex;justify-content: flex-end;margin-top: 20px;">
6262
<el-button @click="close">Cancel</el-button>
6363
<el-button v-show="!isCreate && !isEditTable" @click="check">Test Connect</el-button>
64-
<el-button v-show="active === 1 && isCreate" type="primary" @click="next">Next</el-button>
64+
<el-button v-show="active === 1 && isCreate" type="primary" @click="next(dsFormRef)">Next</el-button>
6565
<el-button v-show="active === 2 && isCreate" @click="preview">Preview</el-button>
6666
<el-button v-show="active === 2 || !isCreate" type="primary" @click="save(dsFormRef)">Save</el-button>
6767
</div>
@@ -113,46 +113,58 @@ const close = () => {
113113
isCreate.value = true
114114
active.value = 1
115115
isEditTable.value = false
116+
checkList.value = []
117+
tableList.value = []
116118
}
117119
118120
const open = (item: any, editTable: boolean = false) => {
119-
if (editTable) {
120-
active.value = 2
121-
isEditTable.value = true
121+
isEditTable.value = false
122+
if (item) {
122123
isCreate.value = false
123-
// request tables
124+
form.value.id = item.id
125+
form.value.name = item.name
126+
form.value.description = item.description
127+
form.value.type = item.type
128+
form.value.configuration = item.configuration
129+
if(item.configuration) {
130+
const configuration = JSON.parse(decrypted(item.configuration))
131+
config.value.host = configuration.host
132+
config.value.port = configuration.port
133+
config.value.username = configuration.username
134+
config.value.password = configuration.password
135+
config.value.database = configuration.database
136+
}
137+
138+
if (editTable) {
139+
active.value = 2
140+
isEditTable.value = true
141+
isCreate.value = false
142+
// request tables and check tables
143+
datasourceApi.tableList(item.id).then((res) => {
144+
checkList.value = res.map((ele:any) => {return ele.table_name})
145+
datasourceApi.getTablesByConf(form.value).then((res) => {
146+
tableList.value = res
147+
})
148+
})
149+
}
124150
} else {
151+
isCreate.value = true
125152
isEditTable.value = false
126-
if (item) {
127-
isCreate.value = false
128-
form.value.id = item.id
129-
form.value.name = item.name
130-
form.value.description = item.description
131-
form.value.type = item.type
132-
if(item.configuration) {
133-
const configuration = JSON.parse(decrypted(item.configuration))
134-
config.value.host = configuration.host
135-
config.value.port = configuration.port
136-
config.value.username = configuration.username
137-
config.value.password = configuration.password
138-
config.value.database = configuration.database
139-
}
140-
} else {
141-
isCreate.value = true
142-
form.value = {
143-
name:'',
144-
description:'',
145-
type:'mysql',
146-
configuration: ''
147-
}
148-
config.value = {
149-
driver:'',
150-
host:'',
151-
port:0,
152-
username:'',
153-
password:'',
154-
database:'',
155-
}
153+
checkList.value = []
154+
tableList.value = []
155+
form.value = {
156+
name:'',
157+
description:'',
158+
type:'mysql',
159+
configuration: ''
160+
}
161+
config.value = {
162+
driver:'',
163+
host:'',
164+
port:0,
165+
username:'',
166+
password:'',
167+
database:'',
156168
}
157169
}
158170
dialogVisible.value = true
@@ -162,6 +174,10 @@ const save = async(formEl: FormInstance | undefined) => {
162174
if(!formEl) return
163175
await formEl.validate((valid) => {
164176
if (valid) {
177+
const list = tableList.value
178+
.filter((ele: any) => {return checkList.value.includes(ele.tableName)})
179+
.map((ele: any) => {return {"table_name": ele.tableName, "table_comment": ele.tableComment}})
180+
165181
form.value.configuration = encrypted(JSON.stringify({
166182
host:config.value.host,
167183
port:config.value.port,
@@ -179,9 +195,12 @@ const save = async(formEl: FormInstance | undefined) => {
179195
})
180196
} else {
181197
// save table and field
198+
datasourceApi.chooseTables(form.value.id, list).then(() => {
199+
close()
200+
})
182201
}
183202
} else {
184-
form.value.tables = [{"table_name":"core_dataset_table"},{"table_name":"pre_user"}]// todo test
203+
form.value.tables = list
185204
datasourceApi.add(form.value).then((res: any) => {
186205
console.log(res)
187206
close()
@@ -215,15 +234,20 @@ const check = () => {
215234
})
216235
}
217236
218-
const next = () => {
219-
// check status if success do next
220-
buildConf()
221-
datasourceApi.check(form.value).then((res: boolean) => {
222-
if(res) {
223-
active.value++
224-
// request tables
225-
datasourceApi.getTablesByConf(form.value).then((res) => {
226-
tableList.value = res
237+
const next = async(formEl: FormInstance | undefined) => {
238+
if(!formEl) return
239+
await formEl.validate((valid) => {
240+
if (valid) {
241+
// check status if success do next
242+
buildConf()
243+
datasourceApi.check(form.value).then((res: boolean) => {
244+
if(res) {
245+
active.value++
246+
// request tables
247+
datasourceApi.getTablesByConf(form.value).then((res) => {
248+
tableList.value = res
249+
})
250+
}
227251
})
228252
}
229253
})

frontend/src/views/ds/index.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<div class="connection-status" :class="`${getStatus(ds.status)}`">{{ ds.status }}</div>
3737
<div class="connection-actions">
3838
<el-button class="action-btn" circle @click="getTables(ds.id, ds.name)" :icon="List" />
39+
<el-button class="action-btn" circle @click="editTables(ds)" :icon="CreditCard" />
3940
<el-button type="primary" class="action-btn" circle @click="editDs(ds)" :icon="IconOpeEdit"/>
4041
<el-button type="danger" class="action-btn" circle @click="deleteDs(ds)" :icon="IconOpeDelete"/>
4142
</div>
@@ -48,7 +49,7 @@
4849
import IconOpeAdd from '@/assets/svg/operate/ope-add.svg'
4950
import IconOpeEdit from '@/assets/svg/operate/ope-edit.svg'
5051
import IconOpeDelete from '@/assets/svg/operate/ope-delete.svg'
51-
import { Search, List } from '@element-plus/icons-vue'
52+
import { Search, List, CreditCard } from '@element-plus/icons-vue'
5253
import { ref, onMounted } from 'vue'
5354
import DsForm from './form.vue'
5455
import { datasourceApi } from '@/api/datasource'
@@ -97,6 +98,10 @@ const editDs = (item: any) => {
9798
dsForm.value.open(item)
9899
}
99100
101+
const editTables = (item: any) => {
102+
dsForm.value.open(item, true)
103+
}
104+
100105
const deleteDs = (item: any) => {
101106
ElMessageBox.confirm(
102107
'Delete this datasource?',

0 commit comments

Comments
 (0)