Merge pull request #33 from xiaoxiaolexlh/feature/mysql-timeout-config

feat(connection): 添加MySQL超时配置
This commit is contained in:
Syngnat
2026-02-03 11:05:46 +08:00
committed by GitHub
4 changed files with 36 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react';
import { Modal, Form, Input, InputNumber, Button, message, Checkbox, Divider, Select, Alert, Card, Row, Col, Typography } from 'antd';
import { Modal, Form, Input, InputNumber, Button, message, Checkbox, Divider, Select, Alert, Card, Row, Col, Typography, Collapse } from 'antd';
import { DatabaseOutlined, ConsoleSqlOutlined, FileTextOutlined, CloudServerOutlined, AppstoreAddOutlined } from '@ant-design/icons';
import { useStore } from '../store';
import { MySQLConnect, MySQLGetDatabases } from '../../wailsjs/go/app/App';
@@ -42,7 +42,8 @@ const ConnectionModal: React.FC<{ open: boolean; onClose: () => void; initialVal
sshPassword: initialValues.config.ssh?.password,
sshKeyPath: initialValues.config.ssh?.keyPath,
driver: (initialValues.config as any).driver,
dsn: (initialValues.config as any).dsn
dsn: (initialValues.config as any).dsn,
timeout: (initialValues.config as any).timeout || 30
});
setUseSSH(initialValues.config.useSSH || false);
setDbType(initialValues.config.type);
@@ -137,7 +138,8 @@ const ConnectionModal: React.FC<{ open: boolean; onClose: () => void; initialVal
useSSH: !!values.useSSH,
ssh: sshConfig,
driver: values.driver,
dsn: values.dsn
dsn: values.dsn,
timeout: Number(values.timeout || 30)
};
};
@@ -196,7 +198,7 @@ const ConnectionModal: React.FC<{ open: boolean; onClose: () => void; initialVal
<Form
form={form}
layout="vertical"
initialValues={{ type: 'mysql', host: 'localhost', port: 3306, user: 'root', useSSH: false, sshPort: 22 }}
initialValues={{ type: 'mysql', host: 'localhost', port: 3306, user: 'root', useSSH: false, sshPort: 22, timeout: 30 }}
onValuesChange={(changed) => {
if (testResult) setTestResult(null); // Clear result on change
if (changed.useSSH !== undefined) setUseSSH(changed.useSSH);
@@ -282,6 +284,26 @@ const ConnectionModal: React.FC<{ open: boolean; onClose: () => void; initialVal
</Form.Item>
</div>
)}
<Divider style={{ margin: '12px 0' }} />
<Collapse
ghost
items={[{
key: 'advanced',
label: '高级连接',
children: (
<Form.Item
name="timeout"
label="连接超时 (秒)"
help="数据库连接超时时间,默认 30 秒"
rules={[{ type: 'number', min: 1, max: 300, message: '超时时间范围: 1-300 秒' }]}
>
<InputNumber style={{ width: '100%' }} min={1} max={300} placeholder="30" />
</Form.Item>
)
}]}
/>
</>
)}
</>

View File

@@ -79,6 +79,7 @@ export namespace connection {
ssh: SSHConfig;
driver?: string;
dsn?: string;
timeout?: number;
static createFrom(source: any = {}) {
return new ConnectionConfig(source);
@@ -96,6 +97,7 @@ export namespace connection {
this.ssh = this.convertValues(source["ssh"], SSHConfig);
this.driver = source["driver"];
this.dsn = source["dsn"];
this.timeout = source["timeout"];
}
convertValues(a: any, classs: any, asMap: boolean = false): any {

View File

@@ -21,6 +21,7 @@ type ConnectionConfig struct {
SSH SSHConfig `json:"ssh"`
Driver string `json:"driver,omitempty"` // For custom connection
DSN string `json:"dsn,omitempty"` // For custom connection
Timeout int `json:"timeout,omitempty"` // Connection timeout in seconds (default: 30)
}
// QueryResult is the standard response format for Wails methods

View File

@@ -30,8 +30,13 @@ func (m *MySQLDB) getDSN(config connection.ConnectionConfig) string {
}
}
return fmt.Sprintf("%s:%s@%s(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.User, config.Password, protocol, address, database)
timeout := config.Timeout
if timeout <= 0 {
timeout = 30
}
return fmt.Sprintf("%s:%s@%s(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=%ds",
config.User, config.Password, protocol, address, database, timeout)
}
func (m *MySQLDB) Connect(config connection.ConnectionConfig) error {