Skip to content

Commit

Permalink
feat: Copy as sql insert (#165)
Browse files Browse the repository at this point in the history
* feat: add data type

* feat: add more refactor

* rework how we represent the data

* more refactoring

* fixing all type error

* fixing lint

* feat: add copy as sql insert
  • Loading branch information
invisal authored Oct 9, 2023
1 parent cf26feb commit 131dae3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function QueryResultTable({
>();
const [newRowCount, setNewRowCount] = useState(0);
const { collector, cellManager } = useEditableResult();
const { schema, currentDatabase } = useSchema();
const { schema, currentDatabase, dialect } = useSchema();

const [selectedRowsIndex, setSelectedRowsIndex] = useState<number[]>([]);
const [removeRowsIndex, setRemoveRowsIndex] = useState<number[]>([]);
Expand Down Expand Up @@ -112,6 +112,7 @@ function QueryResultTable({
headers,
rules,
setSelectedRowsIndex,
dialect,
});

const headerMemo = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getDisplayableFromDatabaseRows } from '../../../../libs/TransformResult
import BaseType from 'renderer/datatype/BaseType';
import SQLCommonInterface from 'drivers/base/SQLCommonInterface';
import { useSqlExecute } from 'renderer/contexts/SqlExecuteProvider';
import { QueryDialetType, qb } from 'libs/QueryBuilder';

interface DataTableContextMenuDeps {
collector: ResultChangeCollector;
Expand All @@ -19,6 +20,7 @@ interface DataTableContextMenuDeps {
headers: QueryResultHeader[];
rules: TableEditableRule;
setSelectedRowsIndex: React.Dispatch<React.SetStateAction<number[]>>;
dialect: QueryDialetType;
}

function dataToArray(
Expand Down Expand Up @@ -52,6 +54,7 @@ export default function useDataTableContextMenu({
headers,
rules,
setSelectedRowsIndex,
dialect,
}: DataTableContextMenuDeps) {
const { common } = useSqlExecute();

Expand Down Expand Up @@ -107,6 +110,35 @@ export default function useDataTableContextMenu({
window.navigator.clipboard.writeText(markdownString.join('\n'));
}

function onCopyInsertSQL() {
const selectedRows = selectedRowsIndex.map(
(rowIndex) => data[rowIndex].data,
);

const tableList = Array.from(
new Set(headers.map((header) => header.schema?.table).filter(Boolean)),
);

const tableName =
(tableList.length === 1 ? tableList[0] : 'Unknown') ?? 'Unknown';

const lines = selectedRows
.map((row) => {
return qb(dialect)
.table(tableName)
.insert(
Object.keys(row).reduce<Record<string, unknown>>((a, b) => {
a[b] = row[b].toSQL(dialect);
return a;
}, {}),
)
.toRawSQL();
})
.join(';\n');

window.navigator.clipboard.writeText(lines);
}

const lastSelectedRow =
selectedRowsIndex.length > 0
? data[selectedRowsIndex[selectedRowsIndex.length - 1]]
Expand All @@ -127,7 +159,7 @@ export default function useDataTableContextMenu({
{ text: 'As CSV', disabled: true },
{ text: 'As JSON', onClick: onCopyAsJson },
{ text: 'As Markdown', onClick: onCopyAsMarkdown },
{ text: 'As SQL', disabled: true },
{ text: 'As SQL Insert', onClick: onCopyInsertSQL },
],
},
{
Expand Down Expand Up @@ -215,5 +247,6 @@ export default function useDataTableContextMenu({
data,
rules,
common,
dialect,
]);
}

0 comments on commit 131dae3

Please sign in to comment.