fix(completion): resolve ob-oracle schema for alias column completion
This commit is contained in:
5 files changed
+112
-8
No files matched your search
+61
@@ -398,6 +398,28 @@ class OceanBaseOracleAgentTest {
|
|||||||
Assertions.assertTrue(sql.get(0).contains("c.DATA_DEFAULT"), sql.get(0));
|
Assertions.assertTrue(sql.get(0).contains("c.DATA_DEFAULT"), sql.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getColumnsResolvesTheCurrentSchemaWhenSchemaIsEmpty() {
|
||||||
|
List<String> sql = new ArrayList<>();
|
||||||
|
List<String> params = new ArrayList<>();
|
||||||
|
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||||
|
TestSupport.setPrivateConnection(agent, currentSchemaColumnConnection(
|
||||||
|
sql,
|
||||||
|
params,
|
||||||
|
resultSet(new String[]{"CURRENT_SCHEMA"}, new Object[][]{{"APP"}}),
|
||||||
|
columnResultSet(new Object[][]{
|
||||||
|
{"PARAM_VALUE", "VARCHAR2", "Y", null, null, 100, 100, null, null, 0}
|
||||||
|
})
|
||||||
|
));
|
||||||
|
|
||||||
|
List<ColumnInfo> columns = agent.getColumns("", "TBPARAM");
|
||||||
|
|
||||||
|
Assertions.assertEquals(List.of("PARAM_VALUE"), columns.stream().map(ColumnInfo::getName).toList());
|
||||||
|
Assertions.assertEquals(List.of("APP", "TBPARAM", "APP", "TBPARAM"), params);
|
||||||
|
Assertions.assertTrue(sql.get(0).contains("SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')"), sql.get(0));
|
||||||
|
Assertions.assertTrue(sql.get(1).contains("FROM ALL_TAB_COLUMNS"), sql.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void tableDdlIncludesDefaultsAndOnlyNonBlankColumnComments() {
|
void tableDdlIncludesDefaultsAndOnlyNonBlankColumnComments() {
|
||||||
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
OceanBaseOracleAgent agent = new OceanBaseOracleAgent();
|
||||||
@@ -496,6 +518,45 @@ class OceanBaseOracleAgentTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Connection currentSchemaColumnConnection(List<String> sql, List<String> params, ResultSet currentSchema, ResultSet columns) {
|
||||||
|
Statement schemaStatement = proxy(Statement.class, (method, args) -> {
|
||||||
|
if ("executeQuery".equals(method.getName())) {
|
||||||
|
sql.add(String.valueOf(args[0]));
|
||||||
|
return currentSchema;
|
||||||
|
}
|
||||||
|
if ("close".equals(method.getName())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
PreparedStatement columnStatement = proxy(PreparedStatement.class, (method, args) -> {
|
||||||
|
if ("executeQuery".equals(method.getName())) {
|
||||||
|
return columns;
|
||||||
|
}
|
||||||
|
if ("setString".equals(method.getName())) {
|
||||||
|
params.add(String.valueOf(args[1]));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ("close".equals(method.getName())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
return proxy(Connection.class, (method, args) -> {
|
||||||
|
if ("createStatement".equals(method.getName())) {
|
||||||
|
return schemaStatement;
|
||||||
|
}
|
||||||
|
if ("prepareStatement".equals(method.getName())) {
|
||||||
|
sql.add(String.valueOf(args[0]));
|
||||||
|
return columnStatement;
|
||||||
|
}
|
||||||
|
if ("isClosed".equals(method.getName())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return defaultValue(method.getReturnType());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private static Connection objectSourceConnection(List<String> sql, List<String> params, ResultSet resultSet) {
|
private static Connection objectSourceConnection(List<String> sql, List<String> params, ResultSet resultSet) {
|
||||||
PreparedStatement statement = objectSourceStatement(params, resultSet, false);
|
PreparedStatement statement = objectSourceStatement(params, resultSet, false);
|
||||||
return proxy(Connection.class, (method, args) -> {
|
return proxy(Connection.class, (method, args) -> {
|
||||||
|
|||||||
@@ -8,8 +8,11 @@ const contentAreaSource = readFileSync(new URL("../../../components/layout/Conte
|
|||||||
describe("Oracle session-scoped completion", () => {
|
describe("Oracle session-scoped completion", () => {
|
||||||
it("uses the tab session only for unqualified references without a selected schema", () => {
|
it("uses the tab session only for unqualified references without a selected schema", () => {
|
||||||
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle", clientSessionId: "tab-a" })).toBe(true);
|
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle", clientSessionId: "tab-a" })).toBe(true);
|
||||||
|
expect(usesOracleSessionCompletionColumns({ databaseType: "oceanbase-oracle", clientSessionId: "tab-a" })).toBe(true);
|
||||||
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle", clientSessionId: "tab-a", referenceSchema: "REPORTING" })).toBe(false);
|
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle", clientSessionId: "tab-a", referenceSchema: "REPORTING" })).toBe(false);
|
||||||
|
expect(usesOracleSessionCompletionColumns({ databaseType: "oceanbase-oracle", clientSessionId: "tab-a", referenceSchema: "REPORTING" })).toBe(false);
|
||||||
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle", clientSessionId: "tab-a", selectedSchema: "REPORTING" })).toBe(false);
|
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle", clientSessionId: "tab-a", selectedSchema: "REPORTING" })).toBe(false);
|
||||||
|
expect(usesOracleSessionCompletionColumns({ databaseType: "oceanbase-oracle", clientSessionId: "tab-a", selectedSchema: "REPORTING" })).toBe(false);
|
||||||
expect(usesOracleSessionCompletionColumns({ databaseType: "postgres", clientSessionId: "tab-a" })).toBe(false);
|
expect(usesOracleSessionCompletionColumns({ databaseType: "postgres", clientSessionId: "tab-a" })).toBe(false);
|
||||||
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle" })).toBe(false);
|
expect(usesOracleSessionCompletionColumns({ databaseType: "oracle" })).toBe(false);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import type { DatabaseType } from "@/types/database";
|
import type { DatabaseType } from "@/types/database";
|
||||||
|
|
||||||
|
export function usesOracleCurrentSchemaCompletion(databaseType?: DatabaseType, schema?: string | null): boolean {
|
||||||
|
return (databaseType === "oracle" || databaseType === "oceanbase-oracle") && !schema;
|
||||||
|
}
|
||||||
|
|
||||||
export function usesOracleSessionCompletionColumns(options: { databaseType?: DatabaseType; selectedSchema?: string; referenceSchema?: string | null; clientSessionId?: string }): boolean {
|
export function usesOracleSessionCompletionColumns(options: { databaseType?: DatabaseType; selectedSchema?: string; referenceSchema?: string | null; clientSessionId?: string }): boolean {
|
||||||
return options.databaseType === "oracle" && !options.selectedSchema && !options.referenceSchema && !!options.clientSessionId;
|
return usesOracleCurrentSchemaCompletion(options.databaseType, options.referenceSchema || options.selectedSchema) && !!options.clientSessionId;
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { createPinia, setActivePinia } from "pinia";
|
import { createPinia, setActivePinia } from "pinia";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { getSqlCompletionContext } from "@/lib/sql/sqlCompletion";
|
||||||
import type { ConnectionConfig } from "@/types/database";
|
import type { ConnectionConfig } from "@/types/database";
|
||||||
|
|
||||||
function installLocalStorage() {
|
function installLocalStorage() {
|
||||||
@@ -715,6 +716,40 @@ describe("connectionStore completion assistant", () => {
|
|||||||
expect(upper).toEqual(lower);
|
expect(upper).toEqual(lower);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("uses the OceanBase Oracle session schema for an unqualified aliased table", async () => {
|
||||||
|
const completionAssistantSearch = vi.fn();
|
||||||
|
const getColumns = vi.fn().mockResolvedValue([{ name: "PARAM_VALUE", data_type: "VARCHAR2(100)", is_nullable: true, column_default: null, is_primary_key: false, extra: null, comment: null }]);
|
||||||
|
|
||||||
|
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
|
||||||
|
vi.doMock("@/lib/backend/api", () => ({
|
||||||
|
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
|
||||||
|
completionAssistantSearch,
|
||||||
|
getColumns,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const sql = "select a. from tbparam a";
|
||||||
|
const completion = getSqlCompletionContext(sql, sql.indexOf("a.") + 2, { databaseType: "oceanbase-oracle" });
|
||||||
|
const reference = completion.referencedTables[0];
|
||||||
|
expect(reference).toEqual(expect.objectContaining({ name: "tbparam", alias: "a", schema: undefined, nameQuoted: false }));
|
||||||
|
|
||||||
|
const { useConnectionStore } = await import("@/stores/connectionStore");
|
||||||
|
const store = useConnectionStore();
|
||||||
|
store.connections = [oceanBaseOracleConnection()];
|
||||||
|
store.connectedIds.add("oceanbase-oracle-1");
|
||||||
|
|
||||||
|
const columns = await store.listCompletionColumns("oceanbase-oracle-1", "OBORCL", reference!.name, reference!.schema, {
|
||||||
|
clientSessionId: "tab-a",
|
||||||
|
version: 0,
|
||||||
|
tableQuoted: reference!.nameQuoted,
|
||||||
|
schemaQuoted: reference!.schemaQuoted,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(completionAssistantSearch).not.toHaveBeenCalled();
|
||||||
|
expect(getColumns).toHaveBeenCalledWith("oceanbase-oracle-1", "OBORCL", "", "TBPARAM", undefined, "tab-a");
|
||||||
|
expect(columns).toEqual([expect.objectContaining({ name: "PARAM_VALUE", table: "TBPARAM", schema: undefined, dataType: "VARCHAR2(100)" })]);
|
||||||
|
expect(store.lookupLocalCompletionColumns("oceanbase-oracle-1", "OBORCL", "TBPARAM")).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
it("keeps quoted OceanBase Oracle identifiers exact and isolated from unquoted cache entries", async () => {
|
it("keeps quoted OceanBase Oracle identifiers exact and isolated from unquoted cache entries", async () => {
|
||||||
const completionAssistantSearch = vi.fn(async (request: { parent_name?: string | null; parent_schema?: string | null }) => ({
|
const completionAssistantSearch = vi.fn(async (request: { parent_name?: string | null; parent_schema?: string | null }) => ({
|
||||||
candidates: [
|
candidates: [
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ import {
|
|||||||
type ConnectionExportProtection,
|
type ConnectionExportProtection,
|
||||||
} from "@/lib/connection/connectionConfigTransfer";
|
} from "@/lib/connection/connectionConfigTransfer";
|
||||||
import type { SqlCompletionColumn, SqlCompletionForeignKey, SqlCompletionObject, SqlCompletionTable } from "@/lib/sql/sqlCompletion";
|
import type { SqlCompletionColumn, SqlCompletionForeignKey, SqlCompletionObject, SqlCompletionTable } from "@/lib/sql/sqlCompletion";
|
||||||
|
import { usesOracleCurrentSchemaCompletion } from "@/lib/sql/oracleCompletionSession";
|
||||||
import { mergeSqlObjectNavigationType, sqlObjectNavigationTypeFromTableType } from "@/lib/sql/sqlNavigation";
|
import { mergeSqlObjectNavigationType, sqlObjectNavigationTypeFromTableType } from "@/lib/sql/sqlNavigation";
|
||||||
import * as api from "@/lib/backend/api";
|
import * as api from "@/lib/backend/api";
|
||||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||||
@@ -7807,11 +7808,11 @@ export const useConnectionStore = defineStore("connection", () => {
|
|||||||
const completionTable = uppercaseUnquotedIdentifier && context?.tableQuoted === false ? table.toUpperCase() : table;
|
const completionTable = uppercaseUnquotedIdentifier && context?.tableQuoted === false ? table.toUpperCase() : table;
|
||||||
const rawCompletionSchema = schema?.trim() || (config?.db_type === "dameng" ? config.username?.trim() || undefined : undefined);
|
const rawCompletionSchema = schema?.trim() || (config?.db_type === "dameng" ? config.username?.trim() || undefined : undefined);
|
||||||
const completionSchema = uppercaseUnquotedIdentifier && rawCompletionSchema && context?.schemaQuoted === false ? rawCompletionSchema.toUpperCase() : rawCompletionSchema;
|
const completionSchema = uppercaseUnquotedIdentifier && rawCompletionSchema && context?.schemaQuoted === false ? rawCompletionSchema.toUpperCase() : rawCompletionSchema;
|
||||||
const usesOracleCurrentSchema = config?.db_type === "oracle" && !completionSchema;
|
const usesCurrentSchema = usesOracleCurrentSchemaCompletion(config?.db_type, completionSchema);
|
||||||
if (isSchemaAwareDatabase(connectionId) && !connectionUsesDatabaseObjectTreeMode(config) && !completionSchema && !usesOracleCurrentSchema) {
|
if (isSchemaAwareDatabase(connectionId) && !connectionUsesDatabaseObjectTreeMode(config) && !completionSchema && !usesCurrentSchema) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const sessionCacheScope = usesOracleCurrentSchema && context?.clientSessionId ? `:${context.clientSessionId}:${context.version ?? 0}` : "";
|
const sessionCacheScope = usesCurrentSchema && context?.clientSessionId ? `:${context.clientSessionId}:${context.version ?? 0}` : "";
|
||||||
const cacheKey = `${completionColumnsKey(connectionId, database, completionTable, completionSchema, catalog, context)}${sessionCacheScope}`;
|
const cacheKey = `${completionColumnsKey(connectionId, database, completionTable, completionSchema, catalog, context)}${sessionCacheScope}`;
|
||||||
if (!completionColumnsCache.value[cacheKey]) {
|
if (!completionColumnsCache.value[cacheKey]) {
|
||||||
const requestRevision = completionCacheRevision(connectionId, database);
|
const requestRevision = completionCacheRevision(connectionId, database);
|
||||||
@@ -7820,7 +7821,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
|||||||
async () => {
|
async () => {
|
||||||
await ensureConnected(connectionId);
|
await ensureConnected(connectionId);
|
||||||
// Use assistant metadata opportunistically, then fall back to canonical metadata.
|
// Use assistant metadata opportunistically, then fall back to canonical metadata.
|
||||||
if (!usesOracleCurrentSchema && !catalog) {
|
if (!usesCurrentSchema && !catalog) {
|
||||||
try {
|
try {
|
||||||
const assistantColumns = await listCompletionAssistantColumns(connectionId, database, completionTable, completionSchema, context, requestRevision);
|
const assistantColumns = await listCompletionAssistantColumns(connectionId, database, completionTable, completionSchema, context, requestRevision);
|
||||||
if (assistantColumns.length > 0) {
|
if (assistantColumns.length > 0) {
|
||||||
@@ -7845,8 +7846,8 @@ export const useConnectionStore = defineStore("connection", () => {
|
|||||||
// Fall back to the existing metadata path below.
|
// Fall back to the existing metadata path below.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const querySchema = usesOracleCurrentSchema ? "" : metadataQuerySchema(connectionId, database, completionSchema);
|
const querySchema = usesCurrentSchema ? "" : metadataQuerySchema(connectionId, database, completionSchema);
|
||||||
const columns = await api.getColumns(connectionId, database, querySchema, completionTable, catalog, usesOracleCurrentSchema ? context?.clientSessionId : undefined);
|
const columns = await api.getColumns(connectionId, database, querySchema, completionTable, catalog, usesCurrentSchema ? context?.clientSessionId : undefined);
|
||||||
if (requestRevision !== completionCacheRevision(connectionId, database)) return;
|
if (requestRevision !== completionCacheRevision(connectionId, database)) return;
|
||||||
completionColumnsCache.value[cacheKey] = columns;
|
completionColumnsCache.value[cacheKey] = columns;
|
||||||
evictOldestCacheEntries(completionColumnsCache.value, COMPLETION_CACHE_MAX);
|
evictOldestCacheEntries(completionColumnsCache.value, COMPLETION_CACHE_MAX);
|
||||||
@@ -7867,7 +7868,7 @@ export const useConnectionStore = defineStore("connection", () => {
|
|||||||
isNullable: column.is_nullable,
|
isNullable: column.is_nullable,
|
||||||
comment: column.comment,
|
comment: column.comment,
|
||||||
}));
|
}));
|
||||||
if (!usesOracleCurrentSchema) indexCompletionColumns(connectionId, database, completionTable, completionSchema, columns, catalog, context);
|
if (!usesCurrentSchema) indexCompletionColumns(connectionId, database, completionTable, completionSchema, columns, catalog, context);
|
||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user