Skip to content

Commit 3be3b7e

Browse files
committed
feat: datasource
1 parent a10058c commit 3be3b7e

File tree

6 files changed

+115
-67
lines changed

6 files changed

+115
-67
lines changed

backend/apps/datasource/crud/datasource.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ def check_status(session: SessionDep, ds: CoreDatasource):
2020
return True
2121
except Exception as e:
2222
print("Fail:", e)
23-
raise e
23+
return False
2424
finally:
2525
conn.close()
26+
return False
2627

2728

2829
def create_ds(session: SessionDep, ds: CoreDatasource):
2930
ds.create_time = datetime.datetime.now()
30-
ds.status = "Success" # todo check status
31+
status = check_status(session, ds)
32+
ds.status = "Success" if status is True else "Fail"
3133
record = CoreDatasource(**ds.model_dump())
34+
# get tables and fields
35+
if status:
36+
pass
3237
session.add(record)
3338
session.commit()
3439
return ds

backend/apps/datasource/models/datasource.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from sqlalchemy import Column, Text, BigInteger, DateTime, Integer, Identity
33
from datetime import datetime
44
from pydantic import BaseModel
5+
from typing import List
56

67

78
class CoreDatasource(SQLModel, table=True):
@@ -16,6 +17,22 @@ class CoreDatasource(SQLModel, table=True):
1617
status: str = Field(max_length=64, nullable=True)
1718

1819

20+
class TableSchema(BaseModel):
21+
checked: str = True
22+
table_name: str = ''
23+
table_comment: str = ''
24+
custom_comment: str = ''
25+
table_fields: List = []
26+
27+
28+
class FieldSchema(BaseModel):
29+
checked: str = True
30+
field_name: str = ''
31+
field_type: str = ''
32+
field_comment: str = ''
33+
custom_comment: str = ''
34+
35+
1936
class DatasourceConf(BaseModel):
2037
host: str = ''
2138
port: int = 0
@@ -28,18 +45,18 @@ class DatasourceConf(BaseModel):
2845
class TableSchema:
2946
def __init__(self, attr1, attr2):
3047
self.tableName = attr1
31-
self.tableRemark = attr2
48+
self.tableComment = attr2
3249

3350
tableName: str
34-
tableRemark: str
51+
tableComment: str
3552

3653

3754
class ColumnSchema:
3855
def __init__(self, attr1, attr2, attr3):
3956
self.fieldName = attr1
4057
self.fieldType = attr2
41-
self.fieldRemark = attr3
58+
self.fieldComment = attr3
4259

4360
fieldName: str
4461
fieldType: str
45-
fieldRemark: str
62+
fieldComment: str

backend/apps/db/db.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def get_uri(ds: CoreDatasource):
1717
raise 'The datasource type not support.'
1818
return db_url
1919

20+
2021
def get_session(ds: CoreDatasource):
2122
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration)))
2223
db_url: str
@@ -55,21 +56,22 @@ def get_tables(ds: CoreDatasource):
5556
session.close()
5657

5758

58-
def get_fields(ds: CoreDatasource, table_name: str):
59+
def get_fields(ds: CoreDatasource, table_name: str = None):
5960
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration)))
6061
session = get_session(ds)
6162
result: Result[Any]
6263
try:
6364
if ds.type == "mysql":
64-
sql = f"""SELECT
65+
sql1 = f"""SELECT
6566
COLUMN_NAME,
6667
DATA_TYPE,
6768
COLUMN_COMMENT
6869
FROM
6970
INFORMATION_SCHEMA.COLUMNS
7071
WHERE
71-
TABLE_SCHEMA = '{conf.database}' AND
72-
TABLE_NAME = '{table_name}';"""
72+
TABLE_SCHEMA = '{conf.database}'"""
73+
sql2 = f""" AND TABLE_NAME = '{table_name}';""" if table_name is not None and table_name != "" else ";"
74+
sql = sql1 + sql2
7375
result = session.execute(text(sql))
7476

7577
res = result.fetchall()
@@ -99,20 +101,3 @@ def exec_sql(ds: CoreDatasource, sql: str):
99101
if session is not None:
100102
session.close()
101103

