♻️ refactor(connection): 统一连接驱动类型归一化

This commit is contained in:
Syngnat
2026-06-12 06:58:41 +08:00
parent f956991bda
commit e353fda7a2
5 changed files with 104 additions and 82 deletions

View File

@@ -79,6 +79,11 @@ import {
supportsSSLClientCertificateForType,
supportsSSLForType,
} from "../utils/connectionTypeCapabilities";
import {
normalizeDriverType,
resolveConnectionDriverType,
type DriverStatusSnapshot,
} from "../utils/connectionDriverType";
import { CUSTOM_CONNECTION_DRIVER_HELP } from "../utils/driverImportGuidance";
import {
describeUnsupportedOceanBaseProtocol,
@@ -246,48 +251,6 @@ const resolveInitialSecretFieldValue = (
}
};
type DriverStatusSnapshot = {
type: string;
name: string;
connectable: boolean;
expectedRevision?: string;
needsUpdate?: boolean;
updateReason?: string;
affectedConnections?: number;
message?: string;
};
const normalizeDriverType = (value: string): string => {
const normalized = String(value || "")
.trim()
.toLowerCase();
if (normalized === "postgresql") return "postgres";
if (normalized === "elastic") return "elasticsearch";
if (normalized === "doris") return "diros";
if (
normalized === "intersystems" ||
normalized === "intersystemsiris" ||
normalized === "inter-systems-iris" ||
normalized === "inter-systems"
)
return "iris";
if (
normalized === "open_gauss" ||
normalized === "open-gauss" ||
normalized === "opengauss"
)
return "opengauss";
return normalized;
};
const resolveConnectionDriverType = (type: string, driver?: string): string => {
const normalizedType = normalizeDriverType(type);
if (normalizedType !== "custom") {
return normalizedType;
}
return normalizeDriverType(driver || "");
};
const ConnectionModal: React.FC<{
open: boolean;
onClose: () => void;

View File

@@ -90,6 +90,7 @@ import { buildTableSelectQuery } from '../utils/objectQueryTemplates';
import { getShortcutPlatform, resolveShortcutDisplay } from '../utils/shortcuts';
import { buildExternalSQLDirectoryId, buildExternalSQLRootNode, buildExternalSQLTabId, type ExternalSQLTreeNode } from '../utils/externalSqlTree';
import { SIDEBAR_SQL_EDITOR_DRAG_MIME, encodeSidebarSqlEditorDragPayload } from '../utils/sidebarSqlDrag';
import type { DriverStatusSnapshot } from '../utils/connectionDriverType';
import JVMModeBadge from './jvm/JVMModeBadge';
import {
SEARCH_SCOPE_LABEL_MAP,
@@ -237,16 +238,6 @@ interface BatchObjectItem {
dataRef: any;
}
type DriverStatusSnapshot = {
type: string;
name: string;
connectable: boolean;
expectedRevision?: string;
needsUpdate?: boolean;
updateReason?: string;
message?: string;
};
const DRIVER_STATUS_CACHE_TTL_MS = 30_000;
const SEARCH_SCOPE_ICON_MAP: Record<SearchScope, React.ReactNode> = {

View File

@@ -1,5 +1,10 @@
import type React from 'react';
import type { SavedConnection } from '../types';
import {
isPostgresSchemaDialect as resolveIsPostgresSchemaDialect,
normalizeDriverType as normalizeConnectionDriverType,
resolveSavedConnectionDriverType as resolveSavedConnectionDriverTypeBase,
} from '../utils/connectionDriverType';
const SIDEBAR_CONTEXT_MENU_SAFE_GAP = 8;
export const SIDEBAR_CONTEXT_MENU_FALLBACK_WIDTH = 264;
@@ -79,36 +84,9 @@ export const isConnectionTreeKey = (key: React.Key, connectionId: string): boole
return text === connectionId || text.startsWith(`${connectionId}-`);
};
export const normalizeDriverType = (value: string): string => {
const normalized = String(value || '').trim().toLowerCase();
if (normalized === 'postgresql' || normalized === 'pg' || normalized === 'pq' || normalized === 'pgx') return 'postgres';
if (normalized === 'elastic') return 'elasticsearch';
if (normalized === 'doris') return 'diros';
if (
normalized === 'open_gauss' ||
normalized === 'open-gauss' ||
normalized === 'opengauss'
) return 'opengauss';
if (
normalized === 'intersystems' ||
normalized === 'intersystemsiris' ||
normalized === 'inter-systems' ||
normalized === 'inter-systems-iris'
) return 'iris';
return normalized;
};
export const resolveSavedConnectionDriverType = (conn: SavedConnection | undefined): string => {
const type = normalizeDriverType(conn?.config?.type || '');
if (type !== 'custom') {
return type;
}
return normalizeDriverType(conn?.config?.driver || '');
};
export const isPostgresSchemaDialect = (dialect: string): boolean => (
['postgres', 'kingbase', 'highgo', 'vastbase', 'opengauss'].includes(normalizeDriverType(dialect))
);
export const normalizeDriverType = normalizeConnectionDriverType;
export const resolveSavedConnectionDriverType = resolveSavedConnectionDriverTypeBase;
export const isPostgresSchemaDialect = resolveIsPostgresSchemaDialect;
export const SEARCH_SCOPE_OPTIONS: Array<{ value: SearchScope; label: string }> = [
{ value: 'smart', label: '智能' },

View File

@@ -0,0 +1,43 @@
import { describe, expect, it } from 'vitest';
import {
isPostgresSchemaDialect,
normalizeDriverType,
resolveConnectionDriverType,
resolveSavedConnectionDriverType,
} from './connectionDriverType';
describe('connectionDriverType', () => {
it('normalizes built-in driver aliases shared by connection modal and sidebar', () => {
expect(normalizeDriverType('postgresql')).toBe('postgres');
expect(normalizeDriverType('pgx')).toBe('postgres');
expect(normalizeDriverType('elastic')).toBe('elasticsearch');
expect(normalizeDriverType('doris')).toBe('diros');
expect(normalizeDriverType('open-gauss')).toBe('opengauss');
expect(normalizeDriverType('InterSystemsIRIS')).toBe('iris');
});
it('resolves custom connection driver types from the selected driver field', () => {
expect(resolveConnectionDriverType('mysql', 'postgresql')).toBe('mysql');
expect(resolveConnectionDriverType('custom', 'postgresql')).toBe('postgres');
expect(resolveConnectionDriverType('custom', 'open_gauss')).toBe('opengauss');
expect(resolveConnectionDriverType('custom', '')).toBe('');
});
it('resolves saved custom connections using the same driver aliases', () => {
const conn = {
config: {
type: 'custom',
driver: 'pg',
},
} as any;
expect(resolveSavedConnectionDriverType(conn)).toBe('postgres');
});
it('detects postgres-compatible schema dialects', () => {
expect(isPostgresSchemaDialect('postgres')).toBe(true);
expect(isPostgresSchemaDialect('kingbase')).toBe(true);
expect(isPostgresSchemaDialect('open-gauss')).toBe(true);
expect(isPostgresSchemaDialect('mysql')).toBe(false);
});
});

View File

@@ -0,0 +1,47 @@
import type { SavedConnection } from '../types';
export type DriverStatusSnapshot = {
type: string;
name: string;
connectable: boolean;
expectedRevision?: string;
needsUpdate?: boolean;
updateReason?: string;
affectedConnections?: number;
message?: string;
};
export const normalizeDriverType = (value: string): string => {
const normalized = String(value || '').trim().toLowerCase();
if (normalized === 'postgresql' || normalized === 'pg' || normalized === 'pq' || normalized === 'pgx') return 'postgres';
if (normalized === 'elastic') return 'elasticsearch';
if (normalized === 'doris') return 'diros';
if (
normalized === 'open_gauss' ||
normalized === 'open-gauss' ||
normalized === 'opengauss'
) return 'opengauss';
if (
normalized === 'intersystems' ||
normalized === 'intersystemsiris' ||
normalized === 'inter-systems' ||
normalized === 'inter-systems-iris'
) return 'iris';
return normalized;
};
export const resolveConnectionDriverType = (type: string, driver?: string): string => {
const normalizedType = normalizeDriverType(type);
if (normalizedType !== 'custom') {
return normalizedType;
}
return normalizeDriverType(driver || '');
};
export const resolveSavedConnectionDriverType = (conn: SavedConnection | undefined): string => {
return resolveConnectionDriverType(conn?.config?.type || '', conn?.config?.driver || '');
};
export const isPostgresSchemaDialect = (dialect: string): boolean => (
['postgres', 'kingbase', 'highgo', 'vastbase', 'opengauss'].includes(normalizeDriverType(dialect))
);