Skip to content

Commit 0388c5d

Browse files
committed
表结构编辑功能添加
30%
1 parent 816bf09 commit 0388c5d

File tree

13 files changed

+128
-39
lines changed

13 files changed

+128
-39
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace HiDb.DataProvider.MySql.Models;
2+
3+
public class MySqlDbTypeList
4+
{
5+
public string COLUMNS { get; set; }
6+
}

DataProviders/HiDb.DataProvider.MySql/TableDataProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using HiDb.DataProvider.MySql.Models;
78

89
namespace HiDb.DataProvider.MySql
910
{
@@ -50,5 +51,11 @@ IS_NULLABLE AS AllowNull
5051
AND TABLE_SCHEMA = '{input.Mode}'
5152
AND TABLE_NAME = '{input.Table}';");
5253
}
54+
55+
public List<TableDbTypeOutput> GetDbTypeList()
56+
{
57+
var res = GetList<MySqlDbTypeList>( @$"SHOW COLUMNS FROM your_table_name;");
58+
return res.Select(c => new TableDbTypeOutput() {Name = c.COLUMNS}).ToList();
59+
}
5360
}
5461
}

DataProviders/HiDb.DataProvider.SqlServer/SqlConnectionFactory.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,13 @@ public static void InitConnection(string connectionString)
3232
public static IDbConnection GetConnection(string connectionString = "")
3333
{
3434
// 如果未初始化,初始化连接
35-
if (connection == null)
35+
if (connection is not {State: ConnectionState.Open})
3636
{
37-
connection = new SqlConnection(connectionString ?? _connectionString);
38-
try
39-
{
40-
connection.Open();
41-
}
42-
catch (Exception ex)
43-
{
44-
// 连接打开异常,抛出错误
45-
throw ex;
46-
}
37+
InitConnection(_connectionString);
4738
return connection;
4839
}
4940
else
5041
{
51-
// 正常返回连接
52-
#warning 这里考虑初始化连接的时候,获取到数据库的超时配置,比如说30000毫秒,则设定时间戳,在30000毫秒临近的时候,进行connection Open重连操作,以免连接超时查询异常
5342
return connection;
5443
}
5544
}

DataProviders/HiDb.DataProvider.SqlServer/TableDataProvider.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ public List<TableColumnOutput> GetDbColumnList(TableColumnInput input)
4141
return GetList<TableColumnOutput>(@$"SELECT
4242
COLUMN_NAME AS Name,
4343
DATA_TYPE AS Type,
44-
IS_NULLABLE AS AllowNull
44+
IS_NULLABLE AS AllowNullStr
4545
FROM
4646
INFORMATION_SCHEMA.COLUMNS
4747
WHERE
4848
TABLE_CATALOG = '{input.DataBase}'
4949
AND TABLE_SCHEMA = '{input.Mode}'
5050
AND TABLE_NAME = '{input.Table}';");
5151
}
52+
53+
public List<TableDbTypeOutput> GetDbTypeList()
54+
{
55+
return GetList<TableDbTypeOutput>( @$"SELECT name as Name
56+
FROM sys.types
57+
WHERE is_user_defined = 0
58+
ORDER BY name desc");
59+
}
5260
}
5361
}

DataProviders/HiDb.DataProvider/Dtos/Tables/TableColumnOutput.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class TableColumnOutput
2121
/// <summary>
2222
/// 是否允许Null
2323
/// </summary>
24-
public bool AllowNull { get; set; }
24+
public string AllowNullStr { get; set; }
25+
26+
/// <summary>
27+
/// 是否允许Null
28+
/// </summary>
29+
public bool AllowNull => this.AllowNullStr == "YES";
2530
}
2631
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace HiDb.DataProvider.Dtos.Tables;
2+
3+
public class TableDbTypeOutput
4+
{
5+
public string Name { get; set; }
6+
}

DataProviders/HiDb.DataProvider/ITableDataProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ public interface ITableDataProvider
2525
/// <param name="tableName"></param>
2626
/// <returns></returns>
2727
public TableColumnFullOutput GetDbColumnFullInfo(TableColumnFullInput input);
28+
29+
List<TableDbTypeOutput> GetDbTypeList();
2830
}
2931
}

Gui/HiDb.Vue/src/renderer/api/table.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ export function getTableColumnDetail(data, dbtype) {
2222
'dbtype': dbtype
2323
}
2424
})
25+
}
26+
27+
28+
// 获取详情
29+
export function getDbType(dbtype) {
30+
return request({
31+
url: '/table/db-type',
32+
method: 'post',
33+
headers: {
34+
'dbtype': dbtype
35+
}
36+
})
2537
}

Gui/HiDb.Vue/src/renderer/components/MainPage.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
</div>
192192
<div v-show="viewMode == 3" class="work" :style="{ 'width': bodyWidth }">
193193
<my-sql-edit
194+
style="width: 100%; height: 100%"
194195
:database="editTableData.database"
195196
:table="editTableData.table"
196197
:mode="editTableData.mode"

