-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathsql.tsx
More file actions
109 lines (97 loc) · 4.17 KB
/
Copy pathsql.tsx
File metadata and controls
109 lines (97 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright 2026 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
import { create } from '@bufbuild/protobuf';
import { createConnectQueryKey, useMutation, useQuery } from '@connectrpc/connect-query';
import { useQueryClient } from '@tanstack/react-query';
import { GetTopicConfigurationsRequestSchema } from 'protogen/redpanda/api/dataplane/v1/topic_pb';
import { getTopicConfigurations } from 'protogen/redpanda/api/dataplane/v1/topic-TopicService_connectquery';
import {
type DescribeTableRequest,
DescribeTableRequestSchema,
GetSqlIdentityRequestSchema,
type ListCatalogsRequest,
ListCatalogsRequestSchema,
type ListTablesRequest,
ListTablesRequestSchema,
} from 'protogen/redpanda/api/dataplane/v1alpha3/sql_pb';
import {
describeTable,
executeQuery,
getSqlIdentity,
listCatalogs,
listTables,
} from 'protogen/redpanda/api/dataplane/v1alpha3/sql-SQLService_connectquery';
import { MAX_PAGE_SIZE, type MessageInit } from 'react-query/react-query.utils';
type SqlQueryOptions = {
enabled?: boolean;
};
export const useListCatalogsQuery = (input?: MessageInit<ListCatalogsRequest>, options?: SqlQueryOptions) => {
const request = create(ListCatalogsRequestSchema, {
pageSize: input?.pageSize ?? MAX_PAGE_SIZE,
pageToken: input?.pageToken ?? '',
});
return useQuery(listCatalogs, request, {
enabled: options?.enabled !== false,
});
};
// Resolves the caller's SQL identity (engine username + admin/superuser flag).
// Admin gates write/DDL affordances like the "Add a topic" button.
export const useGetSqlIdentityQuery = (options?: SqlQueryOptions) => {
const request = create(GetSqlIdentityRequestSchema, {});
return useQuery(getSqlIdentity, request, {
enabled: options?.enabled !== false,
});
};
export const useListTablesQuery = (input?: MessageInit<ListTablesRequest>, options?: SqlQueryOptions) => {
const request = create(ListTablesRequestSchema, {
catalog: input?.catalog ?? '',
pageSize: input?.pageSize ?? MAX_PAGE_SIZE,
pageToken: input?.pageToken ?? '',
filter: input?.filter,
});
return useQuery(listTables, request, {
enabled: options?.enabled !== false && Boolean(input?.catalog),
});
};
export const useDescribeTableQuery = (input?: MessageInit<DescribeTableRequest>, options?: SqlQueryOptions) => {
const request = create(DescribeTableRequestSchema, {
catalog: input?.catalog ?? '',
name: input?.name ?? '',
});
return useQuery(describeTable, request, {
enabled: options?.enabled !== false && Boolean(input?.catalog) && Boolean(input?.name),
});
};
// RP SQL's SHOW TABLES has no per-table Iceberg flag; the authoritative signal
// is the backing Kafka topic's `redpanda.iceberg.mode` config. Returns whether
// the topic is Iceberg-tiered so the catalog tree can show the label.
export const useTopicIcebergQuery = (topicName: string, options?: SqlQueryOptions) => {
const request = create(GetTopicConfigurationsRequestSchema, { topicName });
const result = useQuery(getTopicConfigurations, request, {
enabled: options?.enabled !== false && Boolean(topicName),
});
const mode = result.data?.configurations.find((c) => c.name === 'redpanda.iceberg.mode')?.value;
return { ...result, isIceberg: Boolean(mode && mode !== 'disabled') };
};
// Errors surface inline (run panel / wizard), so no toast on failure.
export const useExecuteQueryMutation = () => useMutation(executeQuery);
// Returns a function that refreshes the catalog/table listings, e.g. after a
// CREATE TABLE so the new table shows up in the tree.
export const useInvalidateSqlCatalog = () => {
const queryClient = useQueryClient();
return () =>
Promise.all([
queryClient.invalidateQueries({
queryKey: createConnectQueryKey({ schema: listCatalogs, cardinality: 'finite' }),
}),
queryClient.invalidateQueries({ queryKey: createConnectQueryKey({ schema: listTables, cardinality: 'finite' }) }),
]);
};