102-
def exec_sql(ds: CoreDatasource, sql: str):
103-
ds = session.get(CoreDatasource, id)
104-
session = get_session(ds)
105-
result = session.execute(text(sql))
106-
try:
107-
columns = result.keys()._keys
108-
res = result.fetchall()
109-
result_list = [
110-
{columns[i]: value for i, value in enumerate(tuple_item)}
111-
for tuple_item in res
112-
]
113-
return {"fields": columns, "data": result_list}
114-
finally:
115-
if result is not None:
116-
result.close()
117-
if session is not None:
118-
session.close()

frontend/src/router/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ const router = createRouter({
3838
name: 'ds',
3939
component: ds,
4040
meta: { title: 'Data Connections', icon: 'ds' }
41+
},
42+
{
43+
path: 'dsTable/:dsId/:dsName',
44+
name: 'dsTable',
45+
component: () => import('@/views/ds/TableList.vue'),
46+
meta: { title: 'Data Connections', icon: 'ds' },
47+
props: true
4148
}
4249
]
4350
},

frontend/src/views/ds/TableList.vue

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
<template>
2-
<el-drawer
2+
<div class="layout">
3+
<el-row class="header">
4+
<div class="title">
5+
<el-button text :icon="ArrowLeft" @click="back()" />
6+
{{ props.dsName }}
7+
</div>
8+
<el-button type="primary" @click="save()">
9+
Save
10+
</el-button>
11+
</el-row>
12+
<el-row class="data">
13+
<el-table-v2
14+
:columns="tableColumns"
15+
:data="tableData"
16+
:width="1000"
17+
:height="800"
18+
fixed
19+
/>
20+
</el-row>
21+
<el-drawer
322
v-model="drawer"
423
direction="btt"
524
:destroy-on-close="true"
@@ -11,40 +30,37 @@
1130
</template>
1231
<template #default>
1332
<el-row :gutter="20">
14-
<el-col :span="12">
15-
<el-table-v2
16-
:columns="tableColumns"
17-
:data="tableData"
18-
:width="700"
19-
:height="400"
20-
fixed
21-
/>
22-
</el-col>
23-
<el-col :span="12">
24-
<el-table-v2
25-
:columns="fieldColumns"
26-
:data="fieldData"
27-
:width="700"
28-
:height="400"
29-
fixed
30-
/>
31-
</el-col>
33+
<el-table-v2
34+
:columns="fieldColumns"
35+
:data="fieldData"
36+
:width="1000"
37+
:height="800"
38+
fixed
39+
/>
3240
</el-row>
3341
</template>
3442
<template #footer>
3543
<div style="flex: auto">
36-
<el-button @click="cancelClick">cancel</el-button>
37-
<el-button type="primary" @click="confirmClick">confirm</el-button>
44+
<el-button @click="cancelClick">Cancel</el-button>
45+
<el-button type="primary" @click="confirmClick">Confirm</el-button>
3846
</div>
3947
</template>
4048
</el-drawer>
49+
</div>
4150
</template>
4251