Gui/HiDb.Vue/src/renderer/components/table-edit/MySqlEdit.vue

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<template>
22
<div class="content">
33
<a-table class="table"
4-
:columns="dbColumns"
5-
size="small"
6-
:data-source="currdbData"
7-
:scroll="{ y: 500 }"
8-
:loading="dbloading"
9-
:pagination="false">
10-
4+
:columns="dbColumns"
5+
size="small"
6+
style="width: 100%;"
7+
:data-source="currdbData"
8+
:scroll="{ y: tableHeight }"
9+
:loading="loading"
10+
:pagination="false">
1111
<template #bodyCell="{ column, text, record }">
1212
<template v-if="allowEditColumns.includes(column.dataIndex)">
1313
<div>
@@ -18,16 +18,22 @@
1818
/>
1919
<template v-else>
2020
<span>
21-
<span v-if="column.dataIndex == 'name'">
22-
<sql-server v-if="record.type == 0"></sql-server>
23-
<my-sql v-if="record.type == 1"></my-sql>
24-
<pg-sql v-if="record.type == 2"></pg-sql>
25-
</span>
2621
{{ text }}
2722
</span>
2823
</template>
2924
</div>
3025
</template>
26+
<template v-if="column.dataIndex == 'type'">
27+
<a-select
28+
v-model:value="record.key"
29+
show-search
30+
placeholder="选择字段类型"
31+
style="width: 200px"
32+
:options="dbTypeOptions"
33+
:filter-option="filterOption"
34+
></a-select>
35+
</template>
36+
3137
<template v-else-if="column.dataIndex === 'operation'">
3238
<div class="editable-row-operations">
3339
<span v-if="editableData[record.key]">
@@ -47,12 +53,22 @@
4753
</template>
4854
<script setup lang="ts">
4955
import { cloneDeep } from 'lodash-es';
50-
import { UnwrapRef, reactive, ref, watchEffect } from 'vue';
56+
import { UnwrapRef, reactive, ref, watchEffect,onMounted } from 'vue';
5157
import { ConnectDbInput } from '../model/MainPageMode';
52-
import { getTableColumnList } from '../../api/table';
58+
import { getTableColumnList, getDbType } from '../../api/table';
5359
5460
const props = defineProps(['database','mode','table','dbtype'])
55-
61+
const tableHeight = ref(document.body.clientHeight);
62+
const sh = 280;
63+
onMounted(() => {
64+
tableHeight.value = document.body.clientHeight - sh;
65+
window.addEventListener('resize', onResize);
66+
loadDbType();
67+
});
68+
// 窗体大小改变事件
69+
const onResize = () => {
70+
tableHeight.value = document.body.clientHeight - sh;
71+
};
5672
// 表格数据列
5773
const dbColumns = ref<any[]>([{
5874
title: '名称',
@@ -63,24 +79,27 @@ const dbColumns = ref<any[]>([{
6379
title: '字段类型',
6480
dataIndex: 'type',
6581
sorter: false,
66-
width: 150
82+
width: 180
6783
},
6884
{
6985
title: '是否允许null',
7086
dataIndex: 'allowNull',
7187
sorter: false,
72-
width: 80
88+
width: 160
89+
},
90+
{
91+
title: '操作',
92+
dataIndex: 'operation',
93+
width: 120,
94+
fixed: 'right'
7395
}
7496
]);
7597
// 可编辑的列
76-
const allowEditColumns = ref<string[]>(['name','type']);
98+
const allowEditColumns = ref<string[]>(['name','allowNull']);
7799
// 当前数据库表格数据
78100
const currdbData = ref<any[]>([]);
79-
// loading
80-
const dbloading = ref(false);
81101
// 编辑数据
82-
const editableData: UnwrapRef<Record<string, ConnectDbInput>> = reactive({});
83-
102+
const editableData: UnwrapRef<Record<string, any>> = reactive({});
84103
// 编辑列
85104
const edit = (key: string) => {
86105
editableData[key] = cloneDeep(currdbData.value.filter(item => key === item.key)[0]);
@@ -95,7 +114,7 @@ const save = (key: string) => {
95114
const cancel = (key: string) => {
96115
delete editableData[key];
97116
};
98-
117+
// 加载
99118
const loading = ref(false);
100119
// 加载字段配置
101120
const loadTableColumn = ()=>{
@@ -106,13 +125,27 @@ const loadTableColumn = ()=>{
106125
table: props.table
107126
}, props.dbtype).then((res: any) => {
108127
loading.value = false;
109-
currdbData.value = res;
128+
currdbData.value = res.data;
110129
},()=> {
111130
loading.value = false;
112131
})
113132
}
133+
const dbTypeOptions = ref([]);
134+
const loadDbType = ()=>{
135+
getDbType(props.dbtype).then(res=>{
136+
dbTypeOptions.value = res.data.map(c => {return { value : c.name, label: c.name}});
137+
})
138+
}
139+
const filterOption = (input: string, option: any) => {
140+
return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
141+
};
142+
const clearData = ()=>{
143+
currdbData.value = [];
144+
}
145+
114146
watchEffect(()=>{
115147
console.log('watch');
148+
clearData();
116149
loadTableColumn();
117150
});
118151
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getDbType } from '../../api/table';
2+
import { props, dbTypeOptions } from './MySqlEdit.vue';
3+
4+
export const loadDbType = () => {
5+
getDbType(props.dbtype).then(res => {
6+
dbTypeOptions.value = res.data.map(c => { return { title: c.name }; });
7+
});
8+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export const MYSQL_DATA_TYPE = [
3+
'int',
4+
5+
]

Services/HiDb.Api/Controllers/TableController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ public List<TableColumnOutput> GetDbColumnList(TableColumnInput input)
3838
var data = GetService(ServiceFactory.GetTable).GetDbColumnList(input);
3939
return data;
4040
}
41+
42+
[HttpPost("db-type")]
43+
public List<TableDbTypeOutput> GetDbTypeList()
44+
{
45+
var data = GetService(ServiceFactory.GetTable).GetDbTypeList();
46+
return data;
47+
}
4148
}

0 commit comments

Comments
 (0)