Skip to content

Commit 06a6924

Browse files
author
之瑛
committed
PullRequest: 463 fix/fix-issue-1615
Merge branch 'fix/fix-issue-1615 of git@code.alipay.com:oceanbase/oceanbase-developer-center.git into dev-4.3.1 https://code.alipay.com/oceanbase/oceanbase-developer-center/pull_requests/463 Signed-off-by: 晓康 <xxk268858@oceanbase.com> * fix: fix issue 1615 * fix: fix issue 1615 oracle nls 类型全部返回时间戳转换数值 * fix: fix issue 1615 oracle nls 类型全部返回时间戳转换数值 - 修复数值多次来回转换会有误差的问题 * feat: change the parameter name of exportToSQL * refactor: change the type of the rows parameter of exportToSQL * feat: rows 调整 * fix: 多行逻辑处理 * refactor: delete console.log * refactor: delete unused code * fix: 搜索后 rows 数据处理 * refactor: delete console.log * feat: 按行、列选中表格导出 SQL 语句 * refactor: Refactor code organization and add comments. * feat: Enhance handling of boundary conditions. * fix: Fix the issue with incorrect row data when exporting SQL for selected rows.
1 parent b8310db commit 06a6924

3 files changed

Lines changed: 80 additions & 26 deletions

File tree

src/page/Workspace/components/DDLResultSet/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,15 @@ const DDLResultSet: React.FC<IProps> = function (props) {
462462
}
463463
function clipSQL() {
464464
if (!tableColumns || (!columnName && !isSelectedRow)) {
465-
copyToSQL(gridRef.current, columns, undefined, session?.connection?.dialectType);
465+
copyToSQL(gridRef.current, columns, undefined, session?.connection?.dialectType, rows);
466466
} else {
467-
copyToSQL(gridRef.current, columns, table?.tableName, session?.connection?.dialectType);
467+
copyToSQL(
468+
gridRef.current,
469+
columns,
470+
table?.tableName,
471+
session?.connection?.dialectType,
472+
rows,
473+
);
468474
}
469475
}
470476
function clipCsv() {

src/page/Workspace/components/DDLResultSet/util.tsx

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,60 @@ export function wrapRow(row, columns: ResultSetColumn[]) {
4343
return newRow;
4444
}
4545

46+
const getSelectedColumnsData = (selectedColumns, rows, columns) => {
47+
const data = [];
48+
const selectedColumnsDataHead = [];
49+
for (let i = 0; i < columns?.length; i++) {
50+
if (selectedColumns.has(columns[i].key)) {
51+
selectedColumnsDataHead.push(columns[i].name);
52+
}
53+
}
54+
data.push(selectedColumnsDataHead);
55+
for (let i = 0; i < rows?.length; i++) {
56+
const selectedRow = [];
57+
for (const item of selectedColumns) {
58+
selectedRow.push(rows?.[i]?.[item]);
59+
}
60+
data.push(selectedRow);
61+
}
62+
return data;
63+
};
64+
4665
export function copyToSQL(
4766
gridRef: DataGridRef,
4867
columns: ResultSetColumn[],
4968
tableName: string = 'tmp_table',
5069
dbMode: ConnectionMode,
70+
allRows?: any,
5171
) {
72+
let newRows = [];
73+
const { selectedRows, selectedColumns, rows } = gridRef;
74+
75+
// export SQL by selected columns
76+
if (selectedColumns?.size > 0) {
77+
const selectedColumnsData = getSelectedColumnsData(selectedColumns, rows, columns);
78+
newRows = rows;
79+
copy(exportToSQL(selectedColumnsData, columns, tableName, dbMode, newRows));
80+
return;
81+
}
82+
5283
const selectData = getSelectedRangeData(gridRef);
53-
if (!selectData) {
84+
if (!selectData) return;
85+
86+
// export SQL by selected rows
87+
if (selectedRows?.size > 0) {
88+
for (let item of selectedRows) {
89+
newRows.push(allRows?.[item]);
90+
}
91+
copy(exportToSQL(selectData, columns, tableName, dbMode, newRows));
5492
return;
5593
}
94+
95+
// export SQL by selected cells
5696
const { rowIdx, endRowIdx } = gridRef.selectedRange;
57-
const newRows = gridRef?.rows.slice(rowIdx, endRowIdx + 1);
58-
copy(exportToSQL(selectData, columns, tableName, dbMode, newRows));
97+
if (rowIdx > -1 && endRowIdx > -1) {
98+
newRows = gridRef?.rows?.slice(rowIdx, endRowIdx + 1);
99+
copy(exportToSQL(selectData, columns, tableName, dbMode, newRows));
100+
return;
101+
}
59102
}

src/util/sqlExport/index.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,40 @@ export default function exportToSQL(
3636
const columnMap: {
3737
[key: string]: ResultSetColumn;
3838
} = {};
39+
3940
columns.forEach((column: ResultSetColumn, index: number) => {
4041
columnMap[column.name] = column;
4142
});
43+
4244
const columnsText = headerColumnNames
4345
.map((columnName) => getQuoteTableName(columnName, dbMode))
4446
.join(',');
47+
48+
const handleRowsText = (rowData, columnIndex) => {
49+
const rowsText = rowData
50+
.map((item, i: number) => {
51+
const columnName = headerColumnNames[i];
52+
const column = columnMap[columnName];
53+
const isMasked = column.masked;
54+
if (isMasked) {
55+
return item || 'NULL';
56+
}
57+
const nlsValueKey = getNlsValueKey(column.key);
58+
const timestamp = rows[columnIndex]?.[nlsValueKey]?.timestamp;
59+
return isMySQL
60+
? mysqlConvertValueToSQLString(item, column.columnType)
61+
: oracleConvertValueToSQLString(item, column.columnType, timestamp);
62+
})
63+
.join(',');
64+
return `insert into ${getQuoteTableName(
65+
tableName,
66+
dbMode,
67+
)}(${columnsText}) values(${rowsText})`;
68+
};
69+
4570
return selectData
4671
.slice(1)
47-
.map((rowData, columnIndex) => {
48-
const rowsText = rowData
49-
.map((item, i: number) => {
50-
const columnName = headerColumnNames[i];
51-
const column = columnMap[columnName];
52-
const isMasked = column.masked;
53-
if (isMasked) {
54-
return item || 'NULL';
55-
}
56-
const nlsValueKey = getNlsValueKey(column.key);
57-
const timestamp = rows[columnIndex]?.[nlsValueKey]?.timestamp;
58-
return isMySQL
59-
? mysqlConvertValueToSQLString(item, column.columnType)
60-
: oracleConvertValueToSQLString(item, column.columnType, timestamp);
61-
})
62-
.join(',');
63-
return `insert into ${getQuoteTableName(
64-
tableName,
65-
dbMode,
66-
)}(${columnsText}) values(${rowsText})`;
67-
})
72+
.map((rowData, columnIndex) => handleRowsText(rowData, columnIndex))
6873
.join(';\n');
6974
}
7075

0 commit comments

Comments
 (0)