4352
<script lang="tsx" setup>
4453
import { ref } from 'vue'
4554
import { datasourceApi } from '@/api/datasource'
4655
import { ElButton } from 'element-plus'
4756
import { h } from 'vue'
57+
import { onMounted } from 'vue'
58+
import { ArrowLeft } from '@element-plus/icons-vue'
59+
60+
const props = defineProps({
61+
dsId: { type: [Number], required: true },
62+
dsName: { type: [String], required: true }
63+
})
4864
4965
const drawer = ref<boolean>(false)
5066
const tableColumns = ref<any>([
@@ -55,9 +71,9 @@ const tableColumns = ref<any>([
5571
width: 150,
5672
},
5773
{
58-
key:"tableRemark",
59-
dataKey:"tableRemark",
60-
title: 'Table Remark',
74+
key:"tableComment",
75+
dataKey:"tableComment",
76+
title: 'Table Comment',
6177
width: 300,
6278
},
6379
{
@@ -87,24 +103,25 @@ const fieldColumns = ref<any>([
87103
width: 150,
88104
},
89105
{
90-
key:"fieldRemark",
91-
dataKey:"fieldRemark",
92-
title: 'Field Remark',
106+
key:"fieldComment",
107+
dataKey:"fieldComment",
108+
title: 'Field Comment',
93109
width: 300,
94110
}
95111
])
96112
const fieldData = ref<any>([])
97113
const dsId = ref<Number>(0)
98114
99-
const open = (id: Number) => {
100-
drawer.value = true
101-
dsId.value = id
102-
datasourceApi.getTables(id).then((res) => {
103-
tableData.value = res
104-
})
115+
const back = () => {
116+
history.back()
117+
}
118+
119+
const save = () => {
120+
105121
}
106122
107123
const showFields = (tableName: string) =>{
124+
drawer.value = true
108125
datasourceApi.getFields(dsId.value, tableName).then((res) => {
109126
fieldData.value = res
110127
})
@@ -119,9 +136,27 @@ const confirmClick = () => {
119136
cancelClick()
120137
}
121138
139+
onMounted(() => {
140+
dsId.value = props.dsId
141+
datasourceApi.getTables(props.dsId).then((res) => {
142+
tableData.value = res
143+
})
144+
})
145+
122146
defineExpose({ open })
123147
</script>
124148

125149
<style lang="less" scoped>
126-
150+
.layout{
151+
padding: 20px 40px;
152+
.header{
153+
padding: 20px 0;
154+
display: flex;
155+
justify-content: space-between;
156+
}
157+
.title{
158+
display: flex;
159+
align-items: center;
160+
}
161+
}
127162
</style>

frontend/src/views/ds/index.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@
3535
</div>
3636
<div class="connection-status" :class="`${getStatus(ds.status)}`">{{ ds.status }}</div>
3737
<div class="connection-actions">
38-
<el-button class="action-btn" circle @click="getTables(ds.id)" :icon="List" />
38+
<el-button class="action-btn" circle @click="getTables(ds.id, ds.name)" :icon="List" />
3939
<el-button type="primary" class="action-btn" circle @click="editDs(ds)" :icon="IconOpeEdit"/>
4040
<el-button type="danger" class="action-btn" circle @click="deleteDs(ds)" :icon="IconOpeDelete"/>
4141
</div>
4242
</div>
4343
</div>
4444
</div>
4545
<DsForm ref="dsForm" @refresh="refresh"/>
46-
<TableList ref="tableList" />
4746
</template>
4847
<script lang="ts" setup>
4948
import IconOpeAdd from '@/assets/svg/operate/ope-add.svg'
@@ -55,13 +54,13 @@ import DsForm from './form.vue'
5554
import { datasourceApi } from '@/api/datasource'
5655
import { datetimeFormat } from '@/utils/utils'
5756
import { ElMessageBox } from 'element-plus'
58-
import TableList from './TableList.vue'
57+
import { useRouter } from 'vue-router'
5958
6059
const searchValue = ref<string>('')
6160
const dsForm = ref()
62-
const tableList = ref()
6361
const dsList = ref<any>([])// show ds list
6462
const allDsList = ref<any>([])// all ds list
63+
const router = useRouter()
6564
6665
const getStatus = (status: string) => {
6766
if (status === 'Success') {
@@ -118,7 +117,7 @@ const deleteDs = (item: any) => {
118117
})
119118
}
120119
121-
const getTables = (id: Number) => {
120+
const getTables = (id: number, name: string) => {
122121
// datasourceApi.getTables(id).then((res) => {
123122
// console.log(res)
124123
// })
@@ -130,7 +129,7 @@ const getTables = (id: Number) => {
130129
// datasourceApi.execSql(id,'select id,name,table_name from core_dataset_table limit 10').then((res) => {
131130
// console.log(res)
132131
// })
133-
tableList.value.open(id)
132+
router.push(`/ds/dsTable/${id}/${name}`)
134133
}
135134
136135
onMounted(() => {

0 commit comments

Comments
 (0)