mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-09 06:23:29 +08:00
Compare commits
15 Commits
v0.2.3
...
feature/ta
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71e5de0cdc | ||
|
|
d8656c6c9c | ||
|
|
443b487a02 | ||
|
|
bac57ebdf0 | ||
|
|
213a33e4f3 | ||
|
|
a00f87582d | ||
|
|
f129623000 | ||
|
|
8dbc97e466 | ||
|
|
4a0db185c0 | ||
|
|
5793f63ac8 | ||
|
|
8aabc67634 | ||
|
|
34c494ce51 | ||
|
|
178de02783 | ||
|
|
94e5b8d2c6 | ||
|
|
89e2247c05 |
58
.github/ISSUE_TEMPLATE/01-bug_report.yml
vendored
Normal file
58
.github/ISSUE_TEMPLATE/01-bug_report.yml
vendored
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
name: 问题反馈
|
||||||
|
description: 软件问题反馈
|
||||||
|
title: "[Bug] "
|
||||||
|
labels: ["bug"]
|
||||||
|
|
||||||
|
body:
|
||||||
|
- type: checkboxes
|
||||||
|
id: searched
|
||||||
|
attributes:
|
||||||
|
label: 已经搜索过 Issues,未发现重复问题*
|
||||||
|
options:
|
||||||
|
- label: 我已经搜索过 Issues,没有发现重复问题
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: input
|
||||||
|
id: system
|
||||||
|
attributes:
|
||||||
|
label: 操作系统及版本
|
||||||
|
placeholder: Windows 10 22H2 / macOS Mojave / Linux
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: input
|
||||||
|
id: version
|
||||||
|
attributes:
|
||||||
|
label: 软件安装版本
|
||||||
|
placeholder: v0.2.3
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: description
|
||||||
|
attributes:
|
||||||
|
label: 问题简述及复现流程
|
||||||
|
description: 请详细描述你遇到的问题,并提供复现步骤
|
||||||
|
placeholder: |
|
||||||
|
1. 打开软件
|
||||||
|
2. 点击 xxx
|
||||||
|
3. 预期结果是 ...
|
||||||
|
4. 实际结果是 ...
|
||||||
|
5. 截图 ...
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: extra
|
||||||
|
attributes:
|
||||||
|
label: 其他补充
|
||||||
|
description: 如果你有额外信息,请在此填写
|
||||||
|
placeholder: 可选
|
||||||
|
|
||||||
|
- type: checkboxes
|
||||||
|
id: pr
|
||||||
|
attributes:
|
||||||
|
label: 是否愿意提交 PR 修复当前 Issue
|
||||||
|
options:
|
||||||
|
- label: 我愿意尝试提交 PR
|
||||||
37
.github/ISSUE_TEMPLATE/02-feature_request.yml
vendored
Normal file
37
.github/ISSUE_TEMPLATE/02-feature_request.yml
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
name: 功能建议
|
||||||
|
description: 添加全新功能或改进现有功能
|
||||||
|
title: "[Enhancement] "
|
||||||
|
labels: ["enhancement"]
|
||||||
|
|
||||||
|
body:
|
||||||
|
- type: checkboxes
|
||||||
|
id: searched
|
||||||
|
attributes:
|
||||||
|
label: 已经搜索过 Issues,未发现重复问题*
|
||||||
|
options:
|
||||||
|
- label: 我已经搜索过 Issues,没有发现重复问题
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: feature
|
||||||
|
attributes:
|
||||||
|
label: 功能描述
|
||||||
|
description: 请详细描述你希望添加或改进的功能
|
||||||
|
placeholder: 请描述你想要的功能
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: extra
|
||||||
|
attributes:
|
||||||
|
label: 其他补充
|
||||||
|
description: 如果你有额外信息,请在此填写
|
||||||
|
placeholder: 可选
|
||||||
|
|
||||||
|
- type: checkboxes
|
||||||
|
id: pr
|
||||||
|
attributes:
|
||||||
|
label: 是否愿意提交 PR 实现当前 Issue
|
||||||
|
options:
|
||||||
|
- label: 我愿意尝试提交 PR
|
||||||
30
.github/ISSUE_TEMPLATE/03-generic.yml
vendored
Normal file
30
.github/ISSUE_TEMPLATE/03-generic.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
name: 其他反馈
|
||||||
|
description: 其他类型反馈、建议或讨论
|
||||||
|
title: "[Question] "
|
||||||
|
labels: ["question"]
|
||||||
|
|
||||||
|
body:
|
||||||
|
- type: checkboxes
|
||||||
|
id: searched
|
||||||
|
attributes:
|
||||||
|
label: 已经搜索过 Issues,未发现重复问题*
|
||||||
|
options:
|
||||||
|
- label: 我已经搜索过 Issues,没有发现重复问题
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: content
|
||||||
|
attributes:
|
||||||
|
label: 内容
|
||||||
|
description: 请填写你的反馈、建议或讨论内容
|
||||||
|
placeholder: 请描述你的问题或想法
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: extra
|
||||||
|
attributes:
|
||||||
|
label: 其他补充
|
||||||
|
description: 如果你有额外信息,请在此填写
|
||||||
|
placeholder: 可选
|
||||||
1
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
1
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
@@ -1 +1 @@
|
|||||||
d0f9366af59a6367ad3c7e2d4185ead4
|
5b8157374dae5f9340e31b2d0bd2c00e
|
||||||
@@ -285,12 +285,12 @@ function App() {
|
|||||||
title="拖动调整宽度"
|
title="拖动调整宽度"
|
||||||
/>
|
/>
|
||||||
</Sider>
|
</Sider>
|
||||||
<Content style={{ background: darkMode ? '#141414' : '#fff', overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
|
<Content style={{ background: darkMode ? '#141414' : '#fff', overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
|
||||||
<div style={{ flex: 1, overflow: 'hidden' }}>
|
<div style={{ flex: 1, minHeight: 0, overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
|
||||||
<TabManager />
|
<TabManager />
|
||||||
</div>
|
</div>
|
||||||
{isLogPanelOpen && (
|
{isLogPanelOpen && (
|
||||||
<LogPanel
|
<LogPanel
|
||||||
height={logPanelHeight}
|
height={logPanelHeight}
|
||||||
onClose={() => setIsLogPanelOpen(false)}
|
onClose={() => setIsLogPanelOpen(false)}
|
||||||
onResizeStart={handleLogResizeStart}
|
onResizeStart={handleLogResizeStart}
|
||||||
@@ -343,4 +343,4 @@ function App() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|||||||
@@ -264,8 +264,8 @@ const ConnectionModal: React.FC<{ open: boolean; onClose: () => void; initialVal
|
|||||||
{useSSH && (
|
{useSSH && (
|
||||||
<div style={{ padding: '12px', background: '#f5f5f5', borderRadius: 6, marginTop: 12 }}>
|
<div style={{ padding: '12px', background: '#f5f5f5', borderRadius: 6, marginTop: 12 }}>
|
||||||
<div style={{ display: 'flex', gap: 16 }}>
|
<div style={{ display: 'flex', gap: 16 }}>
|
||||||
<Form.Item name="sshHost" label="SSH 主机" rules={[{ required: useSSH, message: '请输入SSH主机' }]} style={{ flex: 1 }}>
|
<Form.Item name="sshHost" label="SSH 主机 (域名或IP)" rules={[{ required: useSSH, message: '请输入SSH主机' }]} style={{ flex: 1 }}>
|
||||||
<Input placeholder="ssh.example.com" />
|
<Input placeholder="例如: ssh.example.com 或 192.168.1.100" />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item name="sshPort" label="端口" rules={[{ required: useSSH, message: '请输入SSH端口' }]} style={{ width: 100 }}>
|
<Form.Item name="sshPort" label="端口" rules={[{ required: useSSH, message: '请输入SSH端口' }]} style={{ width: 100 }}>
|
||||||
<InputNumber style={{ width: '100%' }} />
|
<InputNumber style={{ width: '100%' }} />
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import React, { useState, useEffect, useRef, useContext, useMemo, useCallback } from 'react';
|
import React, { useState, useEffect, useRef, useContext, useMemo, useCallback } from 'react';
|
||||||
import { Table, message, Input, Button, Dropdown, MenuProps, Form, Pagination, Select, Modal } from 'antd';
|
import { Table, message, Input, Button, Dropdown, MenuProps, Form, Pagination, Select, Modal } from 'antd';
|
||||||
import type { SortOrder } from 'antd/es/table/interface';
|
import type { SortOrder } from 'antd/es/table/interface';
|
||||||
import { ReloadOutlined, ImportOutlined, ExportOutlined, DownOutlined, PlusOutlined, DeleteOutlined, SaveOutlined, UndoOutlined, FilterOutlined, CloseOutlined, ConsoleSqlOutlined, FileTextOutlined, CopyOutlined, ClearOutlined } from '@ant-design/icons';
|
import { ReloadOutlined, ImportOutlined, ExportOutlined, DownOutlined, PlusOutlined, DeleteOutlined, SaveOutlined, UndoOutlined, FilterOutlined, CloseOutlined, ConsoleSqlOutlined, FileTextOutlined, CopyOutlined, ClearOutlined, EditOutlined } from '@ant-design/icons';
|
||||||
import { ImportData, ExportTable, ExportData, ApplyChanges } from '../../wailsjs/go/app/App';
|
import Editor from '@monaco-editor/react';
|
||||||
|
import { ImportData, ExportTable, ExportData, ExportQuery, ApplyChanges } from '../../wailsjs/go/app/App';
|
||||||
import { useStore } from '../store';
|
import { useStore } from '../store';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import 'react-resizable/css/styles.css';
|
import 'react-resizable/css/styles.css';
|
||||||
|
import { buildWhereSQL, escapeLiteral, quoteIdentPart, quoteQualifiedIdent } from '../utils/sql';
|
||||||
|
|
||||||
// 内部行标识字段:避免与真实业务字段(如 `key` 列)冲突。
|
// 内部行标识字段:避免与真实业务字段(如 `key` 列)冲突。
|
||||||
export const GONAVI_ROW_KEY = '__gonavi_row_key__';
|
export const GONAVI_ROW_KEY = '__gonavi_row_key__';
|
||||||
@@ -27,16 +29,47 @@ const formatCellValue = (val: any) => {
|
|||||||
return String(val);
|
return String(val);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const toEditableText = (val: any): string => {
|
||||||
|
if (val === null || val === undefined) return '';
|
||||||
|
if (typeof val === 'string') return val;
|
||||||
|
try {
|
||||||
|
return JSON.stringify(val, null, 2);
|
||||||
|
} catch {
|
||||||
|
return String(val);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const toFormText = (val: any): string => {
|
||||||
|
if (val === null || val === undefined) return '';
|
||||||
|
if (typeof val === 'string') return normalizeDateTimeString(val);
|
||||||
|
return toEditableText(val);
|
||||||
|
};
|
||||||
|
|
||||||
|
const looksLikeJsonText = (text: string): boolean => {
|
||||||
|
const raw = (text || '').trim();
|
||||||
|
if (!raw) return false;
|
||||||
|
const first = raw[0];
|
||||||
|
const last = raw[raw.length - 1];
|
||||||
|
return (first === '{' && last === '}') || (first === '[' && last === ']');
|
||||||
|
};
|
||||||
|
|
||||||
// --- Resizable Header (Native Implementation) ---
|
// --- Resizable Header (Native Implementation) ---
|
||||||
const ResizableTitle = (props: any) => {
|
const ResizableTitle = (props: any) => {
|
||||||
const { onResizeStart, width, ...restProps } = props;
|
const { onResizeStart, width, ...restProps } = props;
|
||||||
|
|
||||||
if (!width) {
|
const nextStyle = { ...(restProps.style || {}) } as React.CSSProperties;
|
||||||
return <th {...restProps} />;
|
if (width) {
|
||||||
|
nextStyle.width = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注意:virtual table 模式下,rc-table 会依赖 header cell 的 width 样式来渲染选择列。
|
||||||
|
// 若这里丢失 width,可能导致左上角“全选”checkbox 不显示。
|
||||||
|
if (!width || typeof onResizeStart !== 'function') {
|
||||||
|
return <th {...restProps} style={nextStyle} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<th {...restProps} style={{ ...restProps.style, position: 'relative' }}>
|
<th {...restProps} style={{ ...nextStyle, position: 'relative' }}>
|
||||||
{restProps.children}
|
{restProps.children}
|
||||||
<span
|
<span
|
||||||
className="react-resizable-handle"
|
className="react-resizable-handle"
|
||||||
@@ -85,6 +118,7 @@ interface EditableCellProps {
|
|||||||
dataIndex: string;
|
dataIndex: string;
|
||||||
record: Item;
|
record: Item;
|
||||||
handleSave: (record: Item) => void;
|
handleSave: (record: Item) => void;
|
||||||
|
focusCell?: (record: Item, dataIndex: string, title: React.ReactNode) => void;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +129,7 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
|
|||||||
dataIndex,
|
dataIndex,
|
||||||
record,
|
record,
|
||||||
handleSave,
|
handleSave,
|
||||||
|
focusCell,
|
||||||
...restProps
|
...restProps
|
||||||
}) => {
|
}) => {
|
||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
@@ -139,7 +174,26 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return <td {...restProps} onDoubleClick={editable ? toggleEdit : undefined}>{childNode}</td>;
|
const handleDoubleClick = () => {
|
||||||
|
if (!editable) return;
|
||||||
|
toggleEdit();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleClick = (e: React.MouseEvent) => {
|
||||||
|
restProps?.onClick?.(e);
|
||||||
|
if (!editable) return;
|
||||||
|
if (typeof focusCell === 'function') focusCell(record, dataIndex, title);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<td
|
||||||
|
{...restProps}
|
||||||
|
onClick={editable ? handleClick : restProps?.onClick}
|
||||||
|
onDoubleClick={editable ? handleDoubleClick : restProps?.onDoubleClick}
|
||||||
|
>
|
||||||
|
{childNode}
|
||||||
|
</td>
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
const ContextMenuRow = React.memo(({ children, record, ...props }: any) => {
|
const ContextMenuRow = React.memo(({ children, record, ...props }: any) => {
|
||||||
@@ -221,9 +275,23 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const { connections } = useStore();
|
const { connections } = useStore();
|
||||||
const addSqlLog = useStore(state => state.addSqlLog);
|
const addSqlLog = useStore(state => state.addSqlLog);
|
||||||
|
const darkMode = useStore(state => state.darkMode);
|
||||||
|
const selectionColumnWidth = 46;
|
||||||
const [form] = Form.useForm();
|
const [form] = Form.useForm();
|
||||||
const [modal, contextHolder] = Modal.useModal();
|
const [modal, contextHolder] = Modal.useModal();
|
||||||
const gridId = useMemo(() => `grid-${uuidv4()}`, []);
|
const gridId = useMemo(() => `grid-${uuidv4()}`, []);
|
||||||
|
const [cellEditorOpen, setCellEditorOpen] = useState(false);
|
||||||
|
const [cellEditorValue, setCellEditorValue] = useState('');
|
||||||
|
const [cellEditorIsJson, setCellEditorIsJson] = useState(false);
|
||||||
|
const [cellEditorMeta, setCellEditorMeta] = useState<{ record: Item; dataIndex: string; title: string } | null>(null);
|
||||||
|
const cellEditorApplyRef = useRef<((val: string) => void) | null>(null);
|
||||||
|
const [activeCell, setActiveCell] = useState<{ rowKey: string; dataIndex: string; title: string } | null>(null);
|
||||||
|
const [rowEditorOpen, setRowEditorOpen] = useState(false);
|
||||||
|
const [rowEditorRowKey, setRowEditorRowKey] = useState<string>('');
|
||||||
|
const rowEditorBaseRef = useRef<Record<string, string>>({});
|
||||||
|
const rowEditorDisplayRef = useRef<Record<string, string>>({});
|
||||||
|
const rowEditorNullColsRef = useRef<Set<string>>(new Set());
|
||||||
|
const [rowEditorForm] = Form.useForm();
|
||||||
|
|
||||||
// Helper to export specific data
|
// Helper to export specific data
|
||||||
const exportData = async (rows: any[], format: string) => {
|
const exportData = async (rows: any[], format: string) => {
|
||||||
@@ -237,32 +305,66 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
|
|
||||||
const [sortInfo, setSortInfo] = useState<{ columnKey: string, order: string } | null>(null);
|
const [sortInfo, setSortInfo] = useState<{ columnKey: string, order: string } | null>(null);
|
||||||
const [columnWidths, setColumnWidths] = useState<Record<string, number>>({});
|
const [columnWidths, setColumnWidths] = useState<Record<string, number>>({});
|
||||||
|
|
||||||
|
const closeCellEditor = useCallback(() => {
|
||||||
|
setCellEditorOpen(false);
|
||||||
|
setCellEditorMeta(null);
|
||||||
|
setCellEditorValue('');
|
||||||
|
setCellEditorIsJson(false);
|
||||||
|
cellEditorApplyRef.current = null;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const openCellEditor = useCallback((record: Item, dataIndex: string, title: React.ReactNode, onApplyValue?: (val: string) => void) => {
|
||||||
|
if (!record || !dataIndex) return;
|
||||||
|
const raw = record?.[dataIndex];
|
||||||
|
const text = toEditableText(raw);
|
||||||
|
const isJson = looksLikeJsonText(text);
|
||||||
|
const titleText = typeof title === 'string' ? title : (typeof title === 'number' ? String(title) : String(dataIndex));
|
||||||
|
|
||||||
|
setCellEditorMeta({ record, dataIndex, title: titleText });
|
||||||
|
setCellEditorValue(text);
|
||||||
|
setCellEditorIsJson(isJson);
|
||||||
|
setCellEditorOpen(true);
|
||||||
|
cellEditorApplyRef.current = typeof onApplyValue === 'function' ? onApplyValue : null;
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Dynamic Height
|
// Dynamic Height
|
||||||
const [tableHeight, setTableHeight] = useState(500);
|
const [tableHeight, setTableHeight] = useState(500);
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!containerRef.current) return;
|
const el = containerRef.current;
|
||||||
|
if (!el) return;
|
||||||
let rafId: number;
|
|
||||||
|
let rafId: number | null = null;
|
||||||
|
|
||||||
const resizeObserver = new ResizeObserver(entries => {
|
const resizeObserver = new ResizeObserver(entries => {
|
||||||
|
if (rafId !== null) cancelAnimationFrame(rafId);
|
||||||
rafId = requestAnimationFrame(() => {
|
rafId = requestAnimationFrame(() => {
|
||||||
for (let entry of entries) {
|
const target = (entries[0]?.target as HTMLElement | undefined) || containerRef.current;
|
||||||
// Use boundingClientRect for more accurate render size (including padding if any)
|
if (!target) return;
|
||||||
const height = entry.contentRect.height;
|
|
||||||
if (height < 50) return;
|
const height = target.getBoundingClientRect().height;
|
||||||
// Subtract header (~42px) and a buffer
|
if (!Number.isFinite(height) || height < 50) return;
|
||||||
const h = Math.max(100, height - 42);
|
|
||||||
setTableHeight(h);
|
const headerEl =
|
||||||
}
|
(target.querySelector('.ant-table-header') as HTMLElement | null) ||
|
||||||
|
(target.querySelector('.ant-table-thead') as HTMLElement | null);
|
||||||
|
const rawHeaderHeight = headerEl ? headerEl.getBoundingClientRect().height : NaN;
|
||||||
|
const headerHeight =
|
||||||
|
Number.isFinite(rawHeaderHeight) && rawHeaderHeight >= 24 && rawHeaderHeight <= 120 ? rawHeaderHeight : 42;
|
||||||
|
|
||||||
|
// 留一点余量,避免底部(边框/滚动条)遮挡最后一行
|
||||||
|
const extraBottom = 16;
|
||||||
|
const nextHeight = Math.max(100, Math.floor(height - headerHeight - extraBottom));
|
||||||
|
setTableHeight(nextHeight);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
resizeObserver.observe(containerRef.current);
|
resizeObserver.observe(el);
|
||||||
return () => {
|
return () => {
|
||||||
resizeObserver.disconnect();
|
resizeObserver.disconnect();
|
||||||
cancelAnimationFrame(rafId);
|
if (rafId !== null) cancelAnimationFrame(rafId);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -272,7 +374,7 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
const [deletedRowKeys, setDeletedRowKeys] = useState<Set<string>>(new Set());
|
const [deletedRowKeys, setDeletedRowKeys] = useState<Set<string>>(new Set());
|
||||||
|
|
||||||
// Filter State
|
// Filter State
|
||||||
const [filterConditions, setFilterConditions] = useState<{ id: number, column: string, op: string, value: string }[]>([]);
|
const [filterConditions, setFilterConditions] = useState<{ id: number, column: string, op: string, value: string, value2?: string }[]>([]);
|
||||||
const [nextFilterId, setNextFilterId] = useState(1);
|
const [nextFilterId, setNextFilterId] = useState(1);
|
||||||
|
|
||||||
const selectedRowKeysRef = useRef(selectedRowKeys);
|
const selectedRowKeysRef = useRef(selectedRowKeys);
|
||||||
@@ -286,6 +388,14 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
setModifiedRows({});
|
setModifiedRows({});
|
||||||
setDeletedRowKeys(new Set());
|
setDeletedRowKeys(new Set());
|
||||||
setSelectedRowKeys([]);
|
setSelectedRowKeys([]);
|
||||||
|
setActiveCell(null);
|
||||||
|
setRowEditorOpen(false);
|
||||||
|
setRowEditorRowKey('');
|
||||||
|
rowEditorBaseRef.current = {};
|
||||||
|
rowEditorDisplayRef.current = {};
|
||||||
|
rowEditorNullColsRef.current = new Set();
|
||||||
|
rowEditorForm.resetFields();
|
||||||
|
closeCellEditor();
|
||||||
form.resetFields();
|
form.resetFields();
|
||||||
}, [tableName, dbName, connectionId]); // Reset on context change
|
}, [tableName, dbName, connectionId]); // Reset on context change
|
||||||
|
|
||||||
@@ -440,6 +550,29 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
}
|
}
|
||||||
}, [addedRows]);
|
}, [addedRows]);
|
||||||
|
|
||||||
|
const handleCellEditorSave = useCallback(() => {
|
||||||
|
if (!cellEditorMeta) return;
|
||||||
|
const apply = cellEditorApplyRef.current;
|
||||||
|
if (apply) {
|
||||||
|
apply(cellEditorValue);
|
||||||
|
closeCellEditor();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const nextRow: any = { ...cellEditorMeta.record, [cellEditorMeta.dataIndex]: cellEditorValue };
|
||||||
|
handleCellSave(nextRow);
|
||||||
|
closeCellEditor();
|
||||||
|
}, [cellEditorMeta, cellEditorValue, handleCellSave, closeCellEditor]);
|
||||||
|
|
||||||
|
const handleFormatJsonInEditor = useCallback(() => {
|
||||||
|
if (!cellEditorIsJson) return;
|
||||||
|
try {
|
||||||
|
const obj = JSON.parse(cellEditorValue);
|
||||||
|
setCellEditorValue(JSON.stringify(obj, null, 2));
|
||||||
|
} catch (e: any) {
|
||||||
|
message.error("JSON 格式无效:" + (e?.message || String(e)));
|
||||||
|
}
|
||||||
|
}, [cellEditorIsJson, cellEditorValue]);
|
||||||
|
|
||||||
// Merge Data for Display
|
// Merge Data for Display
|
||||||
// 'displayData' already merges addedRows.
|
// 'displayData' already merges addedRows.
|
||||||
// We need to merge modifiedRows into it for rendering.
|
// We need to merge modifiedRows into it for rendering.
|
||||||
@@ -453,6 +586,110 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
});
|
});
|
||||||
}, [displayData, modifiedRows]);
|
}, [displayData, modifiedRows]);
|
||||||
|
|
||||||
|
const focusCell = useCallback((record: Item, dataIndex: string, title: React.ReactNode) => {
|
||||||
|
const k = record?.[GONAVI_ROW_KEY];
|
||||||
|
if (k === undefined) return;
|
||||||
|
const titleText = typeof title === 'string' ? title : (typeof title === 'number' ? String(title) : String(dataIndex));
|
||||||
|
setActiveCell({ rowKey: rowKeyStr(k), dataIndex, title: titleText });
|
||||||
|
}, [rowKeyStr]);
|
||||||
|
|
||||||
|
const closeRowEditor = useCallback(() => {
|
||||||
|
setRowEditorOpen(false);
|
||||||
|
setRowEditorRowKey('');
|
||||||
|
rowEditorBaseRef.current = {};
|
||||||
|
rowEditorDisplayRef.current = {};
|
||||||
|
rowEditorNullColsRef.current = new Set();
|
||||||
|
rowEditorForm.resetFields();
|
||||||
|
}, [rowEditorForm]);
|
||||||
|
|
||||||
|
const openRowEditor = useCallback(() => {
|
||||||
|
if (readOnly || !tableName) return;
|
||||||
|
if (selectedRowKeys.length > 1) {
|
||||||
|
message.info('一次只能编辑一行,请仅选择一行');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const keyStr =
|
||||||
|
selectedRowKeys.length === 1 ? rowKeyStr(selectedRowKeys[0]) : activeCell?.rowKey;
|
||||||
|
if (!keyStr) {
|
||||||
|
message.info('请先选择一行(勾选一行或点击任意单元格)');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const displayRow = mergedDisplayData.find(r => rowKeyStr(r?.[GONAVI_ROW_KEY]) === keyStr);
|
||||||
|
if (!displayRow) {
|
||||||
|
message.error('未找到目标行,请刷新后重试');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseRow =
|
||||||
|
data.find(r => rowKeyStr(r?.[GONAVI_ROW_KEY]) === keyStr) ||
|
||||||
|
addedRows.find(r => rowKeyStr(r?.[GONAVI_ROW_KEY]) === keyStr) ||
|
||||||
|
displayRow;
|
||||||
|
|
||||||
|
const baseMap: Record<string, string> = {};
|
||||||
|
const displayMap: Record<string, string> = {};
|
||||||
|
const nullCols = new Set<string>();
|
||||||
|
|
||||||
|
columnNames.forEach((col) => {
|
||||||
|
const baseVal = (baseRow as any)?.[col];
|
||||||
|
const displayVal = (displayRow as any)?.[col];
|
||||||
|
baseMap[col] = toFormText(baseVal);
|
||||||
|
displayMap[col] = toFormText(displayVal);
|
||||||
|
if (baseVal === null || baseVal === undefined) nullCols.add(col);
|
||||||
|
});
|
||||||
|
|
||||||
|
rowEditorBaseRef.current = baseMap;
|
||||||
|
rowEditorDisplayRef.current = displayMap;
|
||||||
|
rowEditorNullColsRef.current = nullCols;
|
||||||
|
|
||||||
|
rowEditorForm.setFieldsValue(displayMap);
|
||||||
|
setRowEditorRowKey(keyStr);
|
||||||
|
setRowEditorOpen(true);
|
||||||
|
}, [readOnly, tableName, selectedRowKeys, activeCell, mergedDisplayData, data, addedRows, columnNames, rowEditorForm, rowKeyStr]);
|
||||||
|
|
||||||
|
const openRowEditorFieldEditor = useCallback((dataIndex: string) => {
|
||||||
|
if (!dataIndex) return;
|
||||||
|
const val = rowEditorForm.getFieldValue(dataIndex);
|
||||||
|
openCellEditor(
|
||||||
|
{ [dataIndex]: val ?? '' },
|
||||||
|
dataIndex,
|
||||||
|
dataIndex,
|
||||||
|
(nextVal) => rowEditorForm.setFieldsValue({ [dataIndex]: nextVal }),
|
||||||
|
);
|
||||||
|
}, [rowEditorForm, openCellEditor]);
|
||||||
|
|
||||||
|
const applyRowEditor = useCallback(() => {
|
||||||
|
const keyStr = rowEditorRowKey;
|
||||||
|
if (!keyStr) return;
|
||||||
|
const values = rowEditorForm.getFieldsValue(true) || {};
|
||||||
|
|
||||||
|
const isAdded = addedRows.some(r => rowKeyStr(r?.[GONAVI_ROW_KEY]) === keyStr);
|
||||||
|
if (isAdded) {
|
||||||
|
setAddedRows(prev => prev.map(r => rowKeyStr(r?.[GONAVI_ROW_KEY]) === keyStr ? { ...r, ...values } : r));
|
||||||
|
closeRowEditor();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseMap = rowEditorBaseRef.current || {};
|
||||||
|
const patch: Record<string, any> = {};
|
||||||
|
columnNames.forEach((col) => {
|
||||||
|
const nextVal = values[col];
|
||||||
|
const nextStr = toFormText(nextVal);
|
||||||
|
const baseStr = baseMap[col] ?? '';
|
||||||
|
if (nextStr !== baseStr) patch[col] = nextStr;
|
||||||
|
});
|
||||||
|
|
||||||
|
setModifiedRows(prev => {
|
||||||
|
const next = { ...prev };
|
||||||
|
if (Object.keys(patch).length === 0) delete next[keyStr];
|
||||||
|
else next[keyStr] = patch;
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
|
||||||
|
closeRowEditor();
|
||||||
|
}, [rowEditorRowKey, rowEditorForm, addedRows, columnNames, rowKeyStr, closeRowEditor]);
|
||||||
|
|
||||||
const columns = useMemo(() => {
|
const columns = useMemo(() => {
|
||||||
return columnNames.map(key => ({
|
return columnNames.map(key => ({
|
||||||
title: key,
|
title: key,
|
||||||
@@ -481,9 +718,13 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
dataIndex: col.dataIndex,
|
dataIndex: col.dataIndex,
|
||||||
title: col.title,
|
title: col.title,
|
||||||
handleSave: handleCellSave,
|
handleSave: handleCellSave,
|
||||||
|
focusCell,
|
||||||
|
className: (activeCell && rowKeyStr(record?.[GONAVI_ROW_KEY]) === activeCell.rowKey && col.dataIndex === activeCell.dataIndex)
|
||||||
|
? 'gonavi-active-cell'
|
||||||
|
: undefined,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
}), [columns, handleCellSave]);
|
}), [columns, handleCellSave, openCellEditor, focusCell, activeCell, rowKeyStr]);
|
||||||
|
|
||||||
const handleAddRow = () => {
|
const handleAddRow = () => {
|
||||||
const newKey = `new-${Date.now()}`;
|
const newKey = `new-${Date.now()}`;
|
||||||
@@ -633,11 +874,98 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
copyToClipboard(lines.join('\n'));
|
copyToClipboard(lines.join('\n'));
|
||||||
}, [getTargets, copyToClipboard]);
|
}, [getTargets, copyToClipboard]);
|
||||||
|
|
||||||
|
const buildConnConfig = useCallback(() => {
|
||||||
|
if (!connectionId) return null;
|
||||||
|
const conn = connections.find(c => c.id === connectionId);
|
||||||
|
if (!conn) return null;
|
||||||
|
return {
|
||||||
|
...conn.config,
|
||||||
|
port: Number(conn.config.port),
|
||||||
|
password: conn.config.password || "",
|
||||||
|
database: conn.config.database || "",
|
||||||
|
useSSH: conn.config.useSSH || false,
|
||||||
|
ssh: conn.config.ssh || { host: "", port: 22, user: "", password: "", keyPath: "" }
|
||||||
|
};
|
||||||
|
}, [connections, connectionId]);
|
||||||
|
|
||||||
|
const exportByQuery = useCallback(async (sql: string, format: string, defaultName: string) => {
|
||||||
|
const config = buildConnConfig();
|
||||||
|
if (!config) return;
|
||||||
|
const hide = message.loading(`正在导出...`, 0);
|
||||||
|
const res = await ExportQuery(config as any, dbName || '', sql, defaultName || 'export', format);
|
||||||
|
hide();
|
||||||
|
if (res.success) {
|
||||||
|
message.success("导出成功");
|
||||||
|
} else if (res.message !== "Cancelled") {
|
||||||
|
message.error("导出失败: " + res.message);
|
||||||
|
}
|
||||||
|
}, [buildConnConfig, dbName]);
|
||||||
|
|
||||||
|
const buildPkWhereSql = useCallback((rows: any[], dbType: string) => {
|
||||||
|
if (!tableName || pkColumns.length === 0) return '';
|
||||||
|
const targets = (rows || []).filter(Boolean);
|
||||||
|
if (targets.length === 0) return '';
|
||||||
|
|
||||||
|
const clauses: string[] = [];
|
||||||
|
for (const r of targets) {
|
||||||
|
const andParts: string[] = [];
|
||||||
|
for (const pk of pkColumns) {
|
||||||
|
const col = quoteIdentPart(dbType, pk);
|
||||||
|
const v = r?.[pk];
|
||||||
|
if (v === null || v === undefined) return '';
|
||||||
|
andParts.push(`${col} = '${escapeLiteral(String(v))}'`);
|
||||||
|
}
|
||||||
|
if (andParts.length === pkColumns.length) {
|
||||||
|
clauses.push(`(${andParts.join(' AND ')})`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (clauses.length === 0) return '';
|
||||||
|
return clauses.join(' OR ');
|
||||||
|
}, [pkColumns, tableName]);
|
||||||
|
|
||||||
|
const buildCurrentPageSql = useCallback((dbType: string) => {
|
||||||
|
if (!tableName || !pagination) return '';
|
||||||
|
const whereSQL = buildWhereSQL(dbType, filterConditions);
|
||||||
|
let sql = `SELECT * FROM ${quoteQualifiedIdent(dbType, tableName)} ${whereSQL}`;
|
||||||
|
if (sortInfo && sortInfo.order) {
|
||||||
|
sql += ` ORDER BY ${quoteIdentPart(dbType, sortInfo.columnKey)} ${sortInfo.order === 'ascend' ? 'ASC' : 'DESC'}`;
|
||||||
|
}
|
||||||
|
const offset = (pagination.current - 1) * pagination.pageSize;
|
||||||
|
sql += ` LIMIT ${pagination.pageSize} OFFSET ${offset}`;
|
||||||
|
return sql;
|
||||||
|
}, [tableName, pagination, filterConditions, sortInfo]);
|
||||||
|
|
||||||
// Context Menu Export
|
// Context Menu Export
|
||||||
const handleExportSelected = useCallback(async (format: string, record: any) => {
|
const handleExportSelected = useCallback(async (format: string, record: any) => {
|
||||||
const records = getTargets(record);
|
const records = getTargets(record);
|
||||||
await exportData(records, format);
|
if (!connectionId || !tableName) {
|
||||||
}, [getTargets]);
|
await exportData(records, format);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 有未提交修改时,优先按界面数据导出,避免与数据库不一致。
|
||||||
|
if (hasChanges) {
|
||||||
|
message.warning("当前存在未提交修改,导出将按界面数据生成;如需完整长字段建议先提交后再导出。");
|
||||||
|
await exportData(records, format);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const config = buildConnConfig();
|
||||||
|
if (!config) {
|
||||||
|
await exportData(records, format);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const dbType = config.type || '';
|
||||||
|
const pkWhere = buildPkWhereSql(records, dbType);
|
||||||
|
if (!pkWhere) {
|
||||||
|
await exportData(records, format);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sql = `SELECT * FROM ${quoteQualifiedIdent(dbType, tableName)} WHERE ${pkWhere}`;
|
||||||
|
await exportByQuery(sql, format, tableName || 'export');
|
||||||
|
}, [getTargets, connectionId, tableName, hasChanges, exportData, buildConnConfig, buildPkWhereSql, exportByQuery]);
|
||||||
|
|
||||||
// Export
|
// Export
|
||||||
const handleExport = async (format: string) => {
|
const handleExport = async (format: string) => {
|
||||||
@@ -646,7 +974,7 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
// 1. Export Selected
|
// 1. Export Selected
|
||||||
if (selectedRowKeys.length > 0) {
|
if (selectedRowKeys.length > 0) {
|
||||||
const selectedRows = displayData.filter(d => selectedRowKeys.includes(d?.[GONAVI_ROW_KEY]));
|
const selectedRows = displayData.filter(d => selectedRowKeys.includes(d?.[GONAVI_ROW_KEY]));
|
||||||
await exportData(selectedRows, format);
|
await handleExportSelected(format, selectedRows[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -655,9 +983,8 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
let instance: any;
|
let instance: any;
|
||||||
const handleAll = async () => {
|
const handleAll = async () => {
|
||||||
instance.destroy();
|
instance.destroy();
|
||||||
const conn = connections.find(c => c.id === connectionId);
|
const config = buildConnConfig();
|
||||||
if (!conn) return;
|
if (!config) return;
|
||||||
const config = { ...conn.config, port: Number(conn.config.port), password: conn.config.password || "", database: conn.config.database || "", useSSH: conn.config.useSSH || false, ssh: conn.config.ssh || { host: "", port: 22, user: "", password: "", keyPath: "" } };
|
|
||||||
const hide = message.loading(`正在导出全部数据...`, 0);
|
const hide = message.loading(`正在导出全部数据...`, 0);
|
||||||
const res = await ExportTable(config as any, dbName || '', tableName, format);
|
const res = await ExportTable(config as any, dbName || '', tableName, format);
|
||||||
hide();
|
hide();
|
||||||
@@ -665,7 +992,25 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
};
|
};
|
||||||
const handlePage = async () => {
|
const handlePage = async () => {
|
||||||
instance.destroy();
|
instance.destroy();
|
||||||
await exportData(displayData, format);
|
if (hasChanges) {
|
||||||
|
message.warning("当前存在未提交修改,导出将按界面数据生成;如需完整长字段建议先提交后再导出。");
|
||||||
|
await exportData(displayData, format);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const config = buildConnConfig();
|
||||||
|
if (!config) {
|
||||||
|
await exportData(displayData, format);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sql = buildCurrentPageSql(config.type || '');
|
||||||
|
if (!sql) {
|
||||||
|
await exportData(displayData, format);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await exportByQuery(sql, format, tableName || 'export');
|
||||||
};
|
};
|
||||||
|
|
||||||
instance = modal.info({
|
instance = modal.info({
|
||||||
@@ -688,21 +1033,64 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
|
|
||||||
const handleImport = async () => {
|
const handleImport = async () => {
|
||||||
if (!connectionId || !tableName) return;
|
if (!connectionId || !tableName) return;
|
||||||
const conn = connections.find(c => c.id === connectionId);
|
const config = buildConnConfig();
|
||||||
if (!conn) return;
|
if (!config) return;
|
||||||
const config = { ...conn.config, port: Number(conn.config.port), password: conn.config.password || "", database: conn.config.database || "", useSSH: conn.config.useSSH || false, ssh: conn.config.ssh || { host: "", port: 22, user: "", password: "", keyPath: "" } };
|
|
||||||
|
|
||||||
const res = await ImportData(config as any, dbName || '', tableName);
|
const res = await ImportData(config as any, dbName || '', tableName);
|
||||||
if (res.success) { message.success(res.message); if (onReload) onReload(); } else if (res.message !== "Cancelled") { message.error("Import Failed: " + res.message); }
|
if (res.success) { message.success(res.message); if (onReload) onReload(); } else if (res.message !== "Cancelled") { message.error("Import Failed: " + res.message); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Filters
|
// Filters
|
||||||
|
const filterOpOptions = useMemo(() => ([
|
||||||
|
{ value: '=', label: '=' },
|
||||||
|
{ value: '!=', label: '!=' },
|
||||||
|
{ value: '<', label: '<' },
|
||||||
|
{ value: '<=', label: '<=' },
|
||||||
|
{ value: '>', label: '>' },
|
||||||
|
{ value: '>=', label: '>=' },
|
||||||
|
{ value: 'CONTAINS', label: '包含' },
|
||||||
|
{ value: 'NOT_CONTAINS', label: '不包含' },
|
||||||
|
{ value: 'STARTS_WITH', label: '开始以' },
|
||||||
|
{ value: 'NOT_STARTS_WITH', label: '不是开始于' },
|
||||||
|
{ value: 'ENDS_WITH', label: '结束以' },
|
||||||
|
{ value: 'NOT_ENDS_WITH', label: '不是结束于' },
|
||||||
|
{ value: 'IS_NULL', label: '是 null' },
|
||||||
|
{ value: 'IS_NOT_NULL', label: '不是 null' },
|
||||||
|
{ value: 'IS_EMPTY', label: '是空的' },
|
||||||
|
{ value: 'IS_NOT_EMPTY', label: '不是空的' },
|
||||||
|
{ value: 'BETWEEN', label: '介于' },
|
||||||
|
{ value: 'NOT_BETWEEN', label: '不介于' },
|
||||||
|
{ value: 'IN', label: '在列表' },
|
||||||
|
{ value: 'NOT_IN', label: '不在列表' },
|
||||||
|
{ value: 'CUSTOM', label: '[自定义]' },
|
||||||
|
]), []);
|
||||||
|
|
||||||
|
const isNoValueOp = useCallback((op: string) => (
|
||||||
|
op === 'IS_NULL' || op === 'IS_NOT_NULL' || op === 'IS_EMPTY' || op === 'IS_NOT_EMPTY'
|
||||||
|
), []);
|
||||||
|
const isBetweenOp = useCallback((op: string) => op === 'BETWEEN' || op === 'NOT_BETWEEN', []);
|
||||||
|
const isListOp = useCallback((op: string) => op === 'IN' || op === 'NOT_IN', []);
|
||||||
|
|
||||||
const addFilter = () => {
|
const addFilter = () => {
|
||||||
setFilterConditions([...filterConditions, { id: nextFilterId, column: columnNames[0] || '', op: '=', value: '' }]);
|
setFilterConditions([...filterConditions, { id: nextFilterId, column: columnNames[0] || '', op: '=', value: '', value2: '' }]);
|
||||||
setNextFilterId(nextFilterId + 1);
|
setNextFilterId(nextFilterId + 1);
|
||||||
};
|
};
|
||||||
const updateFilter = (id: number, field: string, val: string) => {
|
const updateFilter = (id: number, field: string, val: string) => {
|
||||||
setFilterConditions(prev => prev.map(c => c.id === id ? { ...c, [field]: val } : c));
|
setFilterConditions(prev => prev.map(c => {
|
||||||
|
if (c.id !== id) return c;
|
||||||
|
const next: any = { ...c, [field]: val };
|
||||||
|
if (field === 'op') {
|
||||||
|
if (isNoValueOp(val)) {
|
||||||
|
next.value = '';
|
||||||
|
next.value2 = '';
|
||||||
|
} else if (isBetweenOp(val)) {
|
||||||
|
if (typeof next.value2 !== 'string') next.value2 = '';
|
||||||
|
} else {
|
||||||
|
next.value2 = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
const removeFilter = (id: number) => {
|
const removeFilter = (id: number) => {
|
||||||
setFilterConditions(prev => prev.filter(c => c.id !== id));
|
setFilterConditions(prev => prev.filter(c => c.id !== id));
|
||||||
@@ -723,33 +1111,41 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
header: { cell: ResizableTitle }
|
header: { cell: ResizableTitle }
|
||||||
}), []);
|
}), []);
|
||||||
|
|
||||||
const totalWidth = columns.reduce((sum, col) => sum + (col.width as number || 200), 0);
|
const totalWidth = columns.reduce((sum, col) => sum + (Number(col.width) || 200), 0) + selectionColumnWidth;
|
||||||
const enableVirtual = mergedDisplayData.length >= 200;
|
const enableVirtual = mergedDisplayData.length >= 200;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={gridId} style={{ height: '100%', overflow: 'hidden', padding: 0, display: 'flex', flexDirection: 'column', minHeight: 0 }}>
|
<div className={gridId} style={{ flex: '1 1 auto', height: '100%', overflow: 'hidden', padding: 0, display: 'flex', flexDirection: 'column', minHeight: 0 }}>
|
||||||
{/* Toolbar */}
|
{/* Toolbar */}
|
||||||
<div style={{ padding: '8px', borderBottom: '1px solid #eee', display: 'flex', gap: 8, alignItems: 'center' }}>
|
<div style={{ padding: '8px', borderBottom: '1px solid #eee', display: 'flex', gap: 8, alignItems: 'center' }}>
|
||||||
{onReload && <Button icon={<ReloadOutlined />} onClick={() => {
|
{onReload && <Button icon={<ReloadOutlined />} onClick={() => {
|
||||||
setAddedRows([]);
|
setAddedRows([]);
|
||||||
setModifiedRows({});
|
setModifiedRows({});
|
||||||
setDeletedRowKeys(new Set());
|
setDeletedRowKeys(new Set());
|
||||||
setSelectedRowKeys([]);
|
setSelectedRowKeys([]);
|
||||||
onReload();
|
setActiveCell(null);
|
||||||
}}>刷新</Button>}
|
onReload();
|
||||||
{tableName && <Button icon={<ImportOutlined />} onClick={handleImport}>导入</Button>}
|
}}>刷新</Button>}
|
||||||
{tableName && <Dropdown menu={{ items: exportMenu }}><Button icon={<ExportOutlined />}>导出 <DownOutlined /></Button></Dropdown>}
|
{tableName && <Button icon={<ImportOutlined />} onClick={handleImport}>导入</Button>}
|
||||||
|
{tableName && <Dropdown menu={{ items: exportMenu }}><Button icon={<ExportOutlined />}>导出 <DownOutlined /></Button></Dropdown>}
|
||||||
{!readOnly && tableName && (
|
|
||||||
<>
|
{!readOnly && tableName && (
|
||||||
<div style={{ width: 1, background: '#eee', height: 20, margin: '0 8px' }} />
|
<>
|
||||||
<Button icon={<PlusOutlined />} onClick={handleAddRow}>添加行</Button>
|
<div style={{ width: 1, background: '#eee', height: 20, margin: '0 8px' }} />
|
||||||
<Button icon={<DeleteOutlined />} danger disabled={selectedRowKeys.length === 0} onClick={handleDeleteSelected}>删除选中</Button>
|
<Button icon={<PlusOutlined />} onClick={handleAddRow}>添加行</Button>
|
||||||
{selectedRowKeys.length > 0 && <span style={{ fontSize: '12px', color: '#888' }}>已选 {selectedRowKeys.length}</span>}
|
<Button
|
||||||
<div style={{ width: 1, background: '#eee', height: 20, margin: '0 8px' }} />
|
icon={<EditOutlined />}
|
||||||
<Button icon={<SaveOutlined />} type="primary" disabled={!hasChanges} onClick={handleCommit}>提交事务 ({addedRows.length + Object.keys(modifiedRows).length + deletedRowKeys.size})</Button>
|
disabled={selectedRowKeys.length > 1 || (selectedRowKeys.length !== 1 && !activeCell)}
|
||||||
{hasChanges && (<Button icon={<UndoOutlined />} onClick={() => {
|
onClick={openRowEditor}
|
||||||
setAddedRows([]);
|
>
|
||||||
|
编辑行
|
||||||
|
</Button>
|
||||||
|
<Button icon={<DeleteOutlined />} danger disabled={selectedRowKeys.length === 0} onClick={handleDeleteSelected}>删除选中</Button>
|
||||||
|
{selectedRowKeys.length > 0 && <span style={{ fontSize: '12px', color: '#888' }}>已选 {selectedRowKeys.length}</span>}
|
||||||
|
<div style={{ width: 1, background: '#eee', height: 20, margin: '0 8px' }} />
|
||||||
|
<Button icon={<SaveOutlined />} type="primary" disabled={!hasChanges} onClick={handleCommit}>提交事务 ({addedRows.length + Object.keys(modifiedRows).length + deletedRowKeys.size})</Button>
|
||||||
|
{hasChanges && (<Button icon={<UndoOutlined />} onClick={() => {
|
||||||
|
setAddedRows([]);
|
||||||
setModifiedRows({});
|
setModifiedRows({});
|
||||||
setDeletedRowKeys(new Set());
|
setDeletedRowKeys(new Set());
|
||||||
}}>回滚</Button>)}
|
}}>回滚</Button>)}
|
||||||
@@ -771,10 +1167,62 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
{showFilter && (
|
{showFilter && (
|
||||||
<div style={{ padding: '8px', background: '#f5f5f5', borderBottom: '1px solid #eee' }}>
|
<div style={{ padding: '8px', background: '#f5f5f5', borderBottom: '1px solid #eee' }}>
|
||||||
{filterConditions.map(cond => (
|
{filterConditions.map(cond => (
|
||||||
<div key={cond.id} style={{ display: 'flex', gap: 8, marginBottom: 8 }}>
|
<div key={cond.id} style={{ display: 'flex', gap: 8, marginBottom: 8, alignItems: 'flex-start' }}>
|
||||||
<Select style={{ width: 150 }} value={cond.column} onChange={v => updateFilter(cond.id, 'column', v)} options={columnNames.map(c => ({ value: c, label: c }))} />
|
<Select
|
||||||
<Select style={{ width: 100 }} value={cond.op} onChange={v => updateFilter(cond.id, 'op', v)} options={[{ value: '=', label: '=' }, { value: 'LIKE', label: '包含' }]} />
|
style={{ width: 180 }}
|
||||||
<Input style={{ width: 200 }} value={cond.value} onChange={e => updateFilter(cond.id, 'value', e.target.value)} />
|
value={cond.column}
|
||||||
|
onChange={v => updateFilter(cond.id, 'column', v)}
|
||||||
|
options={columnNames.map(c => ({ value: c, label: c }))}
|
||||||
|
disabled={cond.op === 'CUSTOM'}
|
||||||
|
/>
|
||||||
|
<Select
|
||||||
|
style={{ width: 140 }}
|
||||||
|
value={cond.op}
|
||||||
|
onChange={v => updateFilter(cond.id, 'op', v)}
|
||||||
|
options={filterOpOptions as any}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{cond.op === 'CUSTOM' ? (
|
||||||
|
<Input.TextArea
|
||||||
|
style={{ flex: 1 }}
|
||||||
|
autoSize={{ minRows: 1, maxRows: 4 }}
|
||||||
|
value={cond.value}
|
||||||
|
onChange={e => updateFilter(cond.id, 'value', e.target.value)}
|
||||||
|
placeholder="输入自定义 WHERE 表达式(不需要再写 WHERE),例如:status IN ('A','B')"
|
||||||
|
/>
|
||||||
|
) : isListOp(cond.op) ? (
|
||||||
|
<Input.TextArea
|
||||||
|
style={{ flex: 1 }}
|
||||||
|
autoSize={{ minRows: 1, maxRows: 4 }}
|
||||||
|
value={cond.value}
|
||||||
|
onChange={e => updateFilter(cond.id, 'value', e.target.value)}
|
||||||
|
placeholder="多个值用逗号或换行分隔"
|
||||||
|
/>
|
||||||
|
) : isBetweenOp(cond.op) ? (
|
||||||
|
<>
|
||||||
|
<Input
|
||||||
|
style={{ width: 220 }}
|
||||||
|
value={cond.value}
|
||||||
|
onChange={e => updateFilter(cond.id, 'value', e.target.value)}
|
||||||
|
placeholder="开始值"
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
style={{ width: 220 }}
|
||||||
|
value={cond.value2 || ''}
|
||||||
|
onChange={e => updateFilter(cond.id, 'value2', e.target.value)}
|
||||||
|
placeholder="结束值"
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
) : isNoValueOp(cond.op) ? (
|
||||||
|
<Input style={{ width: 220 }} value="" disabled placeholder="无需输入值" />
|
||||||
|
) : (
|
||||||
|
<Input
|
||||||
|
style={{ width: 280 }}
|
||||||
|
value={cond.value}
|
||||||
|
onChange={e => updateFilter(cond.id, 'value', e.target.value)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<Button icon={<CloseOutlined />} onClick={() => removeFilter(cond.id)} type="text" danger />
|
<Button icon={<CloseOutlined />} onClick={() => removeFilter(cond.id)} type="text" danger />
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
@@ -789,8 +1237,90 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div ref={containerRef} style={{ flex: 1, overflow: 'hidden', position: 'relative', minHeight: 0 }}>
|
<div ref={containerRef} style={{ flex: 1, overflow: 'hidden', position: 'relative', minHeight: 0 }}>
|
||||||
{contextHolder}
|
{contextHolder}
|
||||||
|
<Modal
|
||||||
|
title="编辑行"
|
||||||
|
open={rowEditorOpen}
|
||||||
|
onCancel={closeRowEditor}
|
||||||
|
width={980}
|
||||||
|
destroyOnClose
|
||||||
|
maskClosable={false}
|
||||||
|
footer={[
|
||||||
|
<Button key="cancel" onClick={closeRowEditor}>取消</Button>,
|
||||||
|
<Button key="ok" type="primary" onClick={applyRowEditor}>应用</Button>,
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<div style={{ marginBottom: 8, color: '#888', fontSize: 12, display: 'flex', justifyContent: 'space-between', gap: 8 }}>
|
||||||
|
<span>{tableName ? `${tableName}` : ''}</span>
|
||||||
|
<span>{rowEditorRowKey ? `rowKey: ${rowEditorRowKey}` : ''}</span>
|
||||||
|
</div>
|
||||||
|
<Form form={rowEditorForm} layout="vertical">
|
||||||
|
<div style={{ maxHeight: '62vh', overflow: 'auto', paddingRight: 8 }}>
|
||||||
|
{columnNames.map((col) => {
|
||||||
|
const sample = rowEditorDisplayRef.current?.[col] ?? '';
|
||||||
|
const placeholder = rowEditorNullColsRef.current?.has(col) ? '(NULL)' : undefined;
|
||||||
|
const isJson = looksLikeJsonText(sample);
|
||||||
|
const useArea = isJson || sample.includes('\n') || sample.length >= 160;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Form.Item key={col} label={col} style={{ marginBottom: 12 }}>
|
||||||
|
<div style={{ display: 'flex', gap: 8, alignItems: 'flex-start' }}>
|
||||||
|
<Form.Item name={col} noStyle>
|
||||||
|
{useArea ? (
|
||||||
|
<Input.TextArea
|
||||||
|
style={{ flex: 1 }}
|
||||||
|
autoSize={{ minRows: isJson ? 4 : 1, maxRows: 10 }}
|
||||||
|
placeholder={placeholder}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<Input style={{ flex: 1 }} placeholder={placeholder} />
|
||||||
|
)}
|
||||||
|
</Form.Item>
|
||||||
|
<Button size="small" onClick={() => openRowEditorFieldEditor(col)} title="弹窗编辑">...</Button>
|
||||||
|
</div>
|
||||||
|
</Form.Item>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</Form>
|
||||||
|
</Modal>
|
||||||
|
<Modal
|
||||||
|
title={cellEditorMeta ? `编辑单元格:${cellEditorMeta.title}` : '编辑单元格'}
|
||||||
|
open={cellEditorOpen}
|
||||||
|
onCancel={closeCellEditor}
|
||||||
|
width={960}
|
||||||
|
destroyOnClose
|
||||||
|
maskClosable={false}
|
||||||
|
footer={[
|
||||||
|
<Button key="format" onClick={handleFormatJsonInEditor} disabled={!cellEditorIsJson}>
|
||||||
|
格式化 JSON
|
||||||
|
</Button>,
|
||||||
|
<Button key="cancel" onClick={closeCellEditor}>取消</Button>,
|
||||||
|
<Button key="ok" type="primary" onClick={handleCellEditorSave}>保存</Button>,
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<div style={{ marginBottom: 8, color: '#888', fontSize: 12 }}>
|
||||||
|
{cellEditorMeta ? `${tableName || ''}${tableName ? '.' : ''}${cellEditorMeta.dataIndex}` : ''}
|
||||||
|
</div>
|
||||||
|
{cellEditorOpen && (
|
||||||
|
<Editor
|
||||||
|
height="56vh"
|
||||||
|
language={cellEditorIsJson ? "json" : "plaintext"}
|
||||||
|
theme={darkMode ? "vs-dark" : "light"}
|
||||||
|
value={cellEditorValue}
|
||||||
|
onChange={(val) => setCellEditorValue(val || '')}
|
||||||
|
options={{
|
||||||
|
minimap: { enabled: false },
|
||||||
|
scrollBeyondLastLine: false,
|
||||||
|
wordWrap: "on",
|
||||||
|
fontSize: 14,
|
||||||
|
tabSize: 2,
|
||||||
|
automaticLayout: true,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Modal>
|
||||||
<Form component={false} form={form}>
|
<Form component={false} form={form}>
|
||||||
<DataContext.Provider value={{ selectedRowKeysRef, displayDataRef, handleCopyInsert, handleCopyJson, handleCopyCsv, handleExportSelected, copyToClipboard, tableName }}>
|
<DataContext.Provider value={{ selectedRowKeysRef, displayDataRef, handleCopyInsert, handleCopyJson, handleCopyCsv, handleExportSelected, copyToClipboard, tableName }}>
|
||||||
<EditableContext.Provider value={form}>
|
<EditableContext.Provider value={form}>
|
||||||
@@ -799,6 +1329,7 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
dataSource={mergedDisplayData}
|
dataSource={mergedDisplayData}
|
||||||
columns={mergedColumns}
|
columns={mergedColumns}
|
||||||
size="small"
|
size="small"
|
||||||
|
tableLayout="fixed"
|
||||||
scroll={{ x: Math.max(totalWidth, 1000), y: tableHeight }}
|
scroll={{ x: Math.max(totalWidth, 1000), y: tableHeight }}
|
||||||
virtual={enableVirtual}
|
virtual={enableVirtual}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
@@ -809,6 +1340,7 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
rowSelection={{
|
rowSelection={{
|
||||||
selectedRowKeys,
|
selectedRowKeys,
|
||||||
onChange: setSelectedRowKeys,
|
onChange: setSelectedRowKeys,
|
||||||
|
columnWidth: selectionColumnWidth,
|
||||||
}}
|
}}
|
||||||
rowClassName={(record) => {
|
rowClassName={(record) => {
|
||||||
const k = record?.[GONAVI_ROW_KEY];
|
const k = record?.[GONAVI_ROW_KEY];
|
||||||
@@ -842,14 +1374,14 @@ const DataGrid: React.FC<DataGridProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<style>{`
|
<style>{`
|
||||||
.${gridId} .row-added td { background-color: #f6ffed !important; }
|
.${gridId} .row-added td { background-color: #f6ffed !important; }
|
||||||
.${gridId} .row-modified td { background-color: #e6f7ff !important; }
|
.${gridId} .row-modified td { background-color: #e6f7ff !important; }
|
||||||
.${gridId} .ant-table-body {
|
.${gridId} td.gonavi-active-cell {
|
||||||
height: ${tableHeight}px !important;
|
outline: 2px solid #1677ff;
|
||||||
max-height: ${tableHeight}px !important;
|
outline-offset: -2px;
|
||||||
}
|
}
|
||||||
`}</style>
|
`}</style>
|
||||||
|
|
||||||
{/* Ghost Resize Line for Columns */}
|
{/* Ghost Resize Line for Columns */}
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { TabData, ColumnDefinition } from '../types';
|
|||||||
import { useStore } from '../store';
|
import { useStore } from '../store';
|
||||||
import { DBQuery, DBGetColumns } from '../../wailsjs/go/app/App';
|
import { DBQuery, DBGetColumns } from '../../wailsjs/go/app/App';
|
||||||
import DataGrid, { GONAVI_ROW_KEY } from './DataGrid';
|
import DataGrid, { GONAVI_ROW_KEY } from './DataGrid';
|
||||||
|
import { buildWhereSQL, quoteIdentPart, quoteQualifiedIdent } from '../utils/sql';
|
||||||
|
|
||||||
const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
||||||
const [data, setData] = useState<any[]>([]);
|
const [data, setData] = useState<any[]>([]);
|
||||||
@@ -14,6 +15,8 @@ const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
|||||||
const fetchSeqRef = useRef(0);
|
const fetchSeqRef = useRef(0);
|
||||||
const countSeqRef = useRef(0);
|
const countSeqRef = useRef(0);
|
||||||
const countKeyRef = useRef<string>('');
|
const countKeyRef = useRef<string>('');
|
||||||
|
const pkSeqRef = useRef(0);
|
||||||
|
const pkKeyRef = useRef<string>('');
|
||||||
|
|
||||||
const [pagination, setPagination] = useState({
|
const [pagination, setPagination] = useState({
|
||||||
current: 1,
|
current: 1,
|
||||||
@@ -27,6 +30,13 @@ const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
|||||||
const [showFilter, setShowFilter] = useState(false);
|
const [showFilter, setShowFilter] = useState(false);
|
||||||
const [filterConditions, setFilterConditions] = useState<any[]>([]);
|
const [filterConditions, setFilterConditions] = useState<any[]>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setPkColumns([]);
|
||||||
|
pkKeyRef.current = '';
|
||||||
|
countKeyRef.current = '';
|
||||||
|
setPagination(prev => ({ ...prev, current: 1, total: 0, totalKnown: false }));
|
||||||
|
}, [tab.connectionId, tab.dbName, tab.tableName]);
|
||||||
|
|
||||||
const fetchData = useCallback(async (page = pagination.current, size = pagination.pageSize) => {
|
const fetchData = useCallback(async (page = pagination.current, size = pagination.pageSize) => {
|
||||||
const seq = ++fetchSeqRef.current;
|
const seq = ++fetchSeqRef.current;
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -46,40 +56,18 @@ const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
|||||||
ssh: conn.config.ssh || { host: "", port: 22, user: "", password: "", keyPath: "" }
|
ssh: conn.config.ssh || { host: "", port: 22, user: "", password: "", keyPath: "" }
|
||||||
};
|
};
|
||||||
|
|
||||||
const quoteIdentPart = (ident: string) => {
|
const dbType = config.type || '';
|
||||||
if (!ident) return ident;
|
|
||||||
if (config.type === 'mysql') return `\`${ident.replace(/`/g, '``')}\``;
|
|
||||||
return `"${ident.replace(/"/g, '""')}"`;
|
|
||||||
};
|
|
||||||
const quoteQualifiedIdent = (ident: string) => {
|
|
||||||
const raw = (ident || '').trim();
|
|
||||||
if (!raw) return raw;
|
|
||||||
const parts = raw.split('.').filter(Boolean);
|
|
||||||
if (parts.length <= 1) return quoteIdentPart(raw);
|
|
||||||
return parts.map(quoteIdentPart).join('.');
|
|
||||||
};
|
|
||||||
const escapeLiteral = (val: string) => val.replace(/'/g, "''");
|
|
||||||
|
|
||||||
const dbName = tab.dbName || '';
|
const dbName = tab.dbName || '';
|
||||||
const tableName = tab.tableName || '';
|
const tableName = tab.tableName || '';
|
||||||
|
|
||||||
const whereParts: string[] = [];
|
const whereSQL = buildWhereSQL(dbType, filterConditions);
|
||||||
filterConditions.forEach(cond => {
|
|
||||||
if (cond.column && cond.value) {
|
|
||||||
if (cond.op === 'LIKE') {
|
|
||||||
whereParts.push(`${quoteIdentPart(cond.column)} LIKE '%${escapeLiteral(cond.value)}%'`);
|
|
||||||
} else {
|
|
||||||
whereParts.push(`${quoteIdentPart(cond.column)} ${cond.op} '${escapeLiteral(cond.value)}'`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const whereSQL = whereParts.length > 0 ? `WHERE ${whereParts.join(' AND ')}` : "";
|
|
||||||
|
|
||||||
const countSql = `SELECT COUNT(*) as total FROM ${quoteQualifiedIdent(tableName)} ${whereSQL}`;
|
const countSql = `SELECT COUNT(*) as total FROM ${quoteQualifiedIdent(dbType, tableName)} ${whereSQL}`;
|
||||||
|
|
||||||
let sql = `SELECT * FROM ${quoteQualifiedIdent(tableName)} ${whereSQL}`;
|
let sql = `SELECT * FROM ${quoteQualifiedIdent(dbType, tableName)} ${whereSQL}`;
|
||||||
if (sortInfo && sortInfo.order) {
|
if (sortInfo && sortInfo.order) {
|
||||||
sql += ` ORDER BY ${quoteIdentPart(sortInfo.columnKey)} ${sortInfo.order === 'ascend' ? 'ASC' : 'DESC'}`;
|
sql += ` ORDER BY ${quoteIdentPart(dbType, sortInfo.columnKey)} ${sortInfo.order === 'ascend' ? 'ASC' : 'DESC'}`;
|
||||||
}
|
}
|
||||||
const offset = (page - 1) * size;
|
const offset = (page - 1) * size;
|
||||||
// 大表性能:打开表不阻塞在 COUNT(*),先通过多取 1 条判断是否还有下一页;总数在后台统计并异步回填。
|
// 大表性能:打开表不阻塞在 COUNT(*),先通过多取 1 条判断是否还有下一页;总数在后台统计并异步回填。
|
||||||
@@ -89,11 +77,6 @@ const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
|||||||
try {
|
try {
|
||||||
const pData = DBQuery(config as any, dbName, sql);
|
const pData = DBQuery(config as any, dbName, sql);
|
||||||
|
|
||||||
let pCols: Promise<any> | null = null;
|
|
||||||
if (pkColumns.length === 0) {
|
|
||||||
pCols = DBGetColumns(config as any, dbName, tableName);
|
|
||||||
}
|
|
||||||
|
|
||||||
const resData = await pData;
|
const resData = await pData;
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
|
|
||||||
@@ -109,11 +92,23 @@ const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
|||||||
dbName
|
dbName
|
||||||
});
|
});
|
||||||
|
|
||||||
if (pCols) {
|
if (pkColumns.length === 0) {
|
||||||
const resCols = await pCols;
|
const pkKey = `${tab.connectionId}|${dbName}|${tableName}`;
|
||||||
if (resCols.success) {
|
if (pkKeyRef.current !== pkKey) {
|
||||||
const pks = (resCols.data as ColumnDefinition[]).filter(c => c.key === 'PRI').map(c => c.name);
|
pkKeyRef.current = pkKey;
|
||||||
setPkColumns(pks);
|
const pkSeq = ++pkSeqRef.current;
|
||||||
|
DBGetColumns(config as any, dbName, tableName)
|
||||||
|
.then((resCols: any) => {
|
||||||
|
if (pkSeqRef.current !== pkSeq) return;
|
||||||
|
if (pkKeyRef.current !== pkKey) return;
|
||||||
|
if (!resCols?.success) return;
|
||||||
|
const pks = (resCols.data as ColumnDefinition[]).filter((c: any) => c.key === 'PRI').map((c: any) => c.name);
|
||||||
|
setPkColumns(pks);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (pkSeqRef.current !== pkSeq) return;
|
||||||
|
if (pkKeyRef.current !== pkKey) return;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +222,7 @@ const DataViewer: React.FC<{ tab: TabData }> = ({ tab }) => {
|
|||||||
}, [tab, sortInfo, filterConditions]); // Initial load and re-load on sort/filter
|
}, [tab, sortInfo, filterConditions]); // Initial load and re-load on sort/filter
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ height: '100%', width: '100%', overflow: 'hidden' }}>
|
<div style={{ flex: '1 1 auto', minHeight: 0, height: '100%', width: '100%', overflow: 'hidden', display: 'flex', flexDirection: 'column' }}>
|
||||||
<DataGrid
|
<DataGrid
|
||||||
data={data}
|
data={data}
|
||||||
columnNames={columnNames}
|
columnNames={columnNames}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -47,6 +47,8 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
const [expandedKeys, setExpandedKeys] = useState<React.Key[]>([]);
|
const [expandedKeys, setExpandedKeys] = useState<React.Key[]>([]);
|
||||||
const [autoExpandParent, setAutoExpandParent] = useState(true);
|
const [autoExpandParent, setAutoExpandParent] = useState(true);
|
||||||
const [loadedKeys, setLoadedKeys] = useState<React.Key[]>([]);
|
const [loadedKeys, setLoadedKeys] = useState<React.Key[]>([]);
|
||||||
|
const [selectedKeys, setSelectedKeys] = useState<React.Key[]>([]);
|
||||||
|
const [selectedNodes, setSelectedNodes] = useState<any[]>([]);
|
||||||
const [contextMenu, setContextMenu] = useState<{ x: number, y: number, items: MenuProps['items'] } | null>(null);
|
const [contextMenu, setContextMenu] = useState<{ x: number, y: number, items: MenuProps['items'] } | null>(null);
|
||||||
|
|
||||||
// Virtual Scroll State
|
// Virtual Scroll State
|
||||||
@@ -283,10 +285,14 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onSelect = (keys: React.Key[], info: any) => {
|
const onSelect = (keys: React.Key[], info: any) => {
|
||||||
if (!info.node.selected) {
|
setSelectedKeys(keys);
|
||||||
|
setSelectedNodes(info.selectedNodes || []);
|
||||||
|
|
||||||
|
if (keys.length === 0) {
|
||||||
setActiveContext(null);
|
setActiveContext(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!info.selected) return;
|
||||||
|
|
||||||
const { type, dataRef, key, title } = info.node;
|
const { type, dataRef, key, title } = info.node;
|
||||||
|
|
||||||
@@ -313,15 +319,6 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onDoubleClick = (e: any, node: any) => {
|
const onDoubleClick = (e: any, node: any) => {
|
||||||
const key = node.key;
|
|
||||||
const isExpanded = expandedKeys.includes(key);
|
|
||||||
const newExpandedKeys = isExpanded
|
|
||||||
? expandedKeys.filter(k => k !== key)
|
|
||||||
: [...expandedKeys, key];
|
|
||||||
|
|
||||||
setExpandedKeys(newExpandedKeys);
|
|
||||||
if (!isExpanded) setAutoExpandParent(false);
|
|
||||||
|
|
||||||
if (node.type === 'table') {
|
if (node.type === 'table') {
|
||||||
const { tableName, dbName, id } = node.dataRef;
|
const { tableName, dbName, id } = node.dataRef;
|
||||||
addTab({
|
addTab({
|
||||||
@@ -332,6 +329,7 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
dbName,
|
dbName,
|
||||||
tableName,
|
tableName,
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
} else if (node.type === 'saved-query') {
|
} else if (node.type === 'saved-query') {
|
||||||
const q = node.dataRef;
|
const q = node.dataRef;
|
||||||
addTab({
|
addTab({
|
||||||
@@ -342,7 +340,17 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
dbName: q.dbName,
|
dbName: q.dbName,
|
||||||
query: q.sql
|
query: q.sql
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const key = node.key;
|
||||||
|
const isExpanded = expandedKeys.includes(key);
|
||||||
|
const newExpandedKeys = isExpanded
|
||||||
|
? expandedKeys.filter(k => k !== key)
|
||||||
|
: [...expandedKeys, key];
|
||||||
|
|
||||||
|
setExpandedKeys(newExpandedKeys);
|
||||||
|
if (!isExpanded) setAutoExpandParent(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCopyStructure = async (node: any) => {
|
const handleCopyStructure = async (node: any) => {
|
||||||
@@ -382,6 +390,60 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const normalizeConnConfig = (raw: any) => ({
|
||||||
|
...raw,
|
||||||
|
port: Number(raw.port),
|
||||||
|
password: raw.password || "",
|
||||||
|
database: raw.database || "",
|
||||||
|
useSSH: raw.useSSH || false,
|
||||||
|
ssh: raw.ssh || { host: "", port: 22, user: "", password: "", keyPath: "" }
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleExportDatabaseSQL = async (node: any, includeData: boolean) => {
|
||||||
|
const conn = node.dataRef;
|
||||||
|
const dbName = conn.dbName || node.title;
|
||||||
|
const hide = message.loading(includeData ? `正在备份数据库 ${dbName} (结构+数据)...` : `正在导出数据库 ${dbName} 表结构...`, 0);
|
||||||
|
try {
|
||||||
|
const res = await (window as any).go.app.App.ExportDatabaseSQL(normalizeConnConfig(conn.config), dbName, includeData);
|
||||||
|
hide();
|
||||||
|
if (res.success) {
|
||||||
|
message.success('导出成功');
|
||||||
|
} else if (res.message !== 'Cancelled') {
|
||||||
|
message.error('导出失败: ' + res.message);
|
||||||
|
}
|
||||||
|
} catch (e: any) {
|
||||||
|
hide();
|
||||||
|
message.error('导出失败: ' + (e?.message || String(e)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleExportTablesSQL = async (nodes: any[], includeData: boolean) => {
|
||||||
|
if (!nodes || nodes.length === 0) return;
|
||||||
|
const first = nodes[0].dataRef;
|
||||||
|
const dbName = first.dbName;
|
||||||
|
const connId = first.id;
|
||||||
|
const allSame = nodes.every(n => n?.dataRef?.id === connId && n?.dataRef?.dbName === dbName);
|
||||||
|
if (!allSame) {
|
||||||
|
message.error('请在同一连接、同一数据库下选择多张表进行导出');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tableNames = nodes.map(n => n.dataRef.tableName).filter(Boolean);
|
||||||
|
const hide = message.loading(includeData ? `正在备份选中表 (${tableNames.length})...` : `正在导出选中表结构 (${tableNames.length})...`, 0);
|
||||||
|
try {
|
||||||
|
const res = await (window as any).go.app.App.ExportTablesSQL(normalizeConnConfig(first.config), dbName, tableNames, includeData);
|
||||||
|
hide();
|
||||||
|
if (res.success) {
|
||||||
|
message.success('导出成功');
|
||||||
|
} else if (res.message !== 'Cancelled') {
|
||||||
|
message.error('导出失败: ' + res.message);
|
||||||
|
}
|
||||||
|
} catch (e: any) {
|
||||||
|
hide();
|
||||||
|
message.error('导出失败: ' + (e?.message || String(e)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleRunSQLFile = async (node: any) => {
|
const handleRunSQLFile = async (node: any) => {
|
||||||
const res = await (window as any).go.app.App.OpenSQLFile();
|
const res = await (window as any).go.app.App.OpenSQLFile();
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
@@ -550,6 +612,18 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
icon: <ReloadOutlined />,
|
icon: <ReloadOutlined />,
|
||||||
onClick: () => loadTables(node)
|
onClick: () => loadTables(node)
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: 'export-db-schema',
|
||||||
|
label: '导出全部表结构 (SQL)',
|
||||||
|
icon: <ExportOutlined />,
|
||||||
|
onClick: () => handleExportDatabaseSQL(node, false)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'backup-db-sql',
|
||||||
|
label: '备份全部表 (结构+数据 SQL)',
|
||||||
|
icon: <SaveOutlined />,
|
||||||
|
onClick: () => handleExportDatabaseSQL(node, true)
|
||||||
|
},
|
||||||
{ type: 'divider' },
|
{ type: 'divider' },
|
||||||
{
|
{
|
||||||
key: 'disconnect-db',
|
key: 'disconnect-db',
|
||||||
@@ -588,7 +662,25 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
} else if (node.type === 'table') {
|
} else if (node.type === 'table') {
|
||||||
|
const sameContextSelectedTables = (selectedNodes || []).filter((n: any) => n?.type === 'table' && n?.dataRef?.id === node?.dataRef?.id && n?.dataRef?.dbName === node?.dataRef?.dbName);
|
||||||
|
const selectedForAction = sameContextSelectedTables.some((n: any) => n?.key === node.key) ? sameContextSelectedTables : [node];
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
...(selectedForAction.length > 1 ? ([
|
||||||
|
{
|
||||||
|
key: 'export-selected-schema',
|
||||||
|
label: `导出选中表结构 (${selectedForAction.length}) (SQL)`,
|
||||||
|
icon: <ExportOutlined />,
|
||||||
|
onClick: () => handleExportTablesSQL(selectedForAction, false)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'backup-selected-sql',
|
||||||
|
label: `备份选中表 (${selectedForAction.length}) (结构+数据 SQL)`,
|
||||||
|
icon: <SaveOutlined />,
|
||||||
|
onClick: () => handleExportTablesSQL(selectedForAction, true)
|
||||||
|
},
|
||||||
|
{ type: 'divider' as const }
|
||||||
|
]) : []),
|
||||||
{
|
{
|
||||||
key: 'new-query',
|
key: 'new-query',
|
||||||
label: '新建查询',
|
label: '新建查询',
|
||||||
@@ -684,6 +776,8 @@ const Sidebar: React.FC<{ onEditConnection?: (conn: SavedConnection) => void }>
|
|||||||
loadedKeys={loadedKeys}
|
loadedKeys={loadedKeys}
|
||||||
onLoad={setLoadedKeys}
|
onLoad={setLoadedKeys}
|
||||||
autoExpandParent={autoExpandParent}
|
autoExpandParent={autoExpandParent}
|
||||||
|
multiple
|
||||||
|
selectedKeys={selectedKeys}
|
||||||
blockNode
|
blockNode
|
||||||
height={treeHeight}
|
height={treeHeight}
|
||||||
onRightClick={onRightClick}
|
onRightClick={onRightClick}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import React, { useMemo } from 'react';
|
import React, { useMemo } from 'react';
|
||||||
import { Tabs, Button } from 'antd';
|
import { Tabs, Dropdown } from 'antd';
|
||||||
|
import type { MenuProps } from 'antd';
|
||||||
import { useStore } from '../store';
|
import { useStore } from '../store';
|
||||||
import DataViewer from './DataViewer';
|
import DataViewer from './DataViewer';
|
||||||
import QueryEditor from './QueryEditor';
|
import QueryEditor from './QueryEditor';
|
||||||
import TableDesigner from './TableDesigner';
|
import TableDesigner from './TableDesigner';
|
||||||
|
|
||||||
const TabManager: React.FC = () => {
|
const TabManager: React.FC = () => {
|
||||||
const { tabs, activeTabId, setActiveTab, closeTab } = useStore();
|
const { tabs, activeTabId, setActiveTab, closeTab, closeOtherTabs, closeTabsToLeft, closeTabsToRight, closeAllTabs } = useStore();
|
||||||
|
|
||||||
const onChange = (newActiveKey: string) => {
|
const onChange = (newActiveKey: string) => {
|
||||||
setActiveTab(newActiveKey);
|
setActiveTab(newActiveKey);
|
||||||
@@ -18,7 +19,7 @@ const TabManager: React.FC = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const items = useMemo(() => tabs.map(tab => {
|
const items = useMemo(() => tabs.map((tab, index) => {
|
||||||
let content;
|
let content;
|
||||||
if (tab.type === 'query') {
|
if (tab.type === 'query') {
|
||||||
content = <QueryEditor tab={tab} />;
|
content = <QueryEditor tab={tab} />;
|
||||||
@@ -27,27 +28,95 @@ const TabManager: React.FC = () => {
|
|||||||
} else if (tab.type === 'design') {
|
} else if (tab.type === 'design') {
|
||||||
content = <TableDesigner tab={tab} />;
|
content = <TableDesigner tab={tab} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const menuItems: MenuProps['items'] = [
|
||||||
|
{
|
||||||
|
key: 'close-other',
|
||||||
|
label: '关闭其他页',
|
||||||
|
disabled: tabs.length <= 1,
|
||||||
|
onClick: () => closeOtherTabs(tab.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'close-left',
|
||||||
|
label: '关闭左侧',
|
||||||
|
disabled: index === 0,
|
||||||
|
onClick: () => closeTabsToLeft(tab.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'close-right',
|
||||||
|
label: '关闭右侧',
|
||||||
|
disabled: index === tabs.length - 1,
|
||||||
|
onClick: () => closeTabsToRight(tab.id),
|
||||||
|
},
|
||||||
|
{ type: 'divider' },
|
||||||
|
{
|
||||||
|
key: 'close-all',
|
||||||
|
label: '关闭所有',
|
||||||
|
disabled: tabs.length === 0,
|
||||||
|
onClick: () => closeAllTabs(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
label: tab.title,
|
label: (
|
||||||
|
<Dropdown menu={{ items: menuItems }} trigger={['contextMenu']}>
|
||||||
|
<span onContextMenu={(e) => e.preventDefault()}>{tab.title}</span>
|
||||||
|
</Dropdown>
|
||||||
|
),
|
||||||
key: tab.id,
|
key: tab.id,
|
||||||
children: content,
|
children: content,
|
||||||
};
|
};
|
||||||
}), [tabs]);
|
}), [tabs, closeOtherTabs, closeTabsToLeft, closeTabsToRight, closeAllTabs]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<style>{`
|
<style>{`
|
||||||
.ant-tabs-content { height: 100%; }
|
.main-tabs {
|
||||||
.ant-tabs-tabpane { height: 100%; }
|
height: 100%;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-height: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.main-tabs .ant-tabs-nav {
|
||||||
|
flex: 0 0 auto;
|
||||||
|
}
|
||||||
|
.main-tabs .ant-tabs-content-holder {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-height: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
.main-tabs .ant-tabs-content {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-height: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
.main-tabs .ant-tabs-tabpane {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-height: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
.main-tabs .ant-tabs-tabpane > div {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
.main-tabs .ant-tabs-tabpane-hidden {
|
||||||
|
display: none !important;
|
||||||
|
}
|
||||||
`}</style>
|
`}</style>
|
||||||
<Tabs
|
<Tabs
|
||||||
|
className="main-tabs"
|
||||||
type="editable-card"
|
type="editable-card"
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
activeKey={activeTabId || undefined}
|
activeKey={activeTabId || undefined}
|
||||||
onEdit={onEdit}
|
onEdit={onEdit}
|
||||||
items={items}
|
items={items}
|
||||||
style={{ height: '100%' }}
|
|
||||||
hideAdd
|
hideAdd
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -550,7 +550,6 @@ const TableDesigner: React.FC<{ tab: TabData }> = ({ tab }) => {
|
|||||||
<div ref={containerRef} className="table-designer-wrapper" style={{ height: '100%', overflow: 'hidden', position: 'relative' }}>
|
<div ref={containerRef} className="table-designer-wrapper" style={{ height: '100%', overflow: 'hidden', position: 'relative' }}>
|
||||||
<style>{`
|
<style>{`
|
||||||
.table-designer-wrapper .ant-table-body {
|
.table-designer-wrapper .ant-table-body {
|
||||||
height: ${tableHeight}px !important;
|
|
||||||
max-height: ${tableHeight}px !important;
|
max-height: ${tableHeight}px !important;
|
||||||
}
|
}
|
||||||
`}</style>
|
`}</style>
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ interface AppState {
|
|||||||
savedQueries: SavedQuery[];
|
savedQueries: SavedQuery[];
|
||||||
darkMode: boolean;
|
darkMode: boolean;
|
||||||
sqlFormatOptions: { keywordCase: 'upper' | 'lower' };
|
sqlFormatOptions: { keywordCase: 'upper' | 'lower' };
|
||||||
|
queryOptions: { maxRows: number };
|
||||||
sqlLogs: SqlLog[];
|
sqlLogs: SqlLog[];
|
||||||
|
|
||||||
addConnection: (conn: SavedConnection) => void;
|
addConnection: (conn: SavedConnection) => void;
|
||||||
@@ -29,6 +30,10 @@ interface AppState {
|
|||||||
|
|
||||||
addTab: (tab: TabData) => void;
|
addTab: (tab: TabData) => void;
|
||||||
closeTab: (id: string) => void;
|
closeTab: (id: string) => void;
|
||||||
|
closeOtherTabs: (id: string) => void;
|
||||||
|
closeTabsToLeft: (id: string) => void;
|
||||||
|
closeTabsToRight: (id: string) => void;
|
||||||
|
closeAllTabs: () => void;
|
||||||
setActiveTab: (id: string) => void;
|
setActiveTab: (id: string) => void;
|
||||||
setActiveContext: (context: { connectionId: string; dbName: string } | null) => void;
|
setActiveContext: (context: { connectionId: string; dbName: string } | null) => void;
|
||||||
|
|
||||||
@@ -37,6 +42,7 @@ interface AppState {
|
|||||||
|
|
||||||
toggleDarkMode: () => void;
|
toggleDarkMode: () => void;
|
||||||
setSqlFormatOptions: (options: { keywordCase: 'upper' | 'lower' }) => void;
|
setSqlFormatOptions: (options: { keywordCase: 'upper' | 'lower' }) => void;
|
||||||
|
setQueryOptions: (options: Partial<{ maxRows: number }>) => void;
|
||||||
|
|
||||||
addSqlLog: (log: SqlLog) => void;
|
addSqlLog: (log: SqlLog) => void;
|
||||||
clearSqlLogs: () => void;
|
clearSqlLogs: () => void;
|
||||||
@@ -52,6 +58,7 @@ export const useStore = create<AppState>()(
|
|||||||
savedQueries: [],
|
savedQueries: [],
|
||||||
darkMode: false,
|
darkMode: false,
|
||||||
sqlFormatOptions: { keywordCase: 'upper' },
|
sqlFormatOptions: { keywordCase: 'upper' },
|
||||||
|
queryOptions: { maxRows: 5000 },
|
||||||
sqlLogs: [],
|
sqlLogs: [],
|
||||||
|
|
||||||
addConnection: (conn) => set((state) => ({ connections: [...state.connections, conn] })),
|
addConnection: (conn) => set((state) => ({ connections: [...state.connections, conn] })),
|
||||||
@@ -79,6 +86,30 @@ export const useStore = create<AppState>()(
|
|||||||
}
|
}
|
||||||
return { tabs: newTabs, activeTabId: newActiveId };
|
return { tabs: newTabs, activeTabId: newActiveId };
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
closeOtherTabs: (id) => set((state) => {
|
||||||
|
const keep = state.tabs.find(t => t.id === id);
|
||||||
|
if (!keep) return state;
|
||||||
|
return { tabs: [keep], activeTabId: id };
|
||||||
|
}),
|
||||||
|
|
||||||
|
closeTabsToLeft: (id) => set((state) => {
|
||||||
|
const index = state.tabs.findIndex(t => t.id === id);
|
||||||
|
if (index === -1) return state;
|
||||||
|
const newTabs = state.tabs.slice(index);
|
||||||
|
const activeStillExists = state.activeTabId ? newTabs.some(t => t.id === state.activeTabId) : false;
|
||||||
|
return { tabs: newTabs, activeTabId: activeStillExists ? state.activeTabId : id };
|
||||||
|
}),
|
||||||
|
|
||||||
|
closeTabsToRight: (id) => set((state) => {
|
||||||
|
const index = state.tabs.findIndex(t => t.id === id);
|
||||||
|
if (index === -1) return state;
|
||||||
|
const newTabs = state.tabs.slice(0, index + 1);
|
||||||
|
const activeStillExists = state.activeTabId ? newTabs.some(t => t.id === state.activeTabId) : false;
|
||||||
|
return { tabs: newTabs, activeTabId: activeStillExists ? state.activeTabId : id };
|
||||||
|
}),
|
||||||
|
|
||||||
|
closeAllTabs: () => set(() => ({ tabs: [], activeTabId: null })),
|
||||||
|
|
||||||
setActiveTab: (id) => set({ activeTabId: id }),
|
setActiveTab: (id) => set({ activeTabId: id }),
|
||||||
setActiveContext: (context) => set({ activeContext: context }),
|
setActiveContext: (context) => set({ activeContext: context }),
|
||||||
@@ -96,13 +127,14 @@ export const useStore = create<AppState>()(
|
|||||||
|
|
||||||
toggleDarkMode: () => set((state) => ({ darkMode: !state.darkMode })),
|
toggleDarkMode: () => set((state) => ({ darkMode: !state.darkMode })),
|
||||||
setSqlFormatOptions: (options) => set({ sqlFormatOptions: options }),
|
setSqlFormatOptions: (options) => set({ sqlFormatOptions: options }),
|
||||||
|
setQueryOptions: (options) => set((state) => ({ queryOptions: { ...state.queryOptions, ...options } })),
|
||||||
|
|
||||||
addSqlLog: (log) => set((state) => ({ sqlLogs: [log, ...state.sqlLogs].slice(0, 1000) })), // Keep last 1000 logs
|
addSqlLog: (log) => set((state) => ({ sqlLogs: [log, ...state.sqlLogs].slice(0, 1000) })), // Keep last 1000 logs
|
||||||
clearSqlLogs: () => set({ sqlLogs: [] }),
|
clearSqlLogs: () => set({ sqlLogs: [] }),
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
name: 'lite-db-storage', // name of the item in the storage (must be unique)
|
name: 'lite-db-storage', // name of the item in the storage (must be unique)
|
||||||
partialize: (state) => ({ connections: state.connections, savedQueries: state.savedQueries, darkMode: state.darkMode, sqlFormatOptions: state.sqlFormatOptions }), // Don't persist logs
|
partialize: (state) => ({ connections: state.connections, savedQueries: state.savedQueries, darkMode: state.darkMode, sqlFormatOptions: state.sqlFormatOptions, queryOptions: state.queryOptions }), // Don't persist logs
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
173
frontend/src/utils/sql.ts
Normal file
173
frontend/src/utils/sql.ts
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
export type FilterCondition = {
|
||||||
|
id?: number;
|
||||||
|
column?: string;
|
||||||
|
op?: string;
|
||||||
|
value?: string;
|
||||||
|
value2?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const normalizeIdentPart = (ident: string) => {
|
||||||
|
let raw = (ident || '').trim();
|
||||||
|
if (!raw) return raw;
|
||||||
|
const first = raw[0];
|
||||||
|
const last = raw[raw.length - 1];
|
||||||
|
if ((first === '"' && last === '"') || (first === '`' && last === '`')) {
|
||||||
|
raw = raw.slice(1, -1).trim();
|
||||||
|
}
|
||||||
|
raw = raw.replace(/["`]/g, '').trim();
|
||||||
|
return raw;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const quoteIdentPart = (dbType: string, ident: string) => {
|
||||||
|
const raw = normalizeIdentPart(ident);
|
||||||
|
if (!raw) return raw;
|
||||||
|
if ((dbType || '').toLowerCase() === 'mysql') return `\`${raw.replace(/`/g, '``')}\``;
|
||||||
|
return `"${raw.replace(/"/g, '""')}"`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const quoteQualifiedIdent = (dbType: string, ident: string) => {
|
||||||
|
const raw = (ident || '').trim();
|
||||||
|
if (!raw) return raw;
|
||||||
|
const parts = raw.split('.').map(normalizeIdentPart).filter(Boolean);
|
||||||
|
if (parts.length <= 1) return quoteIdentPart(dbType, raw);
|
||||||
|
return parts.map(p => quoteIdentPart(dbType, p)).join('.');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const escapeLiteral = (val: string) => (val || '').replace(/'/g, "''");
|
||||||
|
|
||||||
|
export const parseListValues = (val: string) => {
|
||||||
|
const raw = (val || '').trim();
|
||||||
|
if (!raw) return [];
|
||||||
|
return raw
|
||||||
|
.split(/[\n,,]+/)
|
||||||
|
.map(s => s.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const buildWhereSQL = (dbType: string, conditions: FilterCondition[]) => {
|
||||||
|
const whereParts: string[] = [];
|
||||||
|
|
||||||
|
(conditions || []).forEach((cond) => {
|
||||||
|
const op = (cond?.op || '').trim();
|
||||||
|
const column = (cond?.column || '').trim();
|
||||||
|
const value = (cond?.value ?? '').toString();
|
||||||
|
const value2 = (cond?.value2 ?? '').toString();
|
||||||
|
|
||||||
|
if (op === 'CUSTOM') {
|
||||||
|
const expr = value.trim();
|
||||||
|
if (expr) whereParts.push(`(${expr})`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!column) return;
|
||||||
|
|
||||||
|
const col = quoteIdentPart(dbType, column);
|
||||||
|
|
||||||
|
switch (op) {
|
||||||
|
case 'IS_NULL':
|
||||||
|
whereParts.push(`${col} IS NULL`);
|
||||||
|
return;
|
||||||
|
case 'IS_NOT_NULL':
|
||||||
|
whereParts.push(`${col} IS NOT NULL`);
|
||||||
|
return;
|
||||||
|
case 'IS_EMPTY':
|
||||||
|
// 兼容:空值通常理解为 NULL 或空字符串
|
||||||
|
whereParts.push(`(${col} IS NULL OR ${col} = '')`);
|
||||||
|
return;
|
||||||
|
case 'IS_NOT_EMPTY':
|
||||||
|
whereParts.push(`(${col} IS NOT NULL AND ${col} <> '')`);
|
||||||
|
return;
|
||||||
|
case 'BETWEEN': {
|
||||||
|
const v1 = value.trim();
|
||||||
|
const v2 = value2.trim();
|
||||||
|
if (!v1 || !v2) return;
|
||||||
|
whereParts.push(`${col} BETWEEN '${escapeLiteral(v1)}' AND '${escapeLiteral(v2)}'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'NOT_BETWEEN': {
|
||||||
|
const v1 = value.trim();
|
||||||
|
const v2 = value2.trim();
|
||||||
|
if (!v1 || !v2) return;
|
||||||
|
whereParts.push(`${col} NOT BETWEEN '${escapeLiteral(v1)}' AND '${escapeLiteral(v2)}'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'IN': {
|
||||||
|
const items = parseListValues(value);
|
||||||
|
if (items.length === 0) return;
|
||||||
|
const list = items.map(v => `'${escapeLiteral(v)}'`).join(', ');
|
||||||
|
whereParts.push(`${col} IN (${list})`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'NOT_IN': {
|
||||||
|
const items = parseListValues(value);
|
||||||
|
if (items.length === 0) return;
|
||||||
|
const list = items.map(v => `'${escapeLiteral(v)}'`).join(', ');
|
||||||
|
whereParts.push(`${col} NOT IN (${list})`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'CONTAINS': {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} LIKE '%${escapeLiteral(v)}%'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'NOT_CONTAINS': {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} NOT LIKE '%${escapeLiteral(v)}%'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'STARTS_WITH': {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} LIKE '${escapeLiteral(v)}%'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'NOT_STARTS_WITH': {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} NOT LIKE '${escapeLiteral(v)}%'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'ENDS_WITH': {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} LIKE '%${escapeLiteral(v)}'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case 'NOT_ENDS_WITH': {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} NOT LIKE '%${escapeLiteral(v)}'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case '=':
|
||||||
|
case '!=':
|
||||||
|
case '<':
|
||||||
|
case '<=':
|
||||||
|
case '>':
|
||||||
|
case '>=': {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} ${op} '${escapeLiteral(v)}'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
// 兼容旧值:LIKE
|
||||||
|
if (op.toUpperCase() === 'LIKE') {
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} LIKE '%${escapeLiteral(v)}%'`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const v = value.trim();
|
||||||
|
if (!v) return;
|
||||||
|
whereParts.push(`${col} ${op} '${escapeLiteral(v)}'`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return whereParts.length > 0 ? `WHERE ${whereParts.join(' AND ')}` : '';
|
||||||
|
};
|
||||||
|
|
||||||
6
frontend/wailsjs/go/app/App.d.ts
vendored
6
frontend/wailsjs/go/app/App.d.ts
vendored
@@ -35,8 +35,14 @@ export function DataSyncPreview(arg1:sync.SyncConfig,arg2:string,arg3:number):Pr
|
|||||||
|
|
||||||
export function ExportData(arg1:Array<Record<string, any>>,arg2:Array<string>,arg3:string,arg4:string):Promise<connection.QueryResult>;
|
export function ExportData(arg1:Array<Record<string, any>>,arg2:Array<string>,arg3:string,arg4:string):Promise<connection.QueryResult>;
|
||||||
|
|
||||||
|
export function ExportDatabaseSQL(arg1:connection.ConnectionConfig,arg2:string,arg3:boolean):Promise<connection.QueryResult>;
|
||||||
|
|
||||||
|
export function ExportQuery(arg1:connection.ConnectionConfig,arg2:string,arg3:string,arg4:string,arg5:string):Promise<connection.QueryResult>;
|
||||||
|
|
||||||
export function ExportTable(arg1:connection.ConnectionConfig,arg2:string,arg3:string,arg4:string):Promise<connection.QueryResult>;
|
export function ExportTable(arg1:connection.ConnectionConfig,arg2:string,arg3:string,arg4:string):Promise<connection.QueryResult>;
|
||||||
|
|
||||||
|
export function ExportTablesSQL(arg1:connection.ConnectionConfig,arg2:string,arg3:Array<string>,arg4:boolean):Promise<connection.QueryResult>;
|
||||||
|
|
||||||
export function ImportConfigFile():Promise<connection.QueryResult>;
|
export function ImportConfigFile():Promise<connection.QueryResult>;
|
||||||
|
|
||||||
export function ImportData(arg1:connection.ConnectionConfig,arg2:string,arg3:string):Promise<connection.QueryResult>;
|
export function ImportData(arg1:connection.ConnectionConfig,arg2:string,arg3:string):Promise<connection.QueryResult>;
|
||||||
|
|||||||
@@ -66,10 +66,22 @@ export function ExportData(arg1, arg2, arg3, arg4) {
|
|||||||
return window['go']['app']['App']['ExportData'](arg1, arg2, arg3, arg4);
|
return window['go']['app']['App']['ExportData'](arg1, arg2, arg3, arg4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ExportDatabaseSQL(arg1, arg2, arg3) {
|
||||||
|
return window['go']['app']['App']['ExportDatabaseSQL'](arg1, arg2, arg3);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ExportQuery(arg1, arg2, arg3, arg4, arg5) {
|
||||||
|
return window['go']['app']['App']['ExportQuery'](arg1, arg2, arg3, arg4, arg5);
|
||||||
|
}
|
||||||
|
|
||||||
export function ExportTable(arg1, arg2, arg3, arg4) {
|
export function ExportTable(arg1, arg2, arg3, arg4) {
|
||||||
return window['go']['app']['App']['ExportTable'](arg1, arg2, arg3, arg4);
|
return window['go']['app']['App']['ExportTable'](arg1, arg2, arg3, arg4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ExportTablesSQL(arg1, arg2, arg3, arg4) {
|
||||||
|
return window['go']['app']['App']['ExportTablesSQL'](arg1, arg2, arg3, arg4);
|
||||||
|
}
|
||||||
|
|
||||||
export function ImportConfigFile() {
|
export function ImportConfigFile() {
|
||||||
return window['go']['app']['App']['ImportConfigFile']();
|
return window['go']['app']['App']['ImportConfigFile']();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,9 +141,6 @@ func (a *App) getDatabase(config connection.ConnectionConfig) (db.Database, erro
|
|||||||
if len(shortKey) > 12 {
|
if len(shortKey) > 12 {
|
||||||
shortKey = shortKey[:12]
|
shortKey = shortKey[:12]
|
||||||
}
|
}
|
||||||
if config.UseSSH && config.Type != "mysql" {
|
|
||||||
logger.Warnf("当前仅 MySQL 支持内置 SSH 直连,其他类型请使用本地端口转发:%s", formatConnSummary(config))
|
|
||||||
}
|
|
||||||
logger.Infof("获取数据库连接:%s 缓存Key=%s", formatConnSummary(config), shortKey)
|
logger.Infof("获取数据库连接:%s 缓存Key=%s", formatConnSummary(config), shortKey)
|
||||||
|
|
||||||
a.mu.Lock()
|
a.mu.Lock()
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
"GoNavi-Wails/internal/logger"
|
"GoNavi-Wails/internal/logger"
|
||||||
|
"GoNavi-Wails/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generic DB Methods
|
// Generic DB Methods
|
||||||
@@ -91,16 +94,39 @@ func (a *App) DBQuery(config connection.ConnectionConfig, dbName string, query s
|
|||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query = sanitizeSQLForPgLike(runConfig.Type, query)
|
||||||
|
timeoutSeconds := runConfig.Timeout
|
||||||
|
if timeoutSeconds <= 0 {
|
||||||
|
timeoutSeconds = 30
|
||||||
|
}
|
||||||
|
ctx, cancel := utils.ContextWithTimeout(time.Duration(timeoutSeconds) * time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
lowerQuery := strings.TrimSpace(strings.ToLower(query))
|
lowerQuery := strings.TrimSpace(strings.ToLower(query))
|
||||||
if strings.HasPrefix(lowerQuery, "select") || strings.HasPrefix(lowerQuery, "show") || strings.HasPrefix(lowerQuery, "describe") || strings.HasPrefix(lowerQuery, "explain") {
|
if strings.HasPrefix(lowerQuery, "select") || strings.HasPrefix(lowerQuery, "show") || strings.HasPrefix(lowerQuery, "describe") || strings.HasPrefix(lowerQuery, "explain") {
|
||||||
data, columns, err := dbInst.Query(query)
|
var data []map[string]interface{}
|
||||||
|
var columns []string
|
||||||
|
if q, ok := dbInst.(interface {
|
||||||
|
QueryContext(context.Context, string) ([]map[string]interface{}, []string, error)
|
||||||
|
}); ok {
|
||||||
|
data, columns, err = q.QueryContext(ctx, query)
|
||||||
|
} else {
|
||||||
|
data, columns, err = dbInst.Query(query)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err, "DBQuery 查询失败:%s SQL片段=%q", formatConnSummary(runConfig), sqlSnippet(query))
|
logger.Error(err, "DBQuery 查询失败:%s SQL片段=%q", formatConnSummary(runConfig), sqlSnippet(query))
|
||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
}
|
}
|
||||||
return connection.QueryResult{Success: true, Data: data, Fields: columns}
|
return connection.QueryResult{Success: true, Data: data, Fields: columns}
|
||||||
} else {
|
} else {
|
||||||
affected, err := dbInst.Exec(query)
|
var affected int64
|
||||||
|
if e, ok := dbInst.(interface {
|
||||||
|
ExecContext(context.Context, string) (int64, error)
|
||||||
|
}); ok {
|
||||||
|
affected, err = e.ExecContext(ctx, query)
|
||||||
|
} else {
|
||||||
|
affected, err = dbInst.Exec(query)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err, "DBQuery 执行失败:%s SQL片段=%q", formatConnSummary(runConfig), sqlSnippet(query))
|
logger.Error(err, "DBQuery 执行失败:%s SQL片段=%q", formatConnSummary(runConfig), sqlSnippet(query))
|
||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
"GoNavi-Wails/internal/db"
|
"GoNavi-Wails/internal/db"
|
||||||
@@ -213,12 +218,36 @@ func (a *App) ExportTable(config connection.ConnectionConfig, dbName string, tab
|
|||||||
}
|
}
|
||||||
|
|
||||||
runConfig := normalizeRunConfig(config, dbName)
|
runConfig := normalizeRunConfig(config, dbName)
|
||||||
|
|
||||||
dbInst, err := a.getDatabase(runConfig)
|
dbInst, err := a.getDatabase(runConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format = strings.ToLower(format)
|
||||||
|
if format == "sql" {
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
w := bufio.NewWriterSize(f, 1024*1024)
|
||||||
|
defer w.Flush()
|
||||||
|
|
||||||
|
if err := writeSQLHeader(w, runConfig, dbName); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
if err := dumpTableSQL(w, dbInst, runConfig, dbName, tableName, true); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
if err := writeSQLFooter(w, runConfig); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
return connection.QueryResult{Success: true, Message: "Export successful"}
|
||||||
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf("SELECT * FROM %s", quoteQualifiedIdentByType(runConfig.Type, tableName))
|
query := fmt.Sprintf("SELECT * FROM %s", quoteQualifiedIdentByType(runConfig.Type, tableName))
|
||||||
|
|
||||||
data, columns, err := dbInst.Query(query)
|
data, columns, err := dbInst.Query(query)
|
||||||
@@ -231,71 +260,129 @@ data, columns, err := dbInst.Query(query)
|
|||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
if err := writeRowsToFile(f, data, columns, format); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: "Write error: " + err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
format = strings.ToLower(format)
|
return connection.QueryResult{Success: true, Message: "Export successful"}
|
||||||
var csvWriter *csv.Writer
|
}
|
||||||
var jsonEncoder *json.Encoder
|
|
||||||
var isJsonFirstRow = true
|
|
||||||
|
|
||||||
switch format {
|
func (a *App) ExportTablesSQL(config connection.ConnectionConfig, dbName string, tableNames []string, includeData bool) connection.QueryResult {
|
||||||
case "csv", "xlsx":
|
safeDbName := strings.TrimSpace(dbName)
|
||||||
f.Write([]byte{0xEF, 0xBB, 0xBF})
|
if safeDbName == "" {
|
||||||
csvWriter = csv.NewWriter(f)
|
safeDbName = "export"
|
||||||
defer csvWriter.Flush()
|
}
|
||||||
if err := csvWriter.Write(columns); err != nil {
|
suffix := "schema"
|
||||||
|
if includeData {
|
||||||
|
suffix = "backup"
|
||||||
|
}
|
||||||
|
defaultFilename := fmt.Sprintf("%s_%s_%dtables.sql", safeDbName, suffix, len(tableNames))
|
||||||
|
if len(tableNames) == 1 && strings.TrimSpace(tableNames[0]) != "" {
|
||||||
|
defaultFilename = fmt.Sprintf("%s_%s.sql", strings.TrimSpace(tableNames[0]), suffix)
|
||||||
|
}
|
||||||
|
|
||||||
|
filename, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
||||||
|
Title: "Export Tables (SQL)",
|
||||||
|
DefaultFilename: defaultFilename,
|
||||||
|
})
|
||||||
|
if err != nil || filename == "" {
|
||||||
|
return connection.QueryResult{Success: false, Message: "Cancelled"}
|
||||||
|
}
|
||||||
|
|
||||||
|
runConfig := normalizeRunConfig(config, dbName)
|
||||||
|
dbInst, err := a.getDatabase(runConfig)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
tables := make([]string, 0, len(tableNames))
|
||||||
|
seen := make(map[string]struct{}, len(tableNames))
|
||||||
|
for _, t := range tableNames {
|
||||||
|
t = strings.TrimSpace(t)
|
||||||
|
if t == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[t]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[t] = struct{}{}
|
||||||
|
tables = append(tables, t)
|
||||||
|
}
|
||||||
|
sort.Strings(tables)
|
||||||
|
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
w := bufio.NewWriterSize(f, 1024*1024)
|
||||||
|
defer w.Flush()
|
||||||
|
|
||||||
|
if err := writeSQLHeader(w, runConfig, dbName); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
for _, t := range tables {
|
||||||
|
if err := dumpTableSQL(w, dbInst, runConfig, dbName, t, includeData); err != nil {
|
||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
}
|
}
|
||||||
case "json":
|
}
|
||||||
f.WriteString("[\n")
|
if err := writeSQLFooter(w, runConfig); err != nil {
|
||||||
jsonEncoder = json.NewEncoder(f)
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
jsonEncoder.SetIndent(" ", " ")
|
|
||||||
case "md":
|
|
||||||
fmt.Fprintf(f, "| %s |\n", strings.Join(columns, " | "))
|
|
||||||
seps := make([]string, len(columns))
|
|
||||||
for i := range seps {
|
|
||||||
seps[i] = "---"
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "| %s |\n", strings.Join(seps, " | "))
|
|
||||||
default:
|
|
||||||
return connection.QueryResult{Success: false, Message: "Unsupported format: " + format}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rowMap := range data {
|
return connection.QueryResult{Success: true, Message: "Export successful"}
|
||||||
record := make([]string, len(columns))
|
}
|
||||||
for i, col := range columns {
|
|
||||||
val := rowMap[col]
|
|
||||||
if val == nil {
|
|
||||||
record[i] = "NULL"
|
|
||||||
} else {
|
|
||||||
s := fmt.Sprintf("%v", val)
|
|
||||||
if format == "md" {
|
|
||||||
s = strings.ReplaceAll(s, "|", "\\|")
|
|
||||||
s = strings.ReplaceAll(s, "\n", "<br>")
|
|
||||||
}
|
|
||||||
record[i] = s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch format {
|
func (a *App) ExportDatabaseSQL(config connection.ConnectionConfig, dbName string, includeData bool) connection.QueryResult {
|
||||||
case "csv", "xlsx":
|
safeDbName := strings.TrimSpace(dbName)
|
||||||
if err := csvWriter.Write(record); err != nil {
|
if safeDbName == "" {
|
||||||
return connection.QueryResult{Success: false, Message: "Write error: " + err.Error()}
|
return connection.QueryResult{Success: false, Message: "dbName required"}
|
||||||
}
|
}
|
||||||
case "json":
|
suffix := "schema"
|
||||||
if !isJsonFirstRow {
|
if includeData {
|
||||||
f.WriteString(",\n")
|
suffix = "backup"
|
||||||
}
|
|
||||||
if err := jsonEncoder.Encode(rowMap); err != nil {
|
|
||||||
return connection.QueryResult{Success: false, Message: "Write error: " + err.Error()}
|
|
||||||
}
|
|
||||||
isJsonFirstRow = false
|
|
||||||
case "md":
|
|
||||||
fmt.Fprintf(f, "| %s |\n", strings.Join(record, " | "))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if format == "json" {
|
filename, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
||||||
f.WriteString("\n]")
|
Title: fmt.Sprintf("Export %s (SQL)", safeDbName),
|
||||||
|
DefaultFilename: fmt.Sprintf("%s_%s.sql", safeDbName, suffix),
|
||||||
|
})
|
||||||
|
if err != nil || filename == "" {
|
||||||
|
return connection.QueryResult{Success: false, Message: "Cancelled"}
|
||||||
|
}
|
||||||
|
|
||||||
|
runConfig := normalizeRunConfig(config, dbName)
|
||||||
|
dbInst, err := a.getDatabase(runConfig)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
tables, err := dbInst.GetTables(dbName)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
sort.Strings(tables)
|
||||||
|
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
w := bufio.NewWriterSize(f, 1024*1024)
|
||||||
|
defer w.Flush()
|
||||||
|
|
||||||
|
if err := writeSQLHeader(w, runConfig, dbName); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
for _, t := range tables {
|
||||||
|
if err := dumpTableSQL(w, dbInst, runConfig, dbName, t, includeData); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := writeSQLFooter(w, runConfig); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
return connection.QueryResult{Success: true, Message: "Export successful"}
|
return connection.QueryResult{Success: true, Message: "Export successful"}
|
||||||
@@ -340,6 +427,173 @@ func quoteQualifiedIdentByType(dbType string, ident string) string {
|
|||||||
return strings.Join(quotedParts, ".")
|
return strings.Join(quotedParts, ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeSQLHeader(w *bufio.Writer, config connection.ConnectionConfig, dbName string) error {
|
||||||
|
now := time.Now().Format("2006-01-02 15:04:05")
|
||||||
|
if _, err := w.WriteString(fmt.Sprintf("-- GoNavi SQL Export\n-- Time: %s\n", now)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(dbName) != "" {
|
||||||
|
if _, err := w.WriteString(fmt.Sprintf("-- Database: %s\n\n", dbName)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.ToLower(strings.TrimSpace(config.Type)) == "mysql" && strings.TrimSpace(dbName) != "" {
|
||||||
|
if _, err := w.WriteString(fmt.Sprintf("USE %s;\n\n", quoteIdentByType("mysql", dbName))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString("SET FOREIGN_KEY_CHECKS=0;\n\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeSQLFooter(w *bufio.Writer, config connection.ConnectionConfig) error {
|
||||||
|
if strings.ToLower(strings.TrimSpace(config.Type)) == "mysql" {
|
||||||
|
if _, err := w.WriteString("\nSET FOREIGN_KEY_CHECKS=1;\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func qualifyTable(schemaName, tableName string) string {
|
||||||
|
schemaName = strings.TrimSpace(schemaName)
|
||||||
|
tableName = strings.TrimSpace(tableName)
|
||||||
|
if schemaName == "" {
|
||||||
|
return tableName
|
||||||
|
}
|
||||||
|
return schemaName + "." + tableName
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureSQLTerminator(sql string) string {
|
||||||
|
trimmed := strings.TrimSpace(sql)
|
||||||
|
if trimmed == "" {
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(trimmed, ";") {
|
||||||
|
return sql
|
||||||
|
}
|
||||||
|
return sql + ";"
|
||||||
|
}
|
||||||
|
|
||||||
|
func isMySQLHexLiteral(s string) bool {
|
||||||
|
if len(s) < 3 || !(strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X")) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := 2; i < len(s); i++ {
|
||||||
|
c := s[i]
|
||||||
|
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatSQLValue(dbType string, v interface{}) string {
|
||||||
|
if v == nil {
|
||||||
|
return "NULL"
|
||||||
|
}
|
||||||
|
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
if val {
|
||||||
|
return "1"
|
||||||
|
}
|
||||||
|
return "0"
|
||||||
|
case int:
|
||||||
|
return strconv.Itoa(val)
|
||||||
|
case int8, int16, int32, int64:
|
||||||
|
return fmt.Sprintf("%d", val)
|
||||||
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
|
return fmt.Sprintf("%d", val)
|
||||||
|
case float32:
|
||||||
|
f := float64(val)
|
||||||
|
if math.IsNaN(f) || math.IsInf(f, 0) {
|
||||||
|
return "NULL"
|
||||||
|
}
|
||||||
|
return strconv.FormatFloat(f, 'f', -1, 32)
|
||||||
|
case float64:
|
||||||
|
if math.IsNaN(val) || math.IsInf(val, 0) {
|
||||||
|
return "NULL"
|
||||||
|
}
|
||||||
|
return strconv.FormatFloat(val, 'f', -1, 64)
|
||||||
|
case time.Time:
|
||||||
|
return "'" + val.Format("2006-01-02 15:04:05") + "'"
|
||||||
|
case string:
|
||||||
|
if strings.ToLower(strings.TrimSpace(dbType)) == "mysql" && isMySQLHexLiteral(val) {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
escaped := strings.ReplaceAll(val, "'", "''")
|
||||||
|
return "'" + escaped + "'"
|
||||||
|
default:
|
||||||
|
escaped := strings.ReplaceAll(fmt.Sprintf("%v", v), "'", "''")
|
||||||
|
return "'" + escaped + "'"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dumpTableSQL(w *bufio.Writer, dbInst db.Database, config connection.ConnectionConfig, dbName, tableName string, includeData bool) error {
|
||||||
|
schemaName, pureTableName := normalizeSchemaAndTable(config, dbName, tableName)
|
||||||
|
|
||||||
|
if _, err := w.WriteString("\n-- ----------------------------\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(fmt.Sprintf("-- Table: %s\n", qualifyTable(schemaName, pureTableName))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString("-- ----------------------------\n\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
createSQL, err := dbInst.GetCreateStatement(schemaName, pureTableName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(ensureSQLTerminator(createSQL)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString("\n\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !includeData {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
qualified := qualifyTable(schemaName, pureTableName)
|
||||||
|
selectSQL := fmt.Sprintf("SELECT * FROM %s", quoteQualifiedIdentByType(config.Type, qualified))
|
||||||
|
data, columns, err := dbInst.Query(selectSQL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(data) == 0 {
|
||||||
|
if _, err := w.WriteString("-- (0 rows)\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
quotedCols := make([]string, 0, len(columns))
|
||||||
|
for _, c := range columns {
|
||||||
|
quotedCols = append(quotedCols, quoteIdentByType(config.Type, c))
|
||||||
|
}
|
||||||
|
quotedTable := quoteQualifiedIdentByType(config.Type, qualified)
|
||||||
|
|
||||||
|
for _, row := range data {
|
||||||
|
values := make([]string, 0, len(columns))
|
||||||
|
for _, c := range columns {
|
||||||
|
values = append(values, formatSQLValue(config.Type, row[c]))
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s);\n", quotedTable, strings.Join(quotedCols, ", "), strings.Join(values, ", "))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ExportData exports provided data to a file
|
// ExportData exports provided data to a file
|
||||||
func (a *App) ExportData(data []map[string]interface{}, columns []string, defaultName string, format string) connection.QueryResult {
|
func (a *App) ExportData(data []map[string]interface{}, columns []string, defaultName string, format string) connection.QueryResult {
|
||||||
if defaultName == "" {
|
if defaultName == "" {
|
||||||
@@ -359,33 +613,101 @@ func (a *App) ExportData(data []map[string]interface{}, columns []string, defaul
|
|||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
if err := writeRowsToFile(f, data, columns, format); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: "Write error: " + err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
return connection.QueryResult{Success: true, Message: "Export successful"}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExportQuery exports by executing the provided SELECT query on backend side.
|
||||||
|
// This avoids frontend IPC payload limits when exporting very large/long-text columns (e.g. base64).
|
||||||
|
func (a *App) ExportQuery(config connection.ConnectionConfig, dbName string, query string, defaultName string, format string) connection.QueryResult {
|
||||||
|
query = strings.TrimSpace(query)
|
||||||
|
if query == "" {
|
||||||
|
return connection.QueryResult{Success: false, Message: "query required"}
|
||||||
|
}
|
||||||
|
|
||||||
|
if defaultName == "" {
|
||||||
|
defaultName = "export"
|
||||||
|
}
|
||||||
|
|
||||||
|
filename, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
||||||
|
Title: "Export Query Result",
|
||||||
|
DefaultFilename: fmt.Sprintf("%s.%s", defaultName, strings.ToLower(format)),
|
||||||
|
})
|
||||||
|
if err != nil || filename == "" {
|
||||||
|
return connection.QueryResult{Success: false, Message: "Cancelled"}
|
||||||
|
}
|
||||||
|
|
||||||
|
runConfig := normalizeRunConfig(config, dbName)
|
||||||
|
dbInst, err := a.getDatabase(runConfig)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
query = sanitizeSQLForPgLike(runConfig.Type, query)
|
||||||
|
lowerQuery := strings.ToLower(strings.TrimSpace(query))
|
||||||
|
if !(strings.HasPrefix(lowerQuery, "select") || strings.HasPrefix(lowerQuery, "with")) {
|
||||||
|
return connection.QueryResult{Success: false, Message: "Only SELECT/WITH queries are supported"}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, columns, err := dbInst.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: err.Error()}
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if err := writeRowsToFile(f, data, columns, format); err != nil {
|
||||||
|
return connection.QueryResult{Success: false, Message: "Write error: " + err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
return connection.QueryResult{Success: true, Message: "Export successful"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeRowsToFile(f *os.File, data []map[string]interface{}, columns []string, format string) error {
|
||||||
|
format = strings.ToLower(strings.TrimSpace(format))
|
||||||
|
if f == nil {
|
||||||
|
return fmt.Errorf("file required")
|
||||||
|
}
|
||||||
|
|
||||||
format = strings.ToLower(format)
|
|
||||||
var csvWriter *csv.Writer
|
var csvWriter *csv.Writer
|
||||||
var jsonEncoder *json.Encoder
|
var jsonEncoder *json.Encoder
|
||||||
var isJsonFirstRow = true
|
isJsonFirstRow := true
|
||||||
|
|
||||||
switch format {
|
switch format {
|
||||||
case "csv", "xlsx":
|
case "csv", "xlsx":
|
||||||
f.Write([]byte{0xEF, 0xBB, 0xBF})
|
if _, err := f.Write([]byte{0xEF, 0xBB, 0xBF}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
csvWriter = csv.NewWriter(f)
|
csvWriter = csv.NewWriter(f)
|
||||||
defer csvWriter.Flush()
|
|
||||||
if err := csvWriter.Write(columns); err != nil {
|
if err := csvWriter.Write(columns); err != nil {
|
||||||
return connection.QueryResult{Success: false, Message: err.Error()}
|
return err
|
||||||
}
|
}
|
||||||
case "json":
|
case "json":
|
||||||
f.WriteString("[\n")
|
if _, err := f.WriteString("[\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
jsonEncoder = json.NewEncoder(f)
|
jsonEncoder = json.NewEncoder(f)
|
||||||
jsonEncoder.SetIndent(" ", " ")
|
jsonEncoder.SetIndent(" ", " ")
|
||||||
case "md":
|
case "md":
|
||||||
fmt.Fprintf(f, "| %s |\n", strings.Join(columns, " | "))
|
if _, err := fmt.Fprintf(f, "| %s |\n", strings.Join(columns, " | ")); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
seps := make([]string, len(columns))
|
seps := make([]string, len(columns))
|
||||||
for i := range seps {
|
for i := range seps {
|
||||||
seps[i] = "---"
|
seps[i] = "---"
|
||||||
}
|
}
|
||||||
fmt.Fprintf(f, "| %s |\n", strings.Join(seps, " | "))
|
if _, err := fmt.Fprintf(f, "| %s |\n", strings.Join(seps, " | ")); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return connection.QueryResult{Success: false, Message: "Unsupported format: " + format}
|
return fmt.Errorf("unsupported format: %s", format)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rowMap := range data {
|
for _, rowMap := range data {
|
||||||
@@ -394,37 +716,51 @@ func (a *App) ExportData(data []map[string]interface{}, columns []string, defaul
|
|||||||
val := rowMap[col]
|
val := rowMap[col]
|
||||||
if val == nil {
|
if val == nil {
|
||||||
record[i] = "NULL"
|
record[i] = "NULL"
|
||||||
} else {
|
continue
|
||||||
s := fmt.Sprintf("%v", val)
|
|
||||||
if format == "md" {
|
|
||||||
s = strings.ReplaceAll(s, "|", "\\|")
|
|
||||||
s = strings.ReplaceAll(s, "\n", "<br>")
|
|
||||||
}
|
|
||||||
record[i] = s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s := fmt.Sprintf("%v", val)
|
||||||
|
if format == "md" {
|
||||||
|
s = strings.ReplaceAll(s, "|", "\\|")
|
||||||
|
s = strings.ReplaceAll(s, "\n", "<br>")
|
||||||
|
}
|
||||||
|
record[i] = s
|
||||||
}
|
}
|
||||||
|
|
||||||
switch format {
|
switch format {
|
||||||
case "csv", "xlsx":
|
case "csv", "xlsx":
|
||||||
if err := csvWriter.Write(record); err != nil {
|
if err := csvWriter.Write(record); err != nil {
|
||||||
return connection.QueryResult{Success: false, Message: "Write error: " + err.Error()}
|
return err
|
||||||
}
|
}
|
||||||
case "json":
|
case "json":
|
||||||
if !isJsonFirstRow {
|
if !isJsonFirstRow {
|
||||||
f.WriteString(",\n")
|
if _, err := f.WriteString(",\n"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err := jsonEncoder.Encode(rowMap); err != nil {
|
if err := jsonEncoder.Encode(rowMap); err != nil {
|
||||||
return connection.QueryResult{Success: false, Message: "Write error: " + err.Error()}
|
return err
|
||||||
}
|
}
|
||||||
isJsonFirstRow = false
|
isJsonFirstRow = false
|
||||||
case "md":
|
case "md":
|
||||||
fmt.Fprintf(f, "| %s |\n", strings.Join(record, " | "))
|
if _, err := fmt.Fprintf(f, "| %s |\n", strings.Join(record, " | ")); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if format == "csv" || format == "xlsx" {
|
||||||
|
csvWriter.Flush()
|
||||||
|
if err := csvWriter.Error(); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if format == "json" {
|
if format == "json" {
|
||||||
f.WriteString("\n]")
|
if _, err := f.WriteString("\n]"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return connection.QueryResult{Success: true, Message: "Export successful"}
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
236
internal/app/sql_sanitize.go
Normal file
236
internal/app/sql_sanitize.go
Normal file
@@ -0,0 +1,236 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
func sanitizeSQLForPgLike(dbType string, query string) string {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(dbType)) {
|
||||||
|
case "postgres", "kingbase":
|
||||||
|
// 有些情况下会出现多层重复引用(例如 """"schema"""" 或 ""schema"""),单次修复不一定收敛。
|
||||||
|
// 这里做有限次数的迭代,直到输出不再变化。
|
||||||
|
out := query
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
fixed := fixBrokenDoubleDoubleQuotedIdent(out)
|
||||||
|
if fixed == out {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
out = fixed
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
default:
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixBrokenDoubleDoubleQuotedIdent fixes accidental identifiers like:
|
||||||
|
//
|
||||||
|
// SELECT * FROM ""schema"".""table""
|
||||||
|
//
|
||||||
|
// which can be produced when a quoted identifier gets wrapped by quotes again.
|
||||||
|
//
|
||||||
|
// It is intentionally conservative:
|
||||||
|
// - only runs outside strings/comments/dollar-quoted blocks
|
||||||
|
// - does not touch valid escaped-quote sequences inside quoted identifiers (e.g. "a""b")
|
||||||
|
func fixBrokenDoubleDoubleQuotedIdent(query string) string {
|
||||||
|
if !strings.Contains(query, `""`) {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
b.Grow(len(query))
|
||||||
|
|
||||||
|
inSingle := false
|
||||||
|
inDoubleIdent := false
|
||||||
|
inLineComment := false
|
||||||
|
inBlockComment := false
|
||||||
|
dollarTag := ""
|
||||||
|
|
||||||
|
for i := 0; i < len(query); i++ {
|
||||||
|
ch := query[i]
|
||||||
|
next := byte(0)
|
||||||
|
if i+1 < len(query) {
|
||||||
|
next = query[i+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if inLineComment {
|
||||||
|
b.WriteByte(ch)
|
||||||
|
if ch == '\n' {
|
||||||
|
inLineComment = false
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if inBlockComment {
|
||||||
|
b.WriteByte(ch)
|
||||||
|
if ch == '*' && next == '/' {
|
||||||
|
b.WriteByte('/')
|
||||||
|
i++
|
||||||
|
inBlockComment = false
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if dollarTag != "" {
|
||||||
|
if strings.HasPrefix(query[i:], dollarTag) {
|
||||||
|
b.WriteString(dollarTag)
|
||||||
|
i += len(dollarTag) - 1
|
||||||
|
dollarTag = ""
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b.WriteByte(ch)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if inSingle {
|
||||||
|
b.WriteByte(ch)
|
||||||
|
if ch == '\'' {
|
||||||
|
// escaped single quote
|
||||||
|
if next == '\'' {
|
||||||
|
b.WriteByte('\'')
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
inSingle = false
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if inDoubleIdent {
|
||||||
|
b.WriteByte(ch)
|
||||||
|
if ch == '"' {
|
||||||
|
// escaped quote inside identifier
|
||||||
|
if next == '"' {
|
||||||
|
b.WriteByte('"')
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
inDoubleIdent = false
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Outside of all string/comment blocks ---
|
||||||
|
if ch == '-' && next == '-' {
|
||||||
|
b.WriteByte(ch)
|
||||||
|
b.WriteByte('-')
|
||||||
|
i++
|
||||||
|
inLineComment = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ch == '/' && next == '*' {
|
||||||
|
b.WriteByte(ch)
|
||||||
|
b.WriteByte('*')
|
||||||
|
i++
|
||||||
|
inBlockComment = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ch == '\'' {
|
||||||
|
b.WriteByte(ch)
|
||||||
|
inSingle = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ch == '$' {
|
||||||
|
if tag := parseDollarTag(query[i:]); tag != "" {
|
||||||
|
b.WriteString(tag)
|
||||||
|
i += len(tag) - 1
|
||||||
|
dollarTag = tag
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ch == '"' {
|
||||||
|
// Fix: ""ident"" -> "ident" (only when it looks like a plain identifier)
|
||||||
|
// Also handle variants like ""ident""" / """"ident"""" (extra quotes at either side).
|
||||||
|
if next == '"' {
|
||||||
|
if replacement, advance, ok := tryFixDoubleDoubleQuotedIdent(query, i); ok {
|
||||||
|
b.WriteString(replacement)
|
||||||
|
i = advance - 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteByte(ch)
|
||||||
|
inDoubleIdent = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
b.WriteByte(ch)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func tryFixDoubleDoubleQuotedIdent(query string, start int) (replacement string, advance int, ok bool) {
|
||||||
|
// start points at the first quote of a broken identifier, usually like:
|
||||||
|
// ""ident"" / ""ident""" / """"ident""""
|
||||||
|
if start < 0 || start+1 >= len(query) {
|
||||||
|
return "", 0, false
|
||||||
|
}
|
||||||
|
if query[start] != '"' || query[start+1] != '"' {
|
||||||
|
return "", 0, false
|
||||||
|
}
|
||||||
|
if start > 0 && query[start-1] == '"' {
|
||||||
|
return "", 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
runLen := 0
|
||||||
|
for start+runLen < len(query) && query[start+runLen] == '"' {
|
||||||
|
runLen++
|
||||||
|
}
|
||||||
|
if runLen < 2 || runLen%2 == 1 {
|
||||||
|
// Odd run (e.g. """...) can be a valid quoted identifier with escaped quotes.
|
||||||
|
return "", 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
contentStart := start + runLen
|
||||||
|
j := contentStart
|
||||||
|
for j < len(query) {
|
||||||
|
if query[j] == '"' {
|
||||||
|
endRunLen := 0
|
||||||
|
for j+endRunLen < len(query) && query[j+endRunLen] == '"' {
|
||||||
|
endRunLen++
|
||||||
|
}
|
||||||
|
if endRunLen >= 2 {
|
||||||
|
content := strings.TrimSpace(query[contentStart:j])
|
||||||
|
if looksLikeIdentifierContent(content) {
|
||||||
|
return `"` + content + `"`, j + endRunLen, true
|
||||||
|
}
|
||||||
|
return "", 0, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Fast abort: identifier-like content should not span lines.
|
||||||
|
if query[j] == '\n' || query[j] == '\r' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
return "", 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func looksLikeIdentifierContent(s string) bool {
|
||||||
|
if strings.TrimSpace(s) == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, r := range s {
|
||||||
|
if r == '_' || r == '$' || r == '-' || unicode.IsLetter(r) || unicode.IsDigit(r) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseDollarTag(s string) string {
|
||||||
|
// Match: $tag$ where tag is [A-Za-z0-9_]* (can be empty => $$)
|
||||||
|
if len(s) < 2 || s[0] != '$' {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for i := 1; i < len(s); i++ {
|
||||||
|
c := s[i]
|
||||||
|
if c == '$' {
|
||||||
|
return s[:i+1]
|
||||||
|
}
|
||||||
|
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
55
internal/app/sql_sanitize_test.go
Normal file
55
internal/app/sql_sanitize_test.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSanitizeSQLForPgLike_FixesBrokenDoubleDoubleQuotes(t *testing.T) {
|
||||||
|
in := `SELECT * FROM ""ldf_server"".""t_user"" LIMIT 1`
|
||||||
|
out := sanitizeSQLForPgLike("kingbase", in)
|
||||||
|
want := `SELECT * FROM "ldf_server"."t_user" LIMIT 1`
|
||||||
|
if out != want {
|
||||||
|
t.Fatalf("unexpected sanitize output:\nIN: %s\nOUT: %s\nWANT: %s", in, out, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSanitizeSQLForPgLike_FixesBrokenDoubleDoubleQuotes_WithExtraQuotes(t *testing.T) {
|
||||||
|
in := `SELECT * FROM ""ldf_server""".""t_user"" LIMIT 1`
|
||||||
|
out := sanitizeSQLForPgLike("kingbase", in)
|
||||||
|
want := `SELECT * FROM "ldf_server"."t_user" LIMIT 1`
|
||||||
|
if out != want {
|
||||||
|
t.Fatalf("unexpected sanitize output:\nIN: %s\nOUT: %s\nWANT: %s", in, out, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSanitizeSQLForPgLike_FixesBrokenDoubleDoubleQuotes_WithQuadQuotes(t *testing.T) {
|
||||||
|
in := `SELECT * FROM """"ldf_server"""".""t_user"" LIMIT 1`
|
||||||
|
out := sanitizeSQLForPgLike("kingbase", in)
|
||||||
|
want := `SELECT * FROM "ldf_server"."t_user" LIMIT 1`
|
||||||
|
if out != want {
|
||||||
|
t.Fatalf("unexpected sanitize output:\nIN: %s\nOUT: %s\nWANT: %s", in, out, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSanitizeSQLForPgLike_DoesNotTouchEscapedQuotesInsideIdentifier(t *testing.T) {
|
||||||
|
in := `SELECT "a""b" FROM "t""x"`
|
||||||
|
out := sanitizeSQLForPgLike("postgres", in)
|
||||||
|
if out != in {
|
||||||
|
t.Fatalf("should keep valid escaped quotes inside identifier:\nIN: %s\nOUT: %s", in, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSanitizeSQLForPgLike_DoesNotTouchDollarQuotedStrings(t *testing.T) {
|
||||||
|
in := "SELECT $$\"\"ldf_server\"\"$$, \"\"ldf_server\"\""
|
||||||
|
out := sanitizeSQLForPgLike("postgres", in)
|
||||||
|
want := "SELECT $$\"\"ldf_server\"\"$$, \"ldf_server\""
|
||||||
|
if out != want {
|
||||||
|
t.Fatalf("unexpected sanitize output for dollar quoted string:\nIN: %s\nOUT: %s\nWANT: %s", in, out, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSanitizeSQLForPgLike_DoesNotModifyOtherDBTypes(t *testing.T) {
|
||||||
|
in := `SELECT * FROM ""ldf_server""`
|
||||||
|
out := sanitizeSQLForPgLike("mysql", in)
|
||||||
|
if out != in {
|
||||||
|
t.Fatalf("non-PG-like db should not be sanitized:\nIN: %s\nOUT: %s", in, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -57,6 +58,20 @@ func (c *CustomDB) Ping() error {
|
|||||||
return c.conn.PingContext(ctx)
|
return c.conn.PingContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CustomDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||||||
|
if c.conn == nil {
|
||||||
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := c.conn.QueryContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *CustomDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
func (c *CustomDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||||||
if c.conn == nil {
|
if c.conn == nil {
|
||||||
return nil, nil, fmt.Errorf("connection not open")
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
@@ -67,33 +82,18 @@ func (c *CustomDB) Query(query string) ([]map[string]interface{}, []string, erro
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
columns, err := rows.Columns()
|
func (c *CustomDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||||||
|
if c.conn == nil {
|
||||||
|
return 0, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
res, err := c.conn.ExecContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
var resultData []map[string]interface{}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]interface{}, len(columns))
|
|
||||||
valuePtrs := make([]interface{}, len(columns))
|
|
||||||
for i := range columns {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := make(map[string]interface{})
|
|
||||||
for i, col := range columns {
|
|
||||||
entry[col] = normalizeQueryValue(values[i])
|
|
||||||
}
|
|
||||||
resultData = append(resultData, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultData, columns, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CustomDB) Exec(query string) (int64, error) {
|
func (c *CustomDB) Exec(query string) (int64, error) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
|
"GoNavi-Wails/internal/logger"
|
||||||
"GoNavi-Wails/internal/ssh"
|
"GoNavi-Wails/internal/ssh"
|
||||||
"GoNavi-Wails/internal/utils"
|
"GoNavi-Wails/internal/utils"
|
||||||
|
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
type DamengDB struct {
|
type DamengDB struct {
|
||||||
conn *sql.DB
|
conn *sql.DB
|
||||||
pingTimeout time.Duration
|
pingTimeout time.Duration
|
||||||
|
forwarder *ssh.LocalForwarder // Store SSH tunnel forwarder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DamengDB) getDSN(config connection.ConnectionConfig) string {
|
func (d *DamengDB) getDSN(config connection.ConnectionConfig) string {
|
||||||
@@ -26,16 +29,6 @@ func (d *DamengDB) getDSN(config connection.ConnectionConfig) string {
|
|||||||
// or dm://user:password@host:port
|
// or dm://user:password@host:port
|
||||||
|
|
||||||
address := net.JoinHostPort(config.Host, strconv.Itoa(config.Port))
|
address := net.JoinHostPort(config.Host, strconv.Itoa(config.Port))
|
||||||
if config.UseSSH {
|
|
||||||
// SSH logic similar to others, assumes port forwarding
|
|
||||||
_, err := ssh.RegisterSSHNetwork(config.SSH)
|
|
||||||
if err == nil {
|
|
||||||
// DM driver likely uses standard net.Dial, so we might need a local listener
|
|
||||||
// or assume port forwarding is handled externally or implicitly via "tcp" override if driver allows.
|
|
||||||
// Similar to Oracle, we skip complex custom dialer injection for now.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
escapedPassword := url.PathEscape(config.Password)
|
escapedPassword := url.PathEscape(config.Password)
|
||||||
q := url.Values{}
|
q := url.Values{}
|
||||||
if config.Database != "" {
|
if config.Database != "" {
|
||||||
@@ -55,7 +48,42 @@ func (d *DamengDB) getDSN(config connection.ConnectionConfig) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *DamengDB) Connect(config connection.ConnectionConfig) error {
|
func (d *DamengDB) Connect(config connection.ConnectionConfig) error {
|
||||||
dsn := d.getDSN(config)
|
var dsn string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if config.UseSSH {
|
||||||
|
// Create SSH tunnel with local port forwarding
|
||||||
|
logger.Infof("达梦数据库使用 SSH 连接:地址=%s:%d 用户=%s", config.Host, config.Port, config.User)
|
||||||
|
|
||||||
|
forwarder, err := ssh.GetOrCreateLocalForwarder(config.SSH, config.Host, config.Port)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("创建 SSH 隧道失败:%w", err)
|
||||||
|
}
|
||||||
|
d.forwarder = forwarder
|
||||||
|
|
||||||
|
// Parse local address
|
||||||
|
host, portStr, err := net.SplitHostPort(forwarder.LocalAddr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地转发地址失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地端口失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a modified config pointing to local forwarder
|
||||||
|
localConfig := config
|
||||||
|
localConfig.Host = host
|
||||||
|
localConfig.Port = port
|
||||||
|
localConfig.UseSSH = false
|
||||||
|
|
||||||
|
dsn = d.getDSN(localConfig)
|
||||||
|
logger.Infof("达梦数据库通过本地端口转发连接:%s -> %s:%d", forwarder.LocalAddr, config.Host, config.Port)
|
||||||
|
} else {
|
||||||
|
dsn = d.getDSN(config)
|
||||||
|
}
|
||||||
|
|
||||||
db, err := sql.Open("dm", dsn)
|
db, err := sql.Open("dm", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("打开数据库连接失败:%w", err)
|
return fmt.Errorf("打开数据库连接失败:%w", err)
|
||||||
@@ -69,6 +97,15 @@ func (d *DamengDB) Connect(config connection.ConnectionConfig) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *DamengDB) Close() error {
|
func (d *DamengDB) Close() error {
|
||||||
|
// Close SSH forwarder first if exists
|
||||||
|
if d.forwarder != nil {
|
||||||
|
if err := d.forwarder.Close(); err != nil {
|
||||||
|
logger.Warnf("关闭达梦数据库 SSH 端口转发失败:%v", err)
|
||||||
|
}
|
||||||
|
d.forwarder = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then close database connection
|
||||||
if d.conn != nil {
|
if d.conn != nil {
|
||||||
return d.conn.Close()
|
return d.conn.Close()
|
||||||
}
|
}
|
||||||
@@ -88,6 +125,20 @@ func (d *DamengDB) Ping() error {
|
|||||||
return d.conn.PingContext(ctx)
|
return d.conn.PingContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DamengDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||||||
|
if d.conn == nil {
|
||||||
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := d.conn.QueryContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *DamengDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
func (d *DamengDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||||||
if d.conn == nil {
|
if d.conn == nil {
|
||||||
return nil, nil, fmt.Errorf("connection not open")
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
@@ -98,33 +149,18 @@ func (d *DamengDB) Query(query string) ([]map[string]interface{}, []string, erro
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
columns, err := rows.Columns()
|
func (d *DamengDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||||||
|
if d.conn == nil {
|
||||||
|
return 0, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
res, err := d.conn.ExecContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
var resultData []map[string]interface{}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]interface{}, len(columns))
|
|
||||||
valuePtrs := make([]interface{}, len(columns))
|
|
||||||
for i := range columns {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := make(map[string]interface{})
|
|
||||||
for i, col := range columns {
|
|
||||||
entry[col] = normalizeQueryValue(values[i])
|
|
||||||
}
|
|
||||||
resultData = append(resultData, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultData, columns, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DamengDB) Exec(query string) (int64, error) {
|
func (d *DamengDB) Exec(query string) (int64, error) {
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
|
"GoNavi-Wails/internal/logger"
|
||||||
"GoNavi-Wails/internal/ssh"
|
"GoNavi-Wails/internal/ssh"
|
||||||
"GoNavi-Wails/internal/utils"
|
"GoNavi-Wails/internal/utils"
|
||||||
|
|
||||||
@@ -16,6 +20,7 @@ import (
|
|||||||
type KingbaseDB struct {
|
type KingbaseDB struct {
|
||||||
conn *sql.DB
|
conn *sql.DB
|
||||||
pingTimeout time.Duration
|
pingTimeout time.Duration
|
||||||
|
forwarder *ssh.LocalForwarder // Store SSH tunnel forwarder
|
||||||
}
|
}
|
||||||
|
|
||||||
func quoteConnValue(v string) string {
|
func quoteConnValue(v string) string {
|
||||||
@@ -57,20 +62,6 @@ func (k *KingbaseDB) getDSN(config connection.ConnectionConfig) string {
|
|||||||
address := config.Host
|
address := config.Host
|
||||||
port := config.Port
|
port := config.Port
|
||||||
|
|
||||||
if config.UseSSH {
|
|
||||||
netName, err := ssh.RegisterSSHNetwork(config.SSH)
|
|
||||||
if err == nil {
|
|
||||||
// Kingbase/Postgres lib/pq allows custom dialer via "host" if using unix socket,
|
|
||||||
// but for custom network it's harder.
|
|
||||||
// Ideally we use a local forwarder.
|
|
||||||
// For now, we assume standard TCP or handle SSH externally.
|
|
||||||
// If we implement the net.Dial override for "kingbase" driver (which might use lib/pq internally),
|
|
||||||
// we might need to check if it supports "cloudsql" style or similar custom dialers.
|
|
||||||
// Similar to others, skipping SSH deep integration here for now.
|
|
||||||
_ = netName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct DSN
|
// Construct DSN
|
||||||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable connect_timeout=%d",
|
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable connect_timeout=%d",
|
||||||
quoteConnValue(address),
|
quoteConnValue(address),
|
||||||
@@ -85,7 +76,42 @@ func (k *KingbaseDB) getDSN(config connection.ConnectionConfig) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) Connect(config connection.ConnectionConfig) error {
|
func (k *KingbaseDB) Connect(config connection.ConnectionConfig) error {
|
||||||
dsn := k.getDSN(config)
|
var dsn string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if config.UseSSH {
|
||||||
|
// Create SSH tunnel with local port forwarding
|
||||||
|
logger.Infof("人大金仓使用 SSH 连接:地址=%s:%d 用户=%s", config.Host, config.Port, config.User)
|
||||||
|
|
||||||
|
forwarder, err := ssh.GetOrCreateLocalForwarder(config.SSH, config.Host, config.Port)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("创建 SSH 隧道失败:%w", err)
|
||||||
|
}
|
||||||
|
k.forwarder = forwarder
|
||||||
|
|
||||||
|
// Parse local address
|
||||||
|
host, portStr, err := net.SplitHostPort(forwarder.LocalAddr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地转发地址失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地端口失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a modified config pointing to local forwarder
|
||||||
|
localConfig := config
|
||||||
|
localConfig.Host = host
|
||||||
|
localConfig.Port = port
|
||||||
|
localConfig.UseSSH = false
|
||||||
|
|
||||||
|
dsn = k.getDSN(localConfig)
|
||||||
|
logger.Infof("人大金仓通过本地端口转发连接:%s -> %s:%d", forwarder.LocalAddr, config.Host, config.Port)
|
||||||
|
} else {
|
||||||
|
dsn = k.getDSN(config)
|
||||||
|
}
|
||||||
|
|
||||||
// Open using "kingbase" driver
|
// Open using "kingbase" driver
|
||||||
db, err := sql.Open("kingbase", dsn)
|
db, err := sql.Open("kingbase", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -100,6 +126,15 @@ func (k *KingbaseDB) Connect(config connection.ConnectionConfig) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) Close() error {
|
func (k *KingbaseDB) Close() error {
|
||||||
|
// Close SSH forwarder first if exists
|
||||||
|
if k.forwarder != nil {
|
||||||
|
if err := k.forwarder.Close(); err != nil {
|
||||||
|
logger.Warnf("关闭人大金仓 SSH 端口转发失败:%v", err)
|
||||||
|
}
|
||||||
|
k.forwarder = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then close database connection
|
||||||
if k.conn != nil {
|
if k.conn != nil {
|
||||||
return k.conn.Close()
|
return k.conn.Close()
|
||||||
}
|
}
|
||||||
@@ -119,6 +154,20 @@ func (k *KingbaseDB) Ping() error {
|
|||||||
return k.conn.PingContext(ctx)
|
return k.conn.PingContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k *KingbaseDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||||||
|
if k.conn == nil {
|
||||||
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := k.conn.QueryContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
func (k *KingbaseDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||||||
if k.conn == nil {
|
if k.conn == nil {
|
||||||
return nil, nil, fmt.Errorf("connection not open")
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
@@ -129,33 +178,18 @@ func (k *KingbaseDB) Query(query string) ([]map[string]interface{}, []string, er
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
columns, err := rows.Columns()
|
func (k *KingbaseDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||||||
|
if k.conn == nil {
|
||||||
|
return 0, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
res, err := k.conn.ExecContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
var resultData []map[string]interface{}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]interface{}, len(columns))
|
|
||||||
valuePtrs := make([]interface{}, len(columns))
|
|
||||||
for i := range columns {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := make(map[string]interface{})
|
|
||||||
for i, col := range columns {
|
|
||||||
entry[col] = normalizeQueryValue(values[i])
|
|
||||||
}
|
|
||||||
resultData = append(resultData, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultData, columns, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) Exec(query string) (int64, error) {
|
func (k *KingbaseDB) Exec(query string) (int64, error) {
|
||||||
@@ -223,15 +257,84 @@ func (k *KingbaseDB) GetCreateStatement(dbName, tableName string) (string, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
func (k *KingbaseDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
||||||
schema := "public"
|
// 解析 schema.table 格式
|
||||||
if dbName != "" {
|
schema := strings.TrimSpace(dbName)
|
||||||
schema = dbName
|
table := strings.TrimSpace(tableName)
|
||||||
|
|
||||||
|
// 如果 tableName 包含 schema (格式: schema.table)
|
||||||
|
if parts := strings.SplitN(table, ".", 2); len(parts) == 2 {
|
||||||
|
parsedSchema := strings.TrimSpace(parts[0])
|
||||||
|
parsedTable := strings.TrimSpace(parts[1])
|
||||||
|
if parsedSchema != "" && parsedTable != "" {
|
||||||
|
schema = parsedSchema
|
||||||
|
table = parsedTable
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf(`SELECT column_name, data_type, is_nullable, column_default
|
// 如果仍然没有 schema,使用 current_schema()
|
||||||
FROM information_schema.columns
|
// 这样可以自动匹配当前连接的 search_path
|
||||||
WHERE table_schema = '%s' AND table_name = '%s'
|
if schema == "" {
|
||||||
ORDER BY ordinal_position`, schema, tableName)
|
return k.getColumnsWithCurrentSchema(table)
|
||||||
|
}
|
||||||
|
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转义函数:处理单引号,移除双引号
|
||||||
|
esc := func(s string) string {
|
||||||
|
// 移除前后的双引号(如果存在)
|
||||||
|
s = strings.Trim(s, "\"")
|
||||||
|
// 转义单引号
|
||||||
|
return strings.ReplaceAll(s, "'", "''")
|
||||||
|
}
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`SELECT column_name, data_type, is_nullable, column_default
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = '%s' AND table_name = '%s'
|
||||||
|
ORDER BY ordinal_position`, esc(schema), esc(table))
|
||||||
|
|
||||||
|
data, _, err := k.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var columns []connection.ColumnDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
col := connection.ColumnDefinition{
|
||||||
|
Name: fmt.Sprintf("%v", row["column_name"]),
|
||||||
|
Type: fmt.Sprintf("%v", row["data_type"]),
|
||||||
|
Nullable: fmt.Sprintf("%v", row["is_nullable"]),
|
||||||
|
}
|
||||||
|
|
||||||
|
if row["column_default"] != nil {
|
||||||
|
def := fmt.Sprintf("%v", row["column_default"])
|
||||||
|
col.Default = &def
|
||||||
|
}
|
||||||
|
|
||||||
|
columns = append(columns, col)
|
||||||
|
}
|
||||||
|
return columns, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getColumnsWithCurrentSchema 使用 current_schema() 查询当前schema的表
|
||||||
|
func (k *KingbaseDB) getColumnsWithCurrentSchema(tableName string) ([]connection.ColumnDefinition, error) {
|
||||||
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转义函数
|
||||||
|
esc := func(s string) string {
|
||||||
|
s = strings.Trim(s, "\"")
|
||||||
|
return strings.ReplaceAll(s, "'", "''")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用 current_schema() 获取当前schema
|
||||||
|
query := fmt.Sprintf(`SELECT column_name, data_type, is_nullable, column_default
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema = current_schema() AND table_name = '%s'
|
||||||
|
ORDER BY ordinal_position`, esc(table))
|
||||||
|
|
||||||
data, _, err := k.Query(query)
|
data, _, err := k.Query(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -257,32 +360,76 @@ func (k *KingbaseDB) GetColumns(dbName, tableName string) ([]connection.ColumnDe
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
func (k *KingbaseDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
||||||
// Postgres/Kingbase index query
|
// 解析 schema.table 格式
|
||||||
query := fmt.Sprintf(`
|
schema := strings.TrimSpace(dbName)
|
||||||
SELECT
|
table := strings.TrimSpace(tableName)
|
||||||
i.relname as index_name,
|
|
||||||
a.attname as column_name,
|
|
||||||
ix.indisunique as is_unique
|
|
||||||
FROM
|
|
||||||
pg_class t,
|
|
||||||
pg_class i,
|
|
||||||
pg_index ix,
|
|
||||||
pg_attribute a,
|
|
||||||
pg_namespace n
|
|
||||||
WHERE
|
|
||||||
t.oid = ix.indrelid
|
|
||||||
AND i.oid = ix.indexrelid
|
|
||||||
AND a.attrelid = t.oid
|
|
||||||
AND a.attnum = ANY(ix.indkey)
|
|
||||||
AND t.relkind = 'r'
|
|
||||||
AND t.relname = '%s'
|
|
||||||
AND n.oid = t.relnamespace
|
|
||||||
AND n.nspname = '%s'
|
|
||||||
`, tableName, "public") // Default to public if dbName (schema) not clear.
|
|
||||||
|
|
||||||
if dbName != "" {
|
// 如果 tableName 包含 schema (格式: schema.table)
|
||||||
// Update query to use dbName as schema
|
if parts := strings.SplitN(table, ".", 2); len(parts) == 2 {
|
||||||
query = strings.Replace(query, "'public'", fmt.Sprintf("'%s'", dbName), 1)
|
parsedSchema := strings.TrimSpace(parts[0])
|
||||||
|
parsedTable := strings.TrimSpace(parts[1])
|
||||||
|
if parsedSchema != "" && parsedTable != "" {
|
||||||
|
schema = parsedSchema
|
||||||
|
table = parsedTable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转义函数:处理单引号,移除双引号
|
||||||
|
esc := func(s string) string {
|
||||||
|
s = strings.Trim(s, "\"")
|
||||||
|
return strings.ReplaceAll(s, "'", "''")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建查询:如果没有指定schema,使用current_schema()
|
||||||
|
var query string
|
||||||
|
if schema != "" {
|
||||||
|
query = fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
i.relname as index_name,
|
||||||
|
a.attname as column_name,
|
||||||
|
ix.indisunique as is_unique
|
||||||
|
FROM
|
||||||
|
pg_class t,
|
||||||
|
pg_class i,
|
||||||
|
pg_index ix,
|
||||||
|
pg_attribute a,
|
||||||
|
pg_namespace n
|
||||||
|
WHERE
|
||||||
|
t.oid = ix.indrelid
|
||||||
|
AND i.oid = ix.indexrelid
|
||||||
|
AND a.attrelid = t.oid
|
||||||
|
AND a.attnum = ANY(ix.indkey)
|
||||||
|
AND t.relkind = 'r'
|
||||||
|
AND t.relname = '%s'
|
||||||
|
AND n.oid = t.relnamespace
|
||||||
|
AND n.nspname = '%s'
|
||||||
|
`, esc(table), esc(schema))
|
||||||
|
} else {
|
||||||
|
query = fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
i.relname as index_name,
|
||||||
|
a.attname as column_name,
|
||||||
|
ix.indisunique as is_unique
|
||||||
|
FROM
|
||||||
|
pg_class t,
|
||||||
|
pg_class i,
|
||||||
|
pg_index ix,
|
||||||
|
pg_attribute a,
|
||||||
|
pg_namespace n
|
||||||
|
WHERE
|
||||||
|
t.oid = ix.indrelid
|
||||||
|
AND i.oid = ix.indexrelid
|
||||||
|
AND a.attrelid = t.oid
|
||||||
|
AND a.attnum = ANY(ix.indkey)
|
||||||
|
AND t.relkind = 'r'
|
||||||
|
AND t.relname = '%s'
|
||||||
|
AND n.oid = t.relnamespace
|
||||||
|
AND n.nspname = current_schema()
|
||||||
|
`, esc(table))
|
||||||
}
|
}
|
||||||
|
|
||||||
data, _, err := k.Query(query)
|
data, _, err := k.Query(query)
|
||||||
@@ -311,27 +458,67 @@ func (k *KingbaseDB) GetIndexes(dbName, tableName string) ([]connection.IndexDef
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
func (k *KingbaseDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
||||||
schema := "public"
|
// 解析 schema.table 格式
|
||||||
if dbName != "" {
|
schema := strings.TrimSpace(dbName)
|
||||||
schema = dbName
|
table := strings.TrimSpace(tableName)
|
||||||
|
|
||||||
|
// 如果 tableName 包含 schema (格式: schema.table)
|
||||||
|
if parts := strings.SplitN(table, ".", 2); len(parts) == 2 {
|
||||||
|
parsedSchema := strings.TrimSpace(parts[0])
|
||||||
|
parsedTable := strings.TrimSpace(parts[1])
|
||||||
|
if parsedSchema != "" && parsedTable != "" {
|
||||||
|
schema = parsedSchema
|
||||||
|
table = parsedTable
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf(`
|
if table == "" {
|
||||||
SELECT
|
return nil, fmt.Errorf("table name required")
|
||||||
tc.constraint_name,
|
}
|
||||||
kcu.column_name,
|
|
||||||
ccu.table_name AS foreign_table_name,
|
// 转义函数:处理单引号,移除双引号
|
||||||
ccu.column_name AS foreign_column_name
|
esc := func(s string) string {
|
||||||
FROM
|
s = strings.Trim(s, "\"")
|
||||||
information_schema.table_constraints AS tc
|
return strings.ReplaceAll(s, "'", "''")
|
||||||
JOIN information_schema.key_column_usage AS kcu
|
}
|
||||||
ON tc.constraint_name = kcu.constraint_name
|
|
||||||
AND tc.table_schema = kcu.table_schema
|
// 构建查询:如果没有指定schema,使用current_schema()
|
||||||
JOIN information_schema.constraint_column_usage AS ccu
|
var query string
|
||||||
ON ccu.constraint_name = tc.constraint_name
|
if schema != "" {
|
||||||
AND ccu.table_schema = tc.table_schema
|
query = fmt.Sprintf(`
|
||||||
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='%s' AND tc.table_schema='%s'`,
|
SELECT
|
||||||
tableName, schema)
|
tc.constraint_name,
|
||||||
|
kcu.column_name,
|
||||||
|
ccu.table_name AS foreign_table_name,
|
||||||
|
ccu.column_name AS foreign_column_name
|
||||||
|
FROM
|
||||||
|
information_schema.table_constraints AS tc
|
||||||
|
JOIN information_schema.key_column_usage AS kcu
|
||||||
|
ON tc.constraint_name = kcu.constraint_name
|
||||||
|
AND tc.table_schema = kcu.table_schema
|
||||||
|
JOIN information_schema.constraint_column_usage AS ccu
|
||||||
|
ON ccu.constraint_name = tc.constraint_name
|
||||||
|
AND ccu.table_schema = tc.table_schema
|
||||||
|
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='%s' AND tc.table_schema='%s'`,
|
||||||
|
esc(table), esc(schema))
|
||||||
|
} else {
|
||||||
|
query = fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
tc.constraint_name,
|
||||||
|
kcu.column_name,
|
||||||
|
ccu.table_name AS foreign_table_name,
|
||||||
|
ccu.column_name AS foreign_column_name
|
||||||
|
FROM
|
||||||
|
information_schema.table_constraints AS tc
|
||||||
|
JOIN information_schema.key_column_usage AS kcu
|
||||||
|
ON tc.constraint_name = kcu.constraint_name
|
||||||
|
AND tc.table_schema = kcu.table_schema
|
||||||
|
JOIN information_schema.constraint_column_usage AS ccu
|
||||||
|
ON ccu.constraint_name = tc.constraint_name
|
||||||
|
AND ccu.table_schema = tc.table_schema
|
||||||
|
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name='%s' AND tc.table_schema=current_schema()`,
|
||||||
|
esc(table))
|
||||||
|
}
|
||||||
|
|
||||||
data, _, err := k.Query(query)
|
data, _, err := k.Query(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -353,9 +540,43 @@ func (k *KingbaseDB) GetForeignKeys(dbName, tableName string) ([]connection.Fore
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
func (k *KingbaseDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
||||||
query := fmt.Sprintf(`SELECT trigger_name, action_timing, event_manipulation
|
// 解析 schema.table 格式
|
||||||
FROM information_schema.triggers
|
schema := strings.TrimSpace(dbName)
|
||||||
WHERE event_object_table = '%s'`, tableName)
|
table := strings.TrimSpace(tableName)
|
||||||
|
|
||||||
|
// 如果 tableName 包含 schema (格式: schema.table)
|
||||||
|
if parts := strings.SplitN(table, ".", 2); len(parts) == 2 {
|
||||||
|
parsedSchema := strings.TrimSpace(parts[0])
|
||||||
|
parsedTable := strings.TrimSpace(parts[1])
|
||||||
|
if parsedSchema != "" && parsedTable != "" {
|
||||||
|
schema = parsedSchema
|
||||||
|
table = parsedTable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转义函数:处理单引号,移除双引号
|
||||||
|
esc := func(s string) string {
|
||||||
|
s = strings.Trim(s, "\"")
|
||||||
|
return strings.ReplaceAll(s, "'", "''")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建查询:如果指定了schema,也加上schema条件
|
||||||
|
var query string
|
||||||
|
if schema != "" {
|
||||||
|
query = fmt.Sprintf(`SELECT trigger_name, action_timing, event_manipulation
|
||||||
|
FROM information_schema.triggers
|
||||||
|
WHERE event_object_table = '%s' AND event_object_schema = '%s'`,
|
||||||
|
esc(table), esc(schema))
|
||||||
|
} else {
|
||||||
|
query = fmt.Sprintf(`SELECT trigger_name, action_timing, event_manipulation
|
||||||
|
FROM information_schema.triggers
|
||||||
|
WHERE event_object_table = '%s' AND event_object_schema = current_schema()`,
|
||||||
|
esc(table))
|
||||||
|
}
|
||||||
|
|
||||||
data, _, err := k.Query(query)
|
data, _, err := k.Query(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -380,14 +601,13 @@ func (k *KingbaseDB) ApplyChanges(tableName string, changes connection.ChangeSet
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (k *KingbaseDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
func (k *KingbaseDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
||||||
schema := "public"
|
// dbName 在本项目语义里是“数据库”,schema 由 table_schema 决定;这里返回全部用户 schema 的列用于查询提示。
|
||||||
if dbName != "" {
|
query := `
|
||||||
schema = dbName
|
SELECT table_schema, table_name, column_name, data_type
|
||||||
}
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||||
query := fmt.Sprintf(`SELECT table_name, column_name, data_type
|
AND table_schema NOT LIKE 'pg_%'
|
||||||
FROM information_schema.columns
|
ORDER BY table_schema, table_name, ordinal_position`
|
||||||
WHERE table_schema = '%s'`, schema)
|
|
||||||
|
|
||||||
data, _, err := k.Query(query)
|
data, _, err := k.Query(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -396,8 +616,14 @@ func (k *KingbaseDB) GetAllColumns(dbName string) ([]connection.ColumnDefinition
|
|||||||
|
|
||||||
var cols []connection.ColumnDefinitionWithTable
|
var cols []connection.ColumnDefinitionWithTable
|
||||||
for _, row := range data {
|
for _, row := range data {
|
||||||
|
schema := fmt.Sprintf("%v", row["table_schema"])
|
||||||
|
table := fmt.Sprintf("%v", row["table_name"])
|
||||||
|
tableName := table
|
||||||
|
if strings.TrimSpace(schema) != "" {
|
||||||
|
tableName = fmt.Sprintf("%s.%s", schema, table)
|
||||||
|
}
|
||||||
col := connection.ColumnDefinitionWithTable{
|
col := connection.ColumnDefinitionWithTable{
|
||||||
TableName: fmt.Sprintf("%v", row["table_name"]),
|
TableName: tableName,
|
||||||
Name: fmt.Sprintf("%v", row["column_name"]),
|
Name: fmt.Sprintf("%v", row["column_name"]),
|
||||||
Type: fmt.Sprintf("%v", row["data_type"]),
|
Type: fmt.Sprintf("%v", row["data_type"]),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -76,6 +77,20 @@ func (m *MySQLDB) Ping() error {
|
|||||||
return m.conn.PingContext(ctx)
|
return m.conn.PingContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MySQLDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||||||
|
if m.conn == nil {
|
||||||
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := m.conn.QueryContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MySQLDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
func (m *MySQLDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||||||
if m.conn == nil {
|
if m.conn == nil {
|
||||||
return nil, nil, fmt.Errorf("connection not open")
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
@@ -86,33 +101,18 @@ func (m *MySQLDB) Query(query string) ([]map[string]interface{}, []string, error
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
columns, err := rows.Columns()
|
func (m *MySQLDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||||||
|
if m.conn == nil {
|
||||||
|
return 0, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
res, err := m.conn.ExecContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
var resultData []map[string]interface{}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]interface{}, len(columns))
|
|
||||||
valuePtrs := make([]interface{}, len(columns))
|
|
||||||
for i := range columns {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := make(map[string]interface{})
|
|
||||||
for i, col := range columns {
|
|
||||||
entry[col] = normalizeQueryValue(values[i])
|
|
||||||
}
|
|
||||||
resultData = append(resultData, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultData, columns, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MySQLDB) Exec(query string) (int64, error) {
|
func (m *MySQLDB) Exec(query string) (int64, error) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
|
"GoNavi-Wails/internal/logger"
|
||||||
"GoNavi-Wails/internal/ssh"
|
"GoNavi-Wails/internal/ssh"
|
||||||
"GoNavi-Wails/internal/utils"
|
"GoNavi-Wails/internal/utils"
|
||||||
|
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
type OracleDB struct {
|
type OracleDB struct {
|
||||||
conn *sql.DB
|
conn *sql.DB
|
||||||
pingTimeout time.Duration
|
pingTimeout time.Duration
|
||||||
|
forwarder *ssh.LocalForwarder // Store SSH tunnel forwarder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OracleDB) getDSN(config connection.ConnectionConfig) string {
|
func (o *OracleDB) getDSN(config connection.ConnectionConfig) string {
|
||||||
@@ -28,28 +31,6 @@ func (o *OracleDB) getDSN(config connection.ConnectionConfig) string {
|
|||||||
database = config.User // Default to user service/schema if empty?
|
database = config.User // Default to user service/schema if empty?
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.UseSSH {
|
|
||||||
_, err := ssh.RegisterSSHNetwork(config.SSH)
|
|
||||||
if err == nil {
|
|
||||||
// Oracle driver might not support custom dialer via DSN easily without extra config
|
|
||||||
// But go-ora v2 supports some advanced options.
|
|
||||||
// For simplicity, we assume standard TCP or we might need a workaround for SSH.
|
|
||||||
// go-ora v2 is pure Go, so we can potentially use a custom dialer if we manually open.
|
|
||||||
// But for now, let's just use the address.
|
|
||||||
// SSH tunneling via net.Dialer override is complex in sql.Open("oracle", ...).
|
|
||||||
// We might need to forward a local port if using SSH.
|
|
||||||
// Since ssh.RegisterSSHNetwork creates a custom network "ssh-via-...",
|
|
||||||
// we need to see if go-ora supports custom networks.
|
|
||||||
// Checking go-ora docs (simulated): It supports "unix" and "tcp".
|
|
||||||
// We might need to map the custom network to a local proxy.
|
|
||||||
// For now, we will assume direct connection or handle SSH separately later.
|
|
||||||
// We'll leave the protocol implementation as is in MySQL for now, hoping go-ora uses standard net.Dial.
|
|
||||||
// Note: go-ora connection string: oracle://user:pass@host:port/service
|
|
||||||
// It parses host/port. It doesn't easily take a custom "network" parameter in URL.
|
|
||||||
// We will proceed with standard TCP string.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
u := &url.URL{
|
u := &url.URL{
|
||||||
Scheme: "oracle",
|
Scheme: "oracle",
|
||||||
Host: net.JoinHostPort(config.Host, strconv.Itoa(config.Port)),
|
Host: net.JoinHostPort(config.Host, strconv.Itoa(config.Port)),
|
||||||
@@ -61,7 +42,42 @@ func (o *OracleDB) getDSN(config connection.ConnectionConfig) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *OracleDB) Connect(config connection.ConnectionConfig) error {
|
func (o *OracleDB) Connect(config connection.ConnectionConfig) error {
|
||||||
dsn := o.getDSN(config)
|
var dsn string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if config.UseSSH {
|
||||||
|
// Create SSH tunnel with local port forwarding
|
||||||
|
logger.Infof("Oracle 使用 SSH 连接:地址=%s:%d 用户=%s", config.Host, config.Port, config.User)
|
||||||
|
|
||||||
|
forwarder, err := ssh.GetOrCreateLocalForwarder(config.SSH, config.Host, config.Port)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("创建 SSH 隧道失败:%w", err)
|
||||||
|
}
|
||||||
|
o.forwarder = forwarder
|
||||||
|
|
||||||
|
// Parse local address
|
||||||
|
host, portStr, err := net.SplitHostPort(forwarder.LocalAddr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地转发地址失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地端口失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a modified config pointing to local forwarder
|
||||||
|
localConfig := config
|
||||||
|
localConfig.Host = host
|
||||||
|
localConfig.Port = port
|
||||||
|
localConfig.UseSSH = false
|
||||||
|
|
||||||
|
dsn = o.getDSN(localConfig)
|
||||||
|
logger.Infof("Oracle 通过本地端口转发连接:%s -> %s:%d", forwarder.LocalAddr, config.Host, config.Port)
|
||||||
|
} else {
|
||||||
|
dsn = o.getDSN(config)
|
||||||
|
}
|
||||||
|
|
||||||
db, err := sql.Open("oracle", dsn)
|
db, err := sql.Open("oracle", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("打开数据库连接失败:%w", err)
|
return fmt.Errorf("打开数据库连接失败:%w", err)
|
||||||
@@ -75,6 +91,15 @@ func (o *OracleDB) Connect(config connection.ConnectionConfig) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *OracleDB) Close() error {
|
func (o *OracleDB) Close() error {
|
||||||
|
// Close SSH forwarder first if exists
|
||||||
|
if o.forwarder != nil {
|
||||||
|
if err := o.forwarder.Close(); err != nil {
|
||||||
|
logger.Warnf("关闭 Oracle SSH 端口转发失败:%v", err)
|
||||||
|
}
|
||||||
|
o.forwarder = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then close database connection
|
||||||
if o.conn != nil {
|
if o.conn != nil {
|
||||||
return o.conn.Close()
|
return o.conn.Close()
|
||||||
}
|
}
|
||||||
@@ -94,6 +119,20 @@ func (o *OracleDB) Ping() error {
|
|||||||
return o.conn.PingContext(ctx)
|
return o.conn.PingContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *OracleDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||||||
|
if o.conn == nil {
|
||||||
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := o.conn.QueryContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func (o *OracleDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
func (o *OracleDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||||||
if o.conn == nil {
|
if o.conn == nil {
|
||||||
return nil, nil, fmt.Errorf("connection not open")
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
@@ -104,33 +143,18 @@ func (o *OracleDB) Query(query string) ([]map[string]interface{}, []string, erro
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
columns, err := rows.Columns()
|
func (o *OracleDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||||||
|
if o.conn == nil {
|
||||||
|
return 0, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
res, err := o.conn.ExecContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
var resultData []map[string]interface{}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]interface{}, len(columns))
|
|
||||||
valuePtrs := make([]interface{}, len(columns))
|
|
||||||
for i := range columns {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := make(map[string]interface{})
|
|
||||||
for i, col := range columns {
|
|
||||||
entry[col] = normalizeQueryValue(values[i])
|
|
||||||
}
|
|
||||||
resultData = append(resultData, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultData, columns, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OracleDB) Exec(query string) (int64, error) {
|
func (o *OracleDB) Exec(query string) (int64, error) {
|
||||||
|
|||||||
@@ -1,24 +1,31 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
|
"GoNavi-Wails/internal/logger"
|
||||||
|
"GoNavi-Wails/internal/ssh"
|
||||||
"GoNavi-Wails/internal/utils"
|
"GoNavi-Wails/internal/utils"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type PostgresDB struct {
|
type PostgresDB struct {
|
||||||
conn *sql.DB
|
conn *sql.DB
|
||||||
pingTimeout time.Duration
|
pingTimeout time.Duration
|
||||||
|
forwarder *ssh.LocalForwarder // Store SSH tunnel forwarder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (p *PostgresDB) getDSN(config connection.ConnectionConfig) string {
|
func (p *PostgresDB) getDSN(config connection.ConnectionConfig) string {
|
||||||
// postgres://user:password@host:port/dbname?sslmode=disable
|
// postgres://user:password@host:port/dbname?sslmode=disable
|
||||||
dbname := config.Database
|
dbname := config.Database
|
||||||
@@ -41,7 +48,42 @@ func (p *PostgresDB) getDSN(config connection.ConnectionConfig) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) Connect(config connection.ConnectionConfig) error {
|
func (p *PostgresDB) Connect(config connection.ConnectionConfig) error {
|
||||||
dsn := p.getDSN(config)
|
var dsn string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if config.UseSSH {
|
||||||
|
// Create SSH tunnel with local port forwarding
|
||||||
|
logger.Infof("PostgreSQL 使用 SSH 连接:地址=%s:%d 用户=%s", config.Host, config.Port, config.User)
|
||||||
|
|
||||||
|
forwarder, err := ssh.GetOrCreateLocalForwarder(config.SSH, config.Host, config.Port)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("创建 SSH 隧道失败:%w", err)
|
||||||
|
}
|
||||||
|
p.forwarder = forwarder
|
||||||
|
|
||||||
|
// Parse local address
|
||||||
|
host, portStr, err := net.SplitHostPort(forwarder.LocalAddr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地转发地址失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
port, err := strconv.Atoi(portStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("解析本地端口失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a modified config pointing to local forwarder
|
||||||
|
localConfig := config
|
||||||
|
localConfig.Host = host
|
||||||
|
localConfig.Port = port
|
||||||
|
localConfig.UseSSH = false // Disable SSH flag for DSN generation
|
||||||
|
|
||||||
|
dsn = p.getDSN(localConfig)
|
||||||
|
logger.Infof("PostgreSQL 通过本地端口转发连接:%s -> %s:%d", forwarder.LocalAddr, config.Host, config.Port)
|
||||||
|
} else {
|
||||||
|
dsn = p.getDSN(config)
|
||||||
|
}
|
||||||
|
|
||||||
db, err := sql.Open("postgres", dsn)
|
db, err := sql.Open("postgres", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("打开数据库连接失败:%w", err)
|
return fmt.Errorf("打开数据库连接失败:%w", err)
|
||||||
@@ -56,7 +98,17 @@ func (p *PostgresDB) Connect(config connection.ConnectionConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (p *PostgresDB) Close() error {
|
func (p *PostgresDB) Close() error {
|
||||||
|
// Close SSH forwarder first if exists
|
||||||
|
if p.forwarder != nil {
|
||||||
|
if err := p.forwarder.Close(); err != nil {
|
||||||
|
logger.Warnf("关闭 PostgreSQL SSH 端口转发失败:%v", err)
|
||||||
|
}
|
||||||
|
p.forwarder = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then close database connection
|
||||||
if p.conn != nil {
|
if p.conn != nil {
|
||||||
return p.conn.Close()
|
return p.conn.Close()
|
||||||
}
|
}
|
||||||
@@ -76,6 +128,20 @@ func (p *PostgresDB) Ping() error {
|
|||||||
return p.conn.PingContext(ctx)
|
return p.conn.PingContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PostgresDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||||||
|
if p.conn == nil {
|
||||||
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := p.conn.QueryContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
func (p *PostgresDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||||||
if p.conn == nil {
|
if p.conn == nil {
|
||||||
return nil, nil, fmt.Errorf("connection not open")
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
@@ -86,33 +152,18 @@ func (p *PostgresDB) Query(query string) ([]map[string]interface{}, []string, er
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
columns, err := rows.Columns()
|
func (p *PostgresDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||||||
|
if p.conn == nil {
|
||||||
|
return 0, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
res, err := p.conn.ExecContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
var resultData []map[string]interface{}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]interface{}, len(columns))
|
|
||||||
valuePtrs := make([]interface{}, len(columns))
|
|
||||||
for i := range columns {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := make(map[string]interface{})
|
|
||||||
for i, col := range columns {
|
|
||||||
entry[col] = normalizeQueryValue(values[i])
|
|
||||||
}
|
|
||||||
resultData = append(resultData, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultData, columns, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) Exec(query string) (int64, error) {
|
func (p *PostgresDB) Exec(query string) (int64, error) {
|
||||||
@@ -167,21 +218,306 @@ func (p *PostgresDB) GetCreateStatement(dbName, tableName string) (string, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
func (p *PostgresDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
||||||
return []connection.ColumnDefinition{}, nil
|
schema := strings.TrimSpace(dbName)
|
||||||
|
if schema == "" {
|
||||||
|
schema = "public"
|
||||||
|
}
|
||||||
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(s string) string { return strings.ReplaceAll(s, "'", "''") }
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
a.attname AS column_name,
|
||||||
|
pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type,
|
||||||
|
CASE WHEN a.attnotnull THEN 'NO' ELSE 'YES' END AS is_nullable,
|
||||||
|
pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
|
||||||
|
col_description(a.attrelid, a.attnum) AS comment,
|
||||||
|
CASE WHEN pk.attname IS NOT NULL THEN 'PRI' ELSE '' END AS column_key
|
||||||
|
FROM pg_class c
|
||||||
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||||
|
JOIN pg_attribute a ON a.attrelid = c.oid
|
||||||
|
LEFT JOIN pg_attrdef ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT i.indrelid, a3.attname
|
||||||
|
FROM pg_index i
|
||||||
|
JOIN pg_attribute a3 ON a3.attrelid = i.indrelid AND a3.attnum = ANY(i.indkey)
|
||||||
|
WHERE i.indisprimary
|
||||||
|
) pk ON pk.indrelid = c.oid AND pk.attname = a.attname
|
||||||
|
WHERE c.relkind IN ('r', 'p')
|
||||||
|
AND n.nspname = '%s'
|
||||||
|
AND c.relname = '%s'
|
||||||
|
AND a.attnum > 0
|
||||||
|
AND NOT a.attisdropped
|
||||||
|
ORDER BY a.attnum`, esc(schema), esc(table))
|
||||||
|
|
||||||
|
data, _, err := p.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var columns []connection.ColumnDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
col := connection.ColumnDefinition{
|
||||||
|
Name: fmt.Sprintf("%v", row["column_name"]),
|
||||||
|
Type: fmt.Sprintf("%v", row["data_type"]),
|
||||||
|
Nullable: fmt.Sprintf("%v", row["is_nullable"]),
|
||||||
|
Key: fmt.Sprintf("%v", row["column_key"]),
|
||||||
|
Extra: "",
|
||||||
|
Comment: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := row["comment"]; ok && v != nil {
|
||||||
|
col.Comment = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := row["column_default"]; ok && v != nil {
|
||||||
|
def := fmt.Sprintf("%v", v)
|
||||||
|
col.Default = &def
|
||||||
|
if strings.HasPrefix(strings.ToLower(strings.TrimSpace(def)), "nextval(") {
|
||||||
|
col.Extra = "auto_increment"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
columns = append(columns, col)
|
||||||
|
}
|
||||||
|
return columns, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
func (p *PostgresDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
||||||
return []connection.IndexDefinition{}, nil
|
schema := strings.TrimSpace(dbName)
|
||||||
|
if schema == "" {
|
||||||
|
schema = "public"
|
||||||
|
}
|
||||||
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(s string) string { return strings.ReplaceAll(s, "'", "''") }
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
i.relname AS index_name,
|
||||||
|
a.attname AS column_name,
|
||||||
|
ix.indisunique AS is_unique,
|
||||||
|
x.ordinality AS seq_in_index,
|
||||||
|
am.amname AS index_type
|
||||||
|
FROM pg_class t
|
||||||
|
JOIN pg_namespace n ON n.oid = t.relnamespace
|
||||||
|
JOIN pg_index ix ON t.oid = ix.indrelid
|
||||||
|
JOIN pg_class i ON i.oid = ix.indexrelid
|
||||||
|
JOIN pg_am am ON i.relam = am.oid
|
||||||
|
JOIN unnest(ix.indkey) WITH ORDINALITY AS x(attnum, ordinality) ON TRUE
|
||||||
|
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = x.attnum
|
||||||
|
WHERE t.relkind IN ('r', 'p')
|
||||||
|
AND t.relname = '%s'
|
||||||
|
AND n.nspname = '%s'
|
||||||
|
ORDER BY i.relname, x.ordinality`, esc(table), esc(schema))
|
||||||
|
|
||||||
|
data, _, err := p.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parseBool := func(v interface{}) bool {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case bool:
|
||||||
|
return val
|
||||||
|
case string:
|
||||||
|
s := strings.ToLower(strings.TrimSpace(val))
|
||||||
|
return s == "t" || s == "true" || s == "1" || s == "y" || s == "yes"
|
||||||
|
default:
|
||||||
|
s := strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", v)))
|
||||||
|
return s == "t" || s == "true" || s == "1" || s == "y" || s == "yes"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parseInt := func(v interface{}) int {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case int:
|
||||||
|
return val
|
||||||
|
case int64:
|
||||||
|
return int(val)
|
||||||
|
case float64:
|
||||||
|
return int(val)
|
||||||
|
case string:
|
||||||
|
// best effort
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(val), "%d", &n)
|
||||||
|
return n
|
||||||
|
default:
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(fmt.Sprintf("%v", v)), "%d", &n)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var indexes []connection.IndexDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
isUnique := false
|
||||||
|
if v, ok := row["is_unique"]; ok && v != nil {
|
||||||
|
isUnique = parseBool(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
nonUnique := 1
|
||||||
|
if isUnique {
|
||||||
|
nonUnique = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
seq := 0
|
||||||
|
if v, ok := row["seq_in_index"]; ok && v != nil {
|
||||||
|
seq = parseInt(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
indexType := ""
|
||||||
|
if v, ok := row["index_type"]; ok && v != nil {
|
||||||
|
indexType = strings.ToUpper(fmt.Sprintf("%v", v))
|
||||||
|
}
|
||||||
|
if indexType == "" {
|
||||||
|
indexType = "BTREE"
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := connection.IndexDefinition{
|
||||||
|
Name: fmt.Sprintf("%v", row["index_name"]),
|
||||||
|
ColumnName: fmt.Sprintf("%v", row["column_name"]),
|
||||||
|
NonUnique: nonUnique,
|
||||||
|
SeqInIndex: seq,
|
||||||
|
IndexType: indexType,
|
||||||
|
}
|
||||||
|
indexes = append(indexes, idx)
|
||||||
|
}
|
||||||
|
return indexes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
func (p *PostgresDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
||||||
return []connection.ForeignKeyDefinition{}, nil
|
schema := strings.TrimSpace(dbName)
|
||||||
|
if schema == "" {
|
||||||
|
schema = "public"
|
||||||
|
}
|
||||||
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(s string) string { return strings.ReplaceAll(s, "'", "''") }
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`
|
||||||
|
SELECT
|
||||||
|
tc.constraint_name AS constraint_name,
|
||||||
|
kcu.column_name AS column_name,
|
||||||
|
ccu.table_schema AS foreign_table_schema,
|
||||||
|
ccu.table_name AS foreign_table_name,
|
||||||
|
ccu.column_name AS foreign_column_name
|
||||||
|
FROM information_schema.table_constraints AS tc
|
||||||
|
JOIN information_schema.key_column_usage AS kcu
|
||||||
|
ON tc.constraint_name = kcu.constraint_name
|
||||||
|
AND tc.table_schema = kcu.table_schema
|
||||||
|
JOIN information_schema.constraint_column_usage AS ccu
|
||||||
|
ON ccu.constraint_name = tc.constraint_name
|
||||||
|
AND ccu.table_schema = tc.table_schema
|
||||||
|
WHERE tc.constraint_type = 'FOREIGN KEY'
|
||||||
|
AND tc.table_name = '%s'
|
||||||
|
AND tc.table_schema = '%s'
|
||||||
|
ORDER BY tc.constraint_name, kcu.ordinal_position`, esc(table), esc(schema))
|
||||||
|
|
||||||
|
data, _, err := p.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var fks []connection.ForeignKeyDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
refSchema := ""
|
||||||
|
if v, ok := row["foreign_table_schema"]; ok && v != nil {
|
||||||
|
refSchema = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
refTable := fmt.Sprintf("%v", row["foreign_table_name"])
|
||||||
|
refTableName := refTable
|
||||||
|
if strings.TrimSpace(refSchema) != "" {
|
||||||
|
refTableName = fmt.Sprintf("%s.%s", refSchema, refTable)
|
||||||
|
}
|
||||||
|
|
||||||
|
fk := connection.ForeignKeyDefinition{
|
||||||
|
Name: fmt.Sprintf("%v", row["constraint_name"]),
|
||||||
|
ColumnName: fmt.Sprintf("%v", row["column_name"]),
|
||||||
|
RefTableName: refTableName,
|
||||||
|
RefColumnName: fmt.Sprintf("%v", row["foreign_column_name"]),
|
||||||
|
ConstraintName: fmt.Sprintf("%v", row["constraint_name"]),
|
||||||
|
}
|
||||||
|
fks = append(fks, fk)
|
||||||
|
}
|
||||||
|
return fks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
func (p *PostgresDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
||||||
return []connection.TriggerDefinition{}, nil
|
schema := strings.TrimSpace(dbName)
|
||||||
|
if schema == "" {
|
||||||
|
schema = "public"
|
||||||
|
}
|
||||||
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(s string) string { return strings.ReplaceAll(s, "'", "''") }
|
||||||
|
|
||||||
|
query := fmt.Sprintf(`
|
||||||
|
SELECT trigger_name, action_timing, event_manipulation, action_statement
|
||||||
|
FROM information_schema.triggers
|
||||||
|
WHERE event_object_table = '%s'
|
||||||
|
AND event_object_schema = '%s'
|
||||||
|
ORDER BY trigger_name, event_manipulation`, esc(table), esc(schema))
|
||||||
|
|
||||||
|
data, _, err := p.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var triggers []connection.TriggerDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
trig := connection.TriggerDefinition{
|
||||||
|
Name: fmt.Sprintf("%v", row["trigger_name"]),
|
||||||
|
Timing: fmt.Sprintf("%v", row["action_timing"]),
|
||||||
|
Event: fmt.Sprintf("%v", row["event_manipulation"]),
|
||||||
|
Statement: fmt.Sprintf("%v", row["action_statement"]),
|
||||||
|
}
|
||||||
|
triggers = append(triggers, trig)
|
||||||
|
}
|
||||||
|
return triggers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PostgresDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
func (p *PostgresDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
||||||
return []connection.ColumnDefinitionWithTable{}, nil
|
query := `
|
||||||
|
SELECT table_schema, table_name, column_name, data_type
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||||
|
AND table_schema NOT LIKE 'pg_%'
|
||||||
|
ORDER BY table_schema, table_name, ordinal_position`
|
||||||
|
|
||||||
|
data, _, err := p.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var cols []connection.ColumnDefinitionWithTable
|
||||||
|
for _, row := range data {
|
||||||
|
schema := fmt.Sprintf("%v", row["table_schema"])
|
||||||
|
table := fmt.Sprintf("%v", row["table_name"])
|
||||||
|
tableName := table
|
||||||
|
if strings.TrimSpace(schema) != "" {
|
||||||
|
tableName = fmt.Sprintf("%s.%s", schema, table)
|
||||||
|
}
|
||||||
|
|
||||||
|
col := connection.ColumnDefinitionWithTable{
|
||||||
|
TableName: tableName,
|
||||||
|
Name: fmt.Sprintf("%v", row["column_name"]),
|
||||||
|
Type: fmt.Sprintf("%v", row["data_type"]),
|
||||||
|
}
|
||||||
|
cols = append(cols, col)
|
||||||
|
}
|
||||||
|
return cols, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
@@ -9,13 +11,17 @@ import (
|
|||||||
// normalizeQueryValue normalizes driver-returned values for UI/JSON transport.
|
// normalizeQueryValue normalizes driver-returned values for UI/JSON transport.
|
||||||
// 当前主要处理 []byte:如果是可读文本则转为 string,否则转为十六进制字符串,避免前端出现“空白值”。
|
// 当前主要处理 []byte:如果是可读文本则转为 string,否则转为十六进制字符串,避免前端出现“空白值”。
|
||||||
func normalizeQueryValue(v interface{}) interface{} {
|
func normalizeQueryValue(v interface{}) interface{} {
|
||||||
|
return normalizeQueryValueWithDBType(v, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeQueryValueWithDBType(v interface{}, databaseTypeName string) interface{} {
|
||||||
if b, ok := v.([]byte); ok {
|
if b, ok := v.([]byte); ok {
|
||||||
return bytesToReadableString(b)
|
return bytesToDisplayValue(b, databaseTypeName)
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func bytesToReadableString(b []byte) interface{} {
|
func bytesToDisplayValue(b []byte, databaseTypeName string) interface{} {
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -23,6 +29,18 @@ func bytesToReadableString(b []byte) interface{} {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbType := strings.ToUpper(strings.TrimSpace(databaseTypeName))
|
||||||
|
if isBitLikeDBType(dbType) {
|
||||||
|
if u, ok := bytesToUint64(b); ok {
|
||||||
|
// JS number precision is limited; keep large bitmasks as string.
|
||||||
|
const maxSafeInteger = 9007199254740991 // 2^53 - 1
|
||||||
|
if u <= maxSafeInteger {
|
||||||
|
return int64(u)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%d", u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if utf8.Valid(b) {
|
if utf8.Valid(b) {
|
||||||
s := string(b)
|
s := string(b)
|
||||||
if isMostlyPrintable(s) {
|
if isMostlyPrintable(s) {
|
||||||
@@ -30,9 +48,47 @@ func bytesToReadableString(b []byte) interface{} {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fallback: some drivers return BIT(1) as []byte{0} / []byte{1} without type info.
|
||||||
|
if dbType == "" && len(b) == 1 && (b[0] == 0 || b[0] == 1) {
|
||||||
|
return int64(b[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytesToReadableString(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bytesToReadableString(b []byte) interface{} {
|
||||||
|
if b == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if len(b) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
return "0x" + hex.EncodeToString(b)
|
return "0x" + hex.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isBitLikeDBType(typeName string) bool {
|
||||||
|
if typeName == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch typeName {
|
||||||
|
case "BIT", "VARBIT":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return strings.HasPrefix(typeName, "BIT")
|
||||||
|
}
|
||||||
|
|
||||||
|
func bytesToUint64(b []byte) (uint64, bool) {
|
||||||
|
if len(b) == 0 || len(b) > 8 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
var u uint64
|
||||||
|
for _, v := range b {
|
||||||
|
u = (u << 8) | uint64(v)
|
||||||
|
}
|
||||||
|
return u, true
|
||||||
|
}
|
||||||
|
|
||||||
func isMostlyPrintable(s string) bool {
|
func isMostlyPrintable(s string) bool {
|
||||||
if s == "" {
|
if s == "" {
|
||||||
return true
|
return true
|
||||||
|
|||||||
44
internal/db/query_value_test.go
Normal file
44
internal/db/query_value_test.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestNormalizeQueryValueWithDBType_BitBytes(t *testing.T) {
|
||||||
|
v := normalizeQueryValueWithDBType([]byte{0x00}, "BIT")
|
||||||
|
if v != int64(0) {
|
||||||
|
t.Fatalf("BIT 0x00 期望为 0,实际=%v(%T)", v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v = normalizeQueryValueWithDBType([]byte{0x01}, "bit")
|
||||||
|
if v != int64(1) {
|
||||||
|
t.Fatalf("BIT 0x01 期望为 1,实际=%v(%T)", v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v = normalizeQueryValueWithDBType([]byte{0x01, 0x02}, "BIT VARYING")
|
||||||
|
if v != int64(258) {
|
||||||
|
t.Fatalf("BIT 0x0102 期望为 258,实际=%v(%T)", v, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeQueryValueWithDBType_BitLargeAsString(t *testing.T) {
|
||||||
|
v := normalizeQueryValueWithDBType([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, "BIT")
|
||||||
|
if s, ok := v.(string); !ok || s != "18446744073709551615" {
|
||||||
|
t.Fatalf("BIT 0xffffffffffffffff 期望为 string(18446744073709551615),实际=%v(%T)", v, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeQueryValueWithDBType_ByteFallbacks(t *testing.T) {
|
||||||
|
v := normalizeQueryValueWithDBType([]byte("abc"), "")
|
||||||
|
if v != "abc" {
|
||||||
|
t.Fatalf("文本 []byte 期望返回 string,实际=%v(%T)", v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v = normalizeQueryValueWithDBType([]byte{0x00}, "")
|
||||||
|
if v != int64(0) {
|
||||||
|
t.Fatalf("未知类型 0x00 期望返回 0,实际=%v(%T)", v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
v = normalizeQueryValueWithDBType([]byte{0xff}, "")
|
||||||
|
if v != "0xff" {
|
||||||
|
t.Fatalf("未知类型 0xff 期望返回 0xff,实际=%v(%T)", v, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
46
internal/db/scan_rows.go
Normal file
46
internal/db/scan_rows.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func scanRows(rows *sql.Rows) ([]map[string]interface{}, []string, error) {
|
||||||
|
columns, err := rows.Columns()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
colTypes, err := rows.ColumnTypes()
|
||||||
|
if err != nil || len(colTypes) != len(columns) {
|
||||||
|
colTypes = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
resultData := make([]map[string]interface{}, 0)
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
values := make([]interface{}, len(columns))
|
||||||
|
valuePtrs := make([]interface{}, len(columns))
|
||||||
|
for i := range columns {
|
||||||
|
valuePtrs[i] = &values[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Scan(valuePtrs...); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := make(map[string]interface{}, len(columns))
|
||||||
|
for i, col := range columns {
|
||||||
|
dbTypeName := ""
|
||||||
|
if colTypes != nil && i < len(colTypes) && colTypes[i] != nil {
|
||||||
|
dbTypeName = colTypes[i].DatabaseTypeName()
|
||||||
|
}
|
||||||
|
entry[col] = normalizeQueryValueWithDBType(values[i], dbTypeName)
|
||||||
|
}
|
||||||
|
resultData = append(resultData, entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return resultData, columns, err
|
||||||
|
}
|
||||||
|
return resultData, columns, nil
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
@@ -52,6 +54,20 @@ func (s *SQLiteDB) Ping() error {
|
|||||||
return s.conn.PingContext(ctx)
|
return s.conn.PingContext(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SQLiteDB) QueryContext(ctx context.Context, query string) ([]map[string]interface{}, []string, error) {
|
||||||
|
if s.conn == nil {
|
||||||
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.conn.QueryContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SQLiteDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
func (s *SQLiteDB) Query(query string) ([]map[string]interface{}, []string, error) {
|
||||||
if s.conn == nil {
|
if s.conn == nil {
|
||||||
return nil, nil, fmt.Errorf("connection not open")
|
return nil, nil, fmt.Errorf("connection not open")
|
||||||
@@ -62,33 +78,18 @@ func (s *SQLiteDB) Query(query string) ([]map[string]interface{}, []string, erro
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
return scanRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
columns, err := rows.Columns()
|
func (s *SQLiteDB) ExecContext(ctx context.Context, query string) (int64, error) {
|
||||||
|
if s.conn == nil {
|
||||||
|
return 0, fmt.Errorf("connection not open")
|
||||||
|
}
|
||||||
|
res, err := s.conn.ExecContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return res.RowsAffected()
|
||||||
var resultData []map[string]interface{}
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
values := make([]interface{}, len(columns))
|
|
||||||
valuePtrs := make([]interface{}, len(columns))
|
|
||||||
for i := range columns {
|
|
||||||
valuePtrs[i] = &values[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rows.Scan(valuePtrs...); err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := make(map[string]interface{})
|
|
||||||
for i, col := range columns {
|
|
||||||
entry[col] = normalizeQueryValue(values[i])
|
|
||||||
}
|
|
||||||
resultData = append(resultData, entry)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultData, columns, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDB) Exec(query string) (int64, error) {
|
func (s *SQLiteDB) Exec(query string) (int64, error) {
|
||||||
@@ -137,21 +138,336 @@ func (s *SQLiteDB) GetCreateStatement(dbName, tableName string) (string, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
func (s *SQLiteDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) {
|
||||||
return []connection.ColumnDefinition{}, nil
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(v string) string { return strings.ReplaceAll(v, "'", "''") }
|
||||||
|
|
||||||
|
// cid, name, type, notnull, dflt_value, pk
|
||||||
|
data, _, err := s.Query(fmt.Sprintf("PRAGMA table_info('%s')", esc(table)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parseInt := func(v interface{}) int {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case int:
|
||||||
|
return val
|
||||||
|
case int64:
|
||||||
|
return int(val)
|
||||||
|
case float64:
|
||||||
|
return int(val)
|
||||||
|
case string:
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(val), "%d", &n)
|
||||||
|
return n
|
||||||
|
default:
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(fmt.Sprintf("%v", v)), "%d", &n)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getStr := func(row map[string]interface{}, key string) string {
|
||||||
|
if v, ok := row[key]; ok && v != nil {
|
||||||
|
return fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
if v, ok := row[strings.ToUpper(key)]; ok && v != nil {
|
||||||
|
return fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var columns []connection.ColumnDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
notnull := 0
|
||||||
|
if v, ok := row["notnull"]; ok && v != nil {
|
||||||
|
notnull = parseInt(v)
|
||||||
|
} else if v, ok := row["NOTNULL"]; ok && v != nil {
|
||||||
|
notnull = parseInt(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
pk := 0
|
||||||
|
if v, ok := row["pk"]; ok && v != nil {
|
||||||
|
pk = parseInt(v)
|
||||||
|
} else if v, ok := row["PK"]; ok && v != nil {
|
||||||
|
pk = parseInt(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
nullable := "YES"
|
||||||
|
if notnull == 1 {
|
||||||
|
nullable = "NO"
|
||||||
|
}
|
||||||
|
|
||||||
|
key := ""
|
||||||
|
if pk == 1 {
|
||||||
|
key = "PRI"
|
||||||
|
}
|
||||||
|
|
||||||
|
col := connection.ColumnDefinition{
|
||||||
|
Name: getStr(row, "name"),
|
||||||
|
Type: getStr(row, "type"),
|
||||||
|
Nullable: nullable,
|
||||||
|
Key: key,
|
||||||
|
Extra: "",
|
||||||
|
Comment: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := row["dflt_value"]; ok && v != nil {
|
||||||
|
def := fmt.Sprintf("%v", v)
|
||||||
|
col.Default = &def
|
||||||
|
} else if v, ok := row["DFLT_VALUE"]; ok && v != nil {
|
||||||
|
def := fmt.Sprintf("%v", v)
|
||||||
|
col.Default = &def
|
||||||
|
}
|
||||||
|
|
||||||
|
columns = append(columns, col)
|
||||||
|
}
|
||||||
|
return columns, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
func (s *SQLiteDB) GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error) {
|
||||||
return []connection.IndexDefinition{}, nil
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(v string) string { return strings.ReplaceAll(v, "'", "''") }
|
||||||
|
parseInt := func(v interface{}) int {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case int:
|
||||||
|
return val
|
||||||
|
case int64:
|
||||||
|
return int(val)
|
||||||
|
case float64:
|
||||||
|
return int(val)
|
||||||
|
case string:
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(val), "%d", &n)
|
||||||
|
return n
|
||||||
|
default:
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(fmt.Sprintf("%v", v)), "%d", &n)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, _, err := s.Query(fmt.Sprintf("PRAGMA index_list('%s')", esc(table)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var indexes []connection.IndexDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
indexName := ""
|
||||||
|
if v, ok := row["name"]; ok && v != nil {
|
||||||
|
indexName = fmt.Sprintf("%v", v)
|
||||||
|
} else if v, ok := row["NAME"]; ok && v != nil {
|
||||||
|
indexName = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(indexName) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
unique := 0
|
||||||
|
if v, ok := row["unique"]; ok && v != nil {
|
||||||
|
unique = parseInt(v)
|
||||||
|
} else if v, ok := row["UNIQUE"]; ok && v != nil {
|
||||||
|
unique = parseInt(v)
|
||||||
|
}
|
||||||
|
nonUnique := 1
|
||||||
|
if unique == 1 {
|
||||||
|
nonUnique = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cols, _, err := s.Query(fmt.Sprintf("PRAGMA index_info('%s')", esc(indexName)))
|
||||||
|
if err != nil {
|
||||||
|
// skip broken index
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cols {
|
||||||
|
colName := ""
|
||||||
|
if v, ok := c["name"]; ok && v != nil {
|
||||||
|
colName = fmt.Sprintf("%v", v)
|
||||||
|
} else if v, ok := c["NAME"]; ok && v != nil {
|
||||||
|
colName = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(colName) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
seq := 0
|
||||||
|
if v, ok := c["seqno"]; ok && v != nil {
|
||||||
|
seq = parseInt(v) + 1
|
||||||
|
} else if v, ok := c["SEQNO"]; ok && v != nil {
|
||||||
|
seq = parseInt(v) + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
indexes = append(indexes, connection.IndexDefinition{
|
||||||
|
Name: indexName,
|
||||||
|
ColumnName: colName,
|
||||||
|
NonUnique: nonUnique,
|
||||||
|
SeqInIndex: seq,
|
||||||
|
IndexType: "BTREE",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return indexes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
func (s *SQLiteDB) GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error) {
|
||||||
return []connection.ForeignKeyDefinition{}, nil
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(v string) string { return strings.ReplaceAll(v, "'", "''") }
|
||||||
|
|
||||||
|
data, _, err := s.Query(fmt.Sprintf("PRAGMA foreign_key_list('%s')", esc(table)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parseInt := func(v interface{}) int {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case int:
|
||||||
|
return val
|
||||||
|
case int64:
|
||||||
|
return int(val)
|
||||||
|
case float64:
|
||||||
|
return int(val)
|
||||||
|
case string:
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(val), "%d", &n)
|
||||||
|
return n
|
||||||
|
default:
|
||||||
|
var n int
|
||||||
|
_, _ = fmt.Sscanf(strings.TrimSpace(fmt.Sprintf("%v", v)), "%d", &n)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var fks []connection.ForeignKeyDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
id := 0
|
||||||
|
if v, ok := row["id"]; ok && v != nil {
|
||||||
|
id = parseInt(v)
|
||||||
|
} else if v, ok := row["ID"]; ok && v != nil {
|
||||||
|
id = parseInt(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
refTable := ""
|
||||||
|
if v, ok := row["table"]; ok && v != nil {
|
||||||
|
refTable = fmt.Sprintf("%v", v)
|
||||||
|
} else if v, ok := row["TABLE"]; ok && v != nil {
|
||||||
|
refTable = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fromCol := ""
|
||||||
|
if v, ok := row["from"]; ok && v != nil {
|
||||||
|
fromCol = fmt.Sprintf("%v", v)
|
||||||
|
} else if v, ok := row["FROM"]; ok && v != nil {
|
||||||
|
fromCol = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
toCol := ""
|
||||||
|
if v, ok := row["to"]; ok && v != nil {
|
||||||
|
toCol = fmt.Sprintf("%v", v)
|
||||||
|
} else if v, ok := row["TO"]; ok && v != nil {
|
||||||
|
toCol = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
name := fmt.Sprintf("fk_%s_%d", table, id)
|
||||||
|
fks = append(fks, connection.ForeignKeyDefinition{
|
||||||
|
Name: name,
|
||||||
|
ColumnName: fromCol,
|
||||||
|
RefTableName: refTable,
|
||||||
|
RefColumnName: toCol,
|
||||||
|
ConstraintName: name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return fks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
func (s *SQLiteDB) GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error) {
|
||||||
return []connection.TriggerDefinition{}, nil
|
table := strings.TrimSpace(tableName)
|
||||||
|
if table == "" {
|
||||||
|
return nil, fmt.Errorf("table name required")
|
||||||
|
}
|
||||||
|
|
||||||
|
esc := func(v string) string { return strings.ReplaceAll(v, "'", "''") }
|
||||||
|
|
||||||
|
data, _, err := s.Query(fmt.Sprintf("SELECT name AS trigger_name, sql AS statement FROM sqlite_master WHERE type='trigger' AND tbl_name='%s' ORDER BY name", esc(table)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var triggers []connection.TriggerDefinition
|
||||||
|
for _, row := range data {
|
||||||
|
name := fmt.Sprintf("%v", row["trigger_name"])
|
||||||
|
stmt := ""
|
||||||
|
if v, ok := row["statement"]; ok && v != nil {
|
||||||
|
stmt = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
upper := strings.ToUpper(stmt)
|
||||||
|
timing := ""
|
||||||
|
switch {
|
||||||
|
case strings.Contains(upper, " BEFORE "):
|
||||||
|
timing = "BEFORE"
|
||||||
|
case strings.Contains(upper, " AFTER "):
|
||||||
|
timing = "AFTER"
|
||||||
|
case strings.Contains(upper, " INSTEAD OF "):
|
||||||
|
timing = "INSTEAD OF"
|
||||||
|
}
|
||||||
|
|
||||||
|
event := ""
|
||||||
|
switch {
|
||||||
|
case strings.Contains(upper, " INSERT "):
|
||||||
|
event = "INSERT"
|
||||||
|
case strings.Contains(upper, " UPDATE "):
|
||||||
|
event = "UPDATE"
|
||||||
|
case strings.Contains(upper, " DELETE "):
|
||||||
|
event = "DELETE"
|
||||||
|
}
|
||||||
|
|
||||||
|
triggers = append(triggers, connection.TriggerDefinition{
|
||||||
|
Name: name,
|
||||||
|
Timing: timing,
|
||||||
|
Event: event,
|
||||||
|
Statement: stmt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return triggers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
func (s *SQLiteDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {
|
||||||
return []connection.ColumnDefinitionWithTable{}, nil
|
tables, err := s.GetTables(dbName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var cols []connection.ColumnDefinitionWithTable
|
||||||
|
for _, table := range tables {
|
||||||
|
// Skip internal tables
|
||||||
|
if strings.HasPrefix(strings.ToLower(table), "sqlite_") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
columns, err := s.GetColumns("", table)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, col := range columns {
|
||||||
|
cols = append(cols, connection.ColumnDefinitionWithTable{
|
||||||
|
TableName: table,
|
||||||
|
Name: col.Name,
|
||||||
|
Type: col.Type,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cols, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package ssh
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"GoNavi-Wails/internal/connection"
|
"GoNavi-Wails/internal/connection"
|
||||||
@@ -110,3 +112,264 @@ func RegisterSSHNetwork(sshConfig connection.SSHConfig) (string, error) {
|
|||||||
|
|
||||||
return netName, nil
|
return netName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sshClientCache stores SSH clients to avoid creating multiple connections
|
||||||
|
var (
|
||||||
|
sshClientCache = make(map[string]*ssh.Client)
|
||||||
|
sshClientCacheMu sync.RWMutex
|
||||||
|
localForwarders = make(map[string]*LocalForwarder)
|
||||||
|
forwarderMu sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
// LocalForwarder represents a local port forwarder through SSH
|
||||||
|
type LocalForwarder struct {
|
||||||
|
LocalAddr string
|
||||||
|
RemoteAddr string
|
||||||
|
SSHClient *ssh.Client
|
||||||
|
listener net.Listener
|
||||||
|
closeChan chan struct{}
|
||||||
|
closeOnce sync.Once // 防止重复关闭
|
||||||
|
closed bool // 关闭状态标记
|
||||||
|
closedMu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLocalForwarder creates a new local port forwarder
|
||||||
|
// It listens on a random local port and forwards all connections through SSH tunnel
|
||||||
|
func NewLocalForwarder(sshConfig connection.SSHConfig, remoteHost string, remotePort int) (*LocalForwarder, error) {
|
||||||
|
client, err := GetOrCreateSSHClient(sshConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("建立 SSH 连接失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen on localhost with a random port
|
||||||
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("创建本地监听器失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
localAddr := listener.Addr().String()
|
||||||
|
remoteAddr := fmt.Sprintf("%s:%d", remoteHost, remotePort)
|
||||||
|
|
||||||
|
forwarder := &LocalForwarder{
|
||||||
|
LocalAddr: localAddr,
|
||||||
|
RemoteAddr: remoteAddr,
|
||||||
|
SSHClient: client,
|
||||||
|
listener: listener,
|
||||||
|
closeChan: make(chan struct{}),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start forwarding in background
|
||||||
|
go forwarder.forward()
|
||||||
|
|
||||||
|
logger.Infof("已创建 SSH 端口转发:本地 %s -> 远程 %s", localAddr, remoteAddr)
|
||||||
|
return forwarder, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// forward handles the port forwarding
|
||||||
|
func (f *LocalForwarder) forward() {
|
||||||
|
for {
|
||||||
|
localConn, err := f.listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
// Check if we're shutting down
|
||||||
|
select {
|
||||||
|
case <-f.closeChan:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
logger.Warnf("接受本地连接失败:%v", err)
|
||||||
|
// listener可能已关闭,退出循环
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go f.handleConnection(localConn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleConnection handles a single connection
|
||||||
|
func (f *LocalForwarder) handleConnection(localConn net.Conn) {
|
||||||
|
defer localConn.Close()
|
||||||
|
|
||||||
|
// Connect to remote through SSH with timeout
|
||||||
|
remoteConn, err := f.SSHClient.Dial("tcp", f.RemoteAddr)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("通过 SSH 连接到远程 %s 失败:%v", f.RemoteAddr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer remoteConn.Close()
|
||||||
|
|
||||||
|
// Bidirectional copy with error channel
|
||||||
|
errc := make(chan error, 2)
|
||||||
|
|
||||||
|
// Copy from local to remote
|
||||||
|
go func() {
|
||||||
|
_, err := io.Copy(remoteConn, localConn)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("本地->远程数据复制错误:%v", err)
|
||||||
|
}
|
||||||
|
errc <- err
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Copy from remote to local
|
||||||
|
go func() {
|
||||||
|
_, err := io.Copy(localConn, remoteConn)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("远程->本地数据复制错误:%v", err)
|
||||||
|
}
|
||||||
|
errc <- err
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for BOTH goroutines to complete
|
||||||
|
<-errc
|
||||||
|
<-errc
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the forwarder (thread-safe, can be called multiple times)
|
||||||
|
func (f *LocalForwarder) Close() error {
|
||||||
|
var err error
|
||||||
|
f.closeOnce.Do(func() {
|
||||||
|
f.closedMu.Lock()
|
||||||
|
f.closed = true
|
||||||
|
f.closedMu.Unlock()
|
||||||
|
|
||||||
|
close(f.closeChan)
|
||||||
|
err = f.listener.Close()
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("关闭端口转发监听器失败:%v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsClosed returns whether the forwarder is closed
|
||||||
|
func (f *LocalForwarder) IsClosed() bool {
|
||||||
|
f.closedMu.RLock()
|
||||||
|
defer f.closedMu.RUnlock()
|
||||||
|
return f.closed
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOrCreateLocalForwarder returns a cached forwarder or creates a new one
|
||||||
|
func GetOrCreateLocalForwarder(sshConfig connection.SSHConfig, remoteHost string, remotePort int) (*LocalForwarder, error) {
|
||||||
|
key := fmt.Sprintf("%s:%d:%s->%s:%d",
|
||||||
|
sshConfig.Host, sshConfig.Port, sshConfig.User,
|
||||||
|
remoteHost, remotePort)
|
||||||
|
|
||||||
|
forwarderMu.RLock()
|
||||||
|
forwarder, exists := localForwarders[key]
|
||||||
|
forwarderMu.RUnlock()
|
||||||
|
|
||||||
|
// Check if exists and is still valid
|
||||||
|
if exists && forwarder != nil && !forwarder.IsClosed() {
|
||||||
|
logger.Infof("复用已有端口转发:%s", key)
|
||||||
|
return forwarder, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove stale forwarder from cache
|
||||||
|
if exists {
|
||||||
|
forwarderMu.Lock()
|
||||||
|
delete(localForwarders, key)
|
||||||
|
forwarderMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
forwarder, err := NewLocalForwarder(sshConfig, remoteHost, remotePort)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
forwarderMu.Lock()
|
||||||
|
localForwarders[key] = forwarder
|
||||||
|
forwarderMu.Unlock()
|
||||||
|
|
||||||
|
return forwarder, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CloseAllForwarders closes all local forwarders
|
||||||
|
func CloseAllForwarders() {
|
||||||
|
forwarderMu.Lock()
|
||||||
|
defer forwarderMu.Unlock()
|
||||||
|
|
||||||
|
for key, forwarder := range localForwarders {
|
||||||
|
if forwarder != nil {
|
||||||
|
_ = forwarder.Close()
|
||||||
|
logger.Infof("已关闭端口转发:%s", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
localForwarders = make(map[string]*LocalForwarder)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// getSSHClientCacheKey generates a unique cache key for SSH config
|
||||||
|
func getSSHClientCacheKey(config connection.SSHConfig) string {
|
||||||
|
return fmt.Sprintf("%s:%d:%s", config.Host, config.Port, config.User)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOrCreateSSHClient returns a cached SSH client or creates a new one
|
||||||
|
func GetOrCreateSSHClient(config connection.SSHConfig) (*ssh.Client, error) {
|
||||||
|
key := getSSHClientCacheKey(config)
|
||||||
|
|
||||||
|
sshClientCacheMu.RLock()
|
||||||
|
client, exists := sshClientCache[key]
|
||||||
|
sshClientCacheMu.RUnlock()
|
||||||
|
|
||||||
|
if exists && client != nil {
|
||||||
|
// Test if connection is still alive by creating a test session
|
||||||
|
session, err := client.NewSession()
|
||||||
|
if err == nil {
|
||||||
|
session.Close()
|
||||||
|
logger.Infof("复用已有 SSH 连接:%s", key)
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
// Connection is dead, remove from cache
|
||||||
|
logger.Warnf("SSH 连接已断开,重新建立:%s (错误: %v)", key, err)
|
||||||
|
sshClientCacheMu.Lock()
|
||||||
|
delete(sshClientCache, key)
|
||||||
|
sshClientCacheMu.Unlock()
|
||||||
|
// Try to close the dead client
|
||||||
|
_ = client.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new SSH client
|
||||||
|
client, err := connectSSH(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache the client
|
||||||
|
sshClientCacheMu.Lock()
|
||||||
|
sshClientCache[key] = client
|
||||||
|
sshClientCacheMu.Unlock()
|
||||||
|
|
||||||
|
logger.Infof("已缓存 SSH 连接:%s", key)
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialThroughSSH creates a connection through SSH tunnel
|
||||||
|
// This is a generic dialer that can be used by any database driver
|
||||||
|
func DialThroughSSH(config connection.SSHConfig, network, address string) (net.Conn, error) {
|
||||||
|
client, err := GetOrCreateSSHClient(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("建立 SSH 连接失败:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := client.Dial(network, address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("通过 SSH 隧道连接到 %s 失败:%w", address, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof("已通过 SSH 隧道连接到:%s", address)
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CloseAllSSHClients closes all cached SSH clients
|
||||||
|
func CloseAllSSHClients() {
|
||||||
|
sshClientCacheMu.Lock()
|
||||||
|
defer sshClientCacheMu.Unlock()
|
||||||
|
|
||||||
|
for key, client := range sshClientCache {
|
||||||
|
if client != nil {
|
||||||
|
_ = client.Close()
|
||||||
|
logger.Infof("已关闭 SSH 连接:%s", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sshClientCache = make(map[string]*ssh.Client)
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user