mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 06:46:00 +00:00
copy as SQL, json lines
This commit is contained in:
@@ -203,6 +203,7 @@
|
|||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { GridDisplay } from 'dbgate-datalib';
|
import { GridDisplay } from 'dbgate-datalib';
|
||||||
|
import { driverBase } from 'dbgate-tools';
|
||||||
import { getContext } from 'svelte';
|
import { getContext } from 'svelte';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import registerCommand from '../commands/registerCommand';
|
import registerCommand from '../commands/registerCommand';
|
||||||
@@ -372,27 +373,19 @@
|
|||||||
const rows = rowIndexes.map(rowIndex => grider.getRowData(rowIndex));
|
const rows = rowIndexes.map(rowIndex => grider.getRowData(rowIndex));
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const columns = colIndexes.map(col => realColumnUniqueNames[col]);
|
const columns = colIndexes.map(col => realColumnUniqueNames[col]);
|
||||||
copyRowsToClipboard(columns, rows, format);
|
copyRowsToClipboard(format, columns, rows, {
|
||||||
|
schemaName: display?.baseTable?.schemaName,
|
||||||
|
pureName: display?.baseTable?.pureName || 'target',
|
||||||
|
driver: display?.driver || driverBase,
|
||||||
|
keyColumns: display?.baseTable?.primaryKey?.columns?.map(col => col.columnName) || [
|
||||||
|
display?.columns ? display?.columns[0].columnName : columns[0],
|
||||||
|
],
|
||||||
|
});
|
||||||
if (domFocusField) domFocusField.focus();
|
if (domFocusField) domFocusField.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function copyToClipboard() {
|
export function copyToClipboard() {
|
||||||
copyToClipboardCore($copyRowsFormat);
|
copyToClipboardCore($copyRowsFormat);
|
||||||
// const cells = cellsToRegularCells(selectedCells);
|
|
||||||
// const rowIndexes = _.sortBy(_.uniq(cells.map(x => x[0])));
|
|
||||||
// const lines = rowIndexes.map(rowIndex => {
|
|
||||||
// let colIndexes = _.sortBy(cells.filter(x => x[0] == rowIndex).map(x => x[1]));
|
|
||||||
// const rowData = grider.getRowData(rowIndex);
|
|
||||||
// if (!rowData) return '';
|
|
||||||
// const line = colIndexes
|
|
||||||
// .map(col => realColumnUniqueNames[col])
|
|
||||||
// .map(col => extractRowCopiedValue(rowData, col))
|
|
||||||
// .join('\t');
|
|
||||||
// return line;
|
|
||||||
// });
|
|
||||||
// const text = lines.join('\r\n');
|
|
||||||
// copyTextToClipboard(text);
|
|
||||||
// if (domFocusField) domFocusField.focus();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loadNextDataIfNeeded() {
|
export function loadNextDataIfNeeded() {
|
||||||
|
|||||||
@@ -96,18 +96,46 @@ const clipboardJsonFormatter = () => (columns, rows) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// export function formatClipboardSqlInsert(columns, rows) {
|
const clipboardJsonLinesFormatter = () => (columns, rows) => {
|
||||||
// }
|
return rows.map(row => JSON.stringify(_.pick(row, columns))).join('\r\n');
|
||||||
|
};
|
||||||
|
|
||||||
export function formatClipboardRows(columns, rows, format) {
|
const clipboardInsertsFormatter = () => (columns, rows, options) => {
|
||||||
|
const { schemaName, pureName, driver } = options;
|
||||||
|
const dmp = driver.createDumper();
|
||||||
|
for (const row of rows) {
|
||||||
|
dmp.putCmd(
|
||||||
|
'^insert ^into %f (%,i) ^values (%,v)',
|
||||||
|
{ schemaName, pureName },
|
||||||
|
columns,
|
||||||
|
columns.map(col => row[col])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return dmp.s;
|
||||||
|
};
|
||||||
|
|
||||||
|
const clipboardUpdatesFormatter = () => (columns, rows, options) => {
|
||||||
|
const { schemaName, pureName, driver, keyColumns } = options;
|
||||||
|
const dmp = driver.createDumper();
|
||||||
|
for (const row of rows) {
|
||||||
|
dmp.put('^update %f ^set ', { schemaName, pureName });
|
||||||
|
dmp.putCollection(', ', columns, col => dmp.put('%i=%v', col, row[col]));
|
||||||
|
dmp.put(' ^where ');
|
||||||
|
dmp.putCollection(' ^and ', keyColumns, col => dmp.put('%i=%v', col, row[col]));
|
||||||
|
dmp.endCommand();
|
||||||
|
}
|
||||||
|
return dmp.s;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function formatClipboardRows(format, columns, rows, options) {
|
||||||
if (format in copyRowsFormatDefs) {
|
if (format in copyRowsFormatDefs) {
|
||||||
return copyRowsFormatDefs[format].formatter(columns, rows);
|
return copyRowsFormatDefs[format].formatter(columns, rows, options);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function copyRowsToClipboard(columns, rows, format) {
|
export function copyRowsToClipboard(format, columns, rows, options) {
|
||||||
const formatted = formatClipboardRows(columns, rows, format);
|
const formatted = formatClipboardRows(format, columns, rows, options);
|
||||||
copyTextToClipboard(formatted);
|
copyTextToClipboard(formatted);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,4 +160,19 @@ export const copyRowsFormatDefs = {
|
|||||||
name: 'JSON',
|
name: 'JSON',
|
||||||
formatter: clipboardJsonFormatter(),
|
formatter: clipboardJsonFormatter(),
|
||||||
},
|
},
|
||||||
|
jsonLines: {
|
||||||
|
label: 'Copy as JSON lines',
|
||||||
|
name: 'JSON lines',
|
||||||
|
formatter: clipboardJsonLinesFormatter(),
|
||||||
|
},
|
||||||
|
inserts: {
|
||||||
|
label: 'Copy as SQL INSERTs',
|
||||||
|
name: 'SQL INSERTs',
|
||||||
|
formatter: clipboardInsertsFormatter(),
|
||||||
|
},
|
||||||
|
updates: {
|
||||||
|
label: 'Copy as SQL UPDATEs',
|
||||||
|
name: 'SQL UPDATEs',
|
||||||
|
formatter: clipboardUpdatesFormatter(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user