Skip to content

Commit 247a570

Browse files
authored
fix: insert SHOW CREATE TABLE resp to editor (#3121)
1 parent fca451d commit 247a570

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

src/containers/Tenant/ObjectSummary/SchemaTree/SchemaTree.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import {
1313
import {useClusterBaseInfo} from '../../../../store/reducers/cluster/cluster';
1414
import {selectIsDirty, selectUserInput} from '../../../../store/reducers/query/query';
1515
import {schemaApi} from '../../../../store/reducers/schema/schema';
16+
import {showCreateTableApi} from '../../../../store/reducers/showCreateTable/showCreateTable';
1617
import {streamingQueriesApi} from '../../../../store/reducers/streamingQuery/streamingQuery';
1718
import {tableSchemaDataApi} from '../../../../store/reducers/tableSchemaData';
1819
import {useTenantBaseInfo} from '../../../../store/reducers/tenant/tenant';
1920
import type {EPathType, TEvDescribeSchemeResult} from '../../../../types/api/schema';
2021
import {valueIsDefined} from '../../../../utils';
22+
import {getStringifiedData} from '../../../../utils/dataFormatters/dataFormatters';
2123
import {useTypedDispatch, useTypedSelector} from '../../../../utils/hooks';
2224
import {getConfirmation} from '../../../../utils/hooks/withConfirmation/useChangeInputWithConfirmation';
2325
import {canShowTenantMonitoringTab} from '../../../../utils/monitoringVisibility';
@@ -28,12 +30,13 @@ import {
2830
mapPathTypeToNavigationTreeType,
2931
nodeStreamingQueryTypeToPathType,
3032
nodeTableTypeToPathType,
33+
tableTypeToPathType,
3134
} from '../../utils/schema';
3235
import {getActions} from '../../utils/schemaActions';
3336
import type {DropdownItem, TreeNodeMeta} from '../../utils/types';
3437
import {CreateDirectoryDialog} from '../CreateDirectoryDialog/CreateDirectoryDialog';
3538
import {useDispatchTreeKey, useTreeKey} from '../UpdateTreeContext';
36-
import {isDomain} from '../transformPath';
39+
import {isDomain, transformPath} from '../transformPath';
3740

3841
interface SchemaTreeProps {
3942
rootName: string;
@@ -58,6 +61,10 @@ export function SchemaTree(props: SchemaTreeProps) {
5861
getStreamingQueryInfo,
5962
{currentData: streamingSysData, isFetching: isStreamingInfoFetching},
6063
] = streamingQueriesApi.useLazyGetStreamingQueryInfoQuery();
64+
const [
65+
getShowCreateTable,
66+
{currentData: showCreateTableData, isFetching: isShowCreateTableFetching},
67+
] = showCreateTableApi.useLazyGetShowCreateTableQuery();
6168

6269
const isTopicPreviewAvailable = useTopicDataAvailable();
6370

@@ -162,6 +169,8 @@ export function SchemaTree(props: SchemaTreeProps) {
162169
isSchemaDataLoading: isActionsDataFetching,
163170
hasMonitoring,
164171
streamingQueryData: streamingSysData,
172+
showCreateTableData: getStringifiedData(showCreateTableData),
173+
isShowCreateTableLoading: isShowCreateTableFetching,
165174
isStreamingQueryTextLoading: isStreamingInfoFetching,
166175
},
167176
databaseFullPath,
@@ -181,6 +190,9 @@ export function SchemaTree(props: SchemaTreeProps) {
181190
databaseFullPath,
182191
controlPlane,
183192
clusterMonitoring,
193+
showCreateTableData,
194+
isShowCreateTableFetching,
195+
handleTenantPageChange,
184196
]);
185197

186198
return (
@@ -208,6 +220,12 @@ export function SchemaTree(props: SchemaTreeProps) {
208220
if (isOpen && pathType) {
209221
getTableSchemaDataQuery({path, database, type: pathType, databaseFullPath});
210222
}
223+
const tableType = tableTypeToPathType[type];
224+
225+
if (isOpen && tableType) {
226+
const relativePath = transformPath(path, databaseFullPath);
227+
getShowCreateTable({path: relativePath, database});
228+
}
211229

212230
const streamingPathType = nodeStreamingQueryTypeToPathType[type];
213231
if (isOpen && streamingPathType) {

src/containers/Tenant/utils/schema.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export const nodeTableTypeToPathType: Partial<Record<NavigationTreeNodeType, EPa
5757
view: EPathType.EPathTypeView,
5858
};
5959

60+
export const tableTypeToPathType: Partial<Record<NavigationTreeNodeType, EPathType>> = {
61+
table: EPathType.EPathTypeTable,
62+
column_table: EPathType.EPathTypeColumnTable,
63+
};
64+
6065
export const nodeStreamingQueryTypeToPathType: Partial<Record<NavigationTreeNodeType, EPathType>> =
6166
{
6267
streaming_query: EPathType.EPathTypeStreamingQuery,

src/containers/Tenant/utils/schemaActions.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ interface ActionsAdditionalParams {
6262
isSchemaDataLoading?: boolean;
6363
hasMonitoring?: boolean;
6464
streamingQueryData?: IQueryResult;
65+
showCreateTableData?: string;
6566
isStreamingQueryTextLoading?: boolean;
67+
isShowCreateTableLoading?: boolean;
6668
}
6769

6870
interface BindActionParams {
@@ -86,6 +88,7 @@ const bindActions = (
8688
getConnectToDBDialog,
8789
schemaData,
8890
streamingQueryData,
91+
showCreateTableData,
8992
} = additionalEffects;
9093

9194
const inputQuery = (tmpl: TemplateFn) => () => {
@@ -94,7 +97,9 @@ const bindActions = (
9497
setTenantPage(TENANT_PAGES_IDS.query);
9598
dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery));
9699
setActivePath(params.path);
97-
insertSnippetToEditor(tmpl({...params, schemaData, streamingQueryData}));
100+
insertSnippetToEditor(
101+
tmpl({...params, schemaData, streamingQueryData, showCreateTableData}),
102+
);
98103
};
99104
if (getConfirmation) {
100105
const confirmedPromise = getConfirmation();
@@ -174,9 +179,10 @@ interface ActionConfig {
174179
text: string;
175180
action: () => void;
176181
isLoading?: boolean;
182+
iconStart?: React.ReactNode;
177183
}
178184

179-
const getActionWithLoader = ({text, action, isLoading}: ActionConfig) => ({
185+
const getActionWithLoader = ({text, action, isLoading, iconStart}: ActionConfig) => ({
180186
text: (
181187
<Flex justifyContent="space-between" alignItems="center">
182188
{text}
@@ -185,6 +191,7 @@ const getActionWithLoader = ({text, action, isLoading}: ActionConfig) => ({
185191
),
186192
action,
187193
disabled: isLoading,
194+
iconStart,
188195
});
189196

190197
export const getActions =
@@ -269,11 +276,12 @@ export const getActions =
269276
DIR_SET.splice(1, 0, [createDirectoryItem]);
270277
}
271278

272-
const showCreateTableItem = {
279+
const showCreateTableItem = getActionWithLoader({
273280
text: i18n('actions.showCreateTable'),
274281
action: actions.showCreateTable,
282+
isLoading: additionalEffects.isShowCreateTableLoading,
275283
iconStart: <Code />,
276-
};
284+
});
277285

278286
const ROW_TABLE_SET: ActionsSet = [
279287
[copyItem],

src/containers/Tenant/utils/schemaQueryTemplates.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface SchemaQueryParams {
1111
relativePath: string;
1212
schemaData?: SchemaData[];
1313
streamingQueryData?: IQueryResult;
14+
showCreateTableData?: string;
1415
}
1516

1617
export type TemplateFn = (params?: SchemaQueryParams) => string;
@@ -161,6 +162,9 @@ ${filters}LIMIT \${5:10};`;
161162
};
162163

163164
export const showCreateTableTemplate = (params?: SchemaQueryParams) => {
165+
if (params?.showCreateTableData) {
166+
return params.showCreateTableData;
167+
}
164168
const tablePath = params?.relativePath
165169
? `\`${normalizeParameter(params.relativePath)}\``
166170
: '${2:<my_table>}';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {QUERY_TECHNICAL_MARK} from '../../../utils/constants';
2+
import {isQueryErrorResponse, parseQueryAPIResponse} from '../../../utils/query';
3+
import {api} from '../api';
4+
5+
function getShowCreateTableSQL(path: string) {
6+
const safePath = path.replace(/`/g, '``');
7+
return `${QUERY_TECHNICAL_MARK}
8+
SHOW CREATE TABLE \`${safePath}\``;
9+
}
10+
11+
export const showCreateTableApi = api.injectEndpoints({
12+
endpoints: (build) => ({
13+
getShowCreateTable: build.query({
14+
queryFn: async ({database, path}: {database: string; path: string}, {signal}) => {
15+
try {
16+
const response = await window.api.viewer.sendQuery(
17+
{
18+
query: getShowCreateTableSQL(path),
19+
database,
20+
action: 'execute-query',
21+
},
22+
{signal, withRetries: true},
23+
);
24+
25+
if (isQueryErrorResponse(response)) {
26+
return {error: response};
27+
}
28+
29+
const data = parseQueryAPIResponse(response);
30+
31+
const result = data?.resultSets?.[0]?.result || [];
32+
33+
const showCreateTableQuery = result[0]?.CreateQuery;
34+
35+
return {data: showCreateTableQuery};
36+
} catch (error) {
37+
return {error};
38+
}
39+
},
40+
providesTags: ['All'],
41+
}),
42+
}),
43+
overrideExisting: 'throw',
44+
});

0 commit comments

Comments
 (0)