Skip to content

Commit

Permalink
Copy column names properly (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncombe authored Dec 3, 2024
1 parent 8c38029 commit 5cfdf1a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add a refresh button to the SQL console schema tree.
- Update text copied to clipboard when clicking a SQL column name.

## 2024-11-29 - 0.19.2

Expand Down
24 changes: 24 additions & 0 deletions src/components/SQLEditor/SQLEditorSchemaTree.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@ describe('The SQLEditorSchemaTree component', () => {
const clipboardText = await navigator.clipboard.readText();
expect(clipboardText).toBe(column.column_name);
});

it('clicking on subcolumn names copies the name', async () => {
const { user } = await setup();

const schema = schemaTableColumns[4];
const table = schema.tables[2];
const column = table.columns[0];
const subColumn = column.children![0];

// open schema tree
await triggerTreeItem(user, schema.schema_name, table.table_name);
// open table tree
await triggerTreeItem(user, table.table_name, column.column_name);
await triggerTreeItem(user, column.column_name, subColumn.column_name);

// click on name
await user.click(screen.getByText(subColumn.column_name));

// should have been copied
const clipboardText = await navigator.clipboard.readText();
expect(clipboardText).toBe(
`${column.column_name}['${subColumn.column_name}']`,
);
});
});

describe('filtering via the search input', () => {
Expand Down
17 changes: 13 additions & 4 deletions src/components/SQLEditor/SQLEditorSchemaTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,20 @@ function SQLEditorSchemaTree() {

const drawTreeData = (filteredSchemaTree: Schema[]): AntDesignTreeItem[] => {
const drawColumns = (columns: SchemaTableColumn[]): AntDesignTreeItem[] => {
const quoteColumnName = (path: string[]) =>
path[2] +
path
.slice(3)
.map((identifier: string) => `['${identifier}']`)
.join('');

return columns.map(column => ({
title: (
<span data-testid={column.path.join('.')}>
<CopyToClipboard textToCopy={column.column_name}>
{column.column_name}
<CopyToClipboard
textToCopy={quoteColumnName(column.path as string[]) as string}
>
{column.unquoted_column_name}
</CopyToClipboard>
<Text pale className="ml-1 inline text-xs italic !leading-3">
{column.data_type}
Expand Down Expand Up @@ -177,7 +186,7 @@ function SQLEditorSchemaTree() {
data-testid={`schema-${schema.schema_name}`}
>
<ApartmentOutlined className="mr-1.5 size-3 opacity-50" />
{schema.schema_name}
{schema.unquoted_schema_name}
{schema.tables.find(table => table.is_system_table) && (
<Text className="ml-1 inline text-xs italic !leading-3" pale>
system
Expand Down Expand Up @@ -284,7 +293,7 @@ function SQLEditorSchemaTree() {
<span className="flex items-center" data-testid={table.path.join('.')}>
{drawTableIcon(table.table_type)}
<CopyToClipboard textToCopy={table.path.join('.')}>
{table.table_name}
{table.unquoted_table_name}
</CopyToClipboard>
<Text pale className="ml-1 inline text-xs italic !leading-3">
{drawTableDescription(table.table_type)}
Expand Down
36 changes: 23 additions & 13 deletions src/swr/jwt/useSchemaTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type SchemaTableColumn = {
column_name: string;
data_type: string;
path: string[];
unquoted_column_name: string;
};

type SchemaTableType = 'BASE TABLE' | 'VIEW' | 'FOREIGN';
Expand All @@ -28,12 +29,14 @@ export type SchemaTable = {
path: string[];
columns: SchemaTableColumn[];
is_system_table: boolean;
unquoted_table_name: string;
};

export type Schema = {
schema_name: string;
path: string[];
tables: SchemaTable[];
unquoted_schema_name: string;
};

export const postFetch = (data: QueryResultSuccess): Schema[] => {
Expand Down Expand Up @@ -100,6 +103,7 @@ export const postFetch = (data: QueryResultSuccess): Schema[] => {
column_name: node.name,
data_type: node.data_type,
path: path,
unquoted_column_name: node.name.replace(/^"|"$/g, ''),
};
if (childNodes) {
output['children'] = childNodes;
Expand All @@ -112,18 +116,21 @@ export const postFetch = (data: QueryResultSuccess): Schema[] => {
};

const constructTables = (input: SchemaDescription[]): SchemaTable[] => {
return input.map(row => {
const path = [row.table_schema, row.table_name];

return {
columns: constructColumns(row, path),
is_system_table: SYSTEM_SCHEMAS.includes(row.table_schema),
path: path,
schema_name: row.table_schema,
table_name: row.table_name,
table_type: row.table_type as SchemaTableType,
};
});
return input
.map(row => {
const path = [row.table_schema, row.table_name];

return {
columns: constructColumns(row, path),
is_system_table: SYSTEM_SCHEMAS.includes(row.table_schema),
path: path,
schema_name: row.table_schema,
table_name: row.table_name,
table_type: row.table_type as SchemaTableType,
unquoted_table_name: row.table_name.replace(/^"|"$/g, ''),
};
})
.sort((a, b) => a.unquoted_table_name.localeCompare(b.unquoted_table_name));
};

const constructSchemas = (input: SchemaDescription[]): Schema[] => {
Expand All @@ -136,10 +143,13 @@ export const postFetch = (data: QueryResultSuccess): Schema[] => {
schema_name: schemaName,
path: [schemaName],
tables: constructTables(input.filter(i => i.table_schema === schemaName)),
unquoted_schema_name: schemaName.replace(/^"|"$/g, ''),
});
});

return tree;
return tree.sort((a, b) =>
a.unquoted_schema_name.localeCompare(b.unquoted_schema_name),
);
};

return constructSchemas(input);
Expand Down

0 comments on commit 5cfdf1a

Please sign in to comment.