Line data Source code
1 : import type { ConnectionState } from './connection_state.js';
2 : import { isListedConnection, type ConnectionReferences, type ConnectionRegistry } from './connection_registry.js';
3 : import { getConnectorInfoForParams, HYPER_CONNECTOR } from './connector_info.js';
4 : import type { ConnectionParams } from './connection_params.js';
5 :
6 1 : export function isHyperWasmConnection(database: ConnectionState): boolean {
7 1 : return database.details.type === HYPER_CONNECTOR &&
8 1 : (database.details.value.proto.setupParams?.protocol ?? 'WASM') === 'WASM';
9 1 : }
10 :
11 : /// Does a connection offer an operational overview?
12 1 : export function supportsConnectionOverview(database: ConnectionState): boolean {
13 1 : return database.details.type === HYPER_CONNECTOR;
14 1 : }
15 :
16 : /// The label of a connection, its name and the protocol it speaks
17 : export interface ConnectionLabelParts {
18 : /// The name of the connection, e.g. "Hyper", "Hyper #2" or the name the user gave it
19 : name: string;
20 : /// The name of the connector, e.g. "Hyper"
21 : connector: string;
22 : /// The protocol, e.g. "Wasm" or "Http"
23 : detail: string;
24 : }
25 :
26 : /// The protocol of a connection as the label shows it
27 1 : function protocolLabel(protocol: string | undefined): string {
28 1 : return protocol === 'V3_GRPC' ? 'gRPC' : protocol === 'V3_HTTP' ? 'Http' : protocol === 'V3_HTTP_LINK' ? 'Link' : 'Wasm';
29 1 : }
30 :
31 : /// The parts of the label of a connection.
32 : /// A connection is called by the name the user gave it, otherwise after its connector. The ordinal
33 : /// tells it apart from other connections of the same connector, see `connectionOrdinals`.
34 1 : export function connectionLabelParts(database: ConnectionState, ordinal: number | null = null): ConnectionLabelParts {
35 1 : let detail = 'Http';
36 1 : if (database.details.type === HYPER_CONNECTOR) {
37 1 : detail = protocolLabel(database.details.value.proto.setupParams?.protocol ?? 'WASM');
38 : }
39 1 : const connector = database.connectorInfo.names.displayLong;
40 1 : return { name: database.name ?? (ordinal == null ? connector : `${connector} #${ordinal}`), connector, detail };
41 1 : }
42 :
43 : /// The label of a connection as plain text, e.g. "Hyper #2 Wasm"
44 1 : export function connectionLabel(database: ConnectionState, ordinal: number | null = null): string {
45 1 : return connectionLabelText(connectionLabelParts(database, ordinal));
46 1 : }
47 :
48 : /// The plain text of the parts of a label
49 1 : export function connectionLabelText(parts: ConnectionLabelParts): string {
50 1 : return `${parts.name} ${parts.detail}`;
51 1 : }
52 :
53 : /// The ordinals of the connections that share their connector with other connections, 1-based in
54 : /// registration order. Only listed connections are numbered, see `isListedConnection`, so the
55 : /// numbers don't depend on the current session.
56 1 : export function connectionOrdinals(
57 1 : registry: Pick<ConnectionRegistry, 'connections' | 'connectionsByType'>,
58 1 : references: ConnectionReferences,
59 1 : ): Map<string, number> {
60 1 : const ordinals = new Map<string, number>();
61 1 : for (const connectionIds of registry.connectionsByType) {
62 1 : const listed = connectionIds.filter(connectionId => isListedConnection(registry, references, connectionId));
63 1 : if (listed.length < 2) continue;
64 1 : listed.forEach((connectionId, index) => ordinals.set(connectionId, index + 1));
65 1 : }
66 1 : return ordinals;
67 1 : }
68 :
69 : /// A label for connection params, e.g. when the connection state is not restored.
70 : /// It names the connector and the location of the database that the params describe.
71 1 : export function connectionParamsLabel(params: ConnectionParams): string {
72 1 : const name = getConnectorInfoForParams(params)?.names.displayLong ?? 'Connection';
73 1 : if ('hyper' in params) {
74 1 : const protocol = params.hyper.protocol ?? 'WASM';
75 1 : return protocol === 'WASM' ? `${name} Wasm` : `${name} / ${params.hyper.endpoint ?? ''}`;
76 : }
77 1 : if ('trino' in params) return `${name} / ${params.trino.endpoint ?? ''}`;
78 1 : if ('salesforce' in params) return `${name} / ${params.salesforce.instanceUrl ?? ''}`;
79 1 : if ('sidb' in params) return `${name} / ${params.sidb.instanceUrl ?? ''}`;
80 1 : return name;
81 1 : }
|