fix(mysql): use database-specific catalog switching
This commit is contained in:
1 parent
9571519179
commit
2152f8cde8
31 files changed
+818
-182
No files matched your search
+45
-1
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSelectAllSql, isNewQueryPrefillSupported, resolveNewQueryInitialSql, resolveNewQueryTable, type ResolveNewQueryTableInput } from "@/lib/sql/newQueryContext";
|
||||
import { buildSelectAllSql, isNewQueryPrefillSupported, resolveNewQueryInitialSql, resolveNewQueryTable, resolveNewQueryTarget } from "@/lib/sql/newQueryContext";
|
||||
import type { ResolveNewQueryTableInput } from "@/lib/sql/newQueryContext";
|
||||
import type { QueryTab, TreeNode } from "@/types/database";
|
||||
|
||||
function dataTab(overrides: Partial<Pick<QueryTab, "mode" | "connectionId" | "database" | "schema" | "tableMeta" | "structureTableName" | "title">> = {}): ResolveNewQueryTableInput["activeTab"] {
|
||||
@@ -18,6 +19,46 @@ function tableNode(overrides: Partial<Pick<TreeNode, "type" | "connectionId" | "
|
||||
return { type: "table", connectionId: "conn-1", database: "app_db", schema: "public", tableName: "orders", label: "orders", ...overrides };
|
||||
}
|
||||
|
||||
describe("resolveNewQueryTarget", () => {
|
||||
it("inherits an external catalog from the active object browser", () => {
|
||||
expect(
|
||||
resolveNewQueryTarget({
|
||||
activeTab: {
|
||||
connectionId: "conn-1",
|
||||
database: "bi",
|
||||
objectBrowser: { catalog: "paimon_catalog" },
|
||||
},
|
||||
connections: [{ id: "conn-1", database: "" }],
|
||||
preferredSource: "tab",
|
||||
}),
|
||||
).toEqual({
|
||||
connectionId: "conn-1",
|
||||
database: "bi",
|
||||
schema: undefined,
|
||||
catalog: "paimon_catalog",
|
||||
shouldRefreshDefaultDatabase: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("inherits an external catalog from active table metadata", () => {
|
||||
expect(
|
||||
resolveNewQueryTarget({
|
||||
activeTab: {
|
||||
connectionId: "conn-1",
|
||||
database: "bi",
|
||||
tableMeta: {
|
||||
catalog: "paimon_catalog",
|
||||
tableName: "events",
|
||||
columns: [],
|
||||
primaryKeys: [],
|
||||
},
|
||||
},
|
||||
connections: [{ id: "conn-1", database: "" }],
|
||||
})?.catalog,
|
||||
).toBe("paimon_catalog");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveNewQueryTable", () => {
|
||||
it("resolves the table from an active data tab", () => {
|
||||
const table = resolveNewQueryTable({ activeTab: dataTab(), preferredSource: "tab" });
|
||||
@@ -119,6 +160,9 @@ describe("buildSelectAllSql", () => {
|
||||
expect(buildSelectAllSql("mysql", { tableName: "a`b" })).toBe("SELECT * FROM `a``b`");
|
||||
expect(buildSelectAllSql("postgres", { tableName: 'a"b' })).toBe('SELECT * FROM "a""b"');
|
||||
});
|
||||
it("qualifies a StarRocks external-catalog table with catalog and database", () => {
|
||||
expect(buildSelectAllSql("starrocks", { catalog: "paimon_catalog", database: "bi", tableName: "events" })).toBe("SELECT * FROM `paimon_catalog`.`bi`.`events`");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isNewQueryPrefillSupported", () => {
|
||||
|
||||
@@ -813,6 +813,7 @@ export async function executeQuery(
|
||||
executionId?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
resultSessionId?: string;
|
||||
@@ -839,6 +840,7 @@ export async function executeMulti(
|
||||
executionId?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
resultSessionId?: string;
|
||||
@@ -874,6 +876,7 @@ export async function executeMultiWithProgress(
|
||||
schema?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
resultSessionId?: string;
|
||||
@@ -895,20 +898,22 @@ export async function executeMultiWithProgress(
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function closeQuerySession(connectionId: string, database: string, sessionId: string, clientSessionId?: string): Promise<boolean> {
|
||||
export async function closeQuerySession(connectionId: string, database: string, sessionId: string, clientSessionId?: string, catalog?: string): Promise<boolean> {
|
||||
return post("/api/query/close-session", {
|
||||
connectionId,
|
||||
database,
|
||||
sessionId,
|
||||
clientSessionId,
|
||||
catalog,
|
||||
});
|
||||
}
|
||||
|
||||
export async function closeClientConnectionSession(connectionId: string, database: string, clientSessionId: string): Promise<boolean> {
|
||||
export async function closeClientConnectionSession(connectionId: string, database: string, clientSessionId: string, catalog?: string): Promise<boolean> {
|
||||
return post("/api/query/close-client-session", {
|
||||
connectionId,
|
||||
database,
|
||||
clientSessionId,
|
||||
catalog,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -939,16 +944,17 @@ export async function executeScriptWith2pc(connectionId: string, database: strin
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeInTransaction(connectionId: string, database: string, statements: string[], schema?: string): Promise<QueryResult> {
|
||||
export async function executeInTransaction(connectionId: string, database: string, statements: string[], schema?: string, catalog?: string): Promise<QueryResult> {
|
||||
return post("/api/query/execute-in-transaction", {
|
||||
connectionId,
|
||||
database,
|
||||
statements,
|
||||
schema,
|
||||
catalog,
|
||||
});
|
||||
}
|
||||
|
||||
export async function beginManualTransaction(_connectionId: string, _database: string, _schema?: string): Promise<string> {
|
||||
export async function beginManualTransaction(_connectionId: string, _database: string, _schema?: string, _catalog?: string): Promise<string> {
|
||||
throw new Error("Manual transaction management is only available in the desktop app.");
|
||||
}
|
||||
|
||||
|
||||
@@ -1021,6 +1021,7 @@ export async function executeQuery(
|
||||
executionId?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
resultSessionId?: string;
|
||||
@@ -1047,6 +1048,7 @@ export async function executeMulti(
|
||||
executionId?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
resultSessionId?: string;
|
||||
@@ -1082,6 +1084,7 @@ export async function executeMultiWithProgress(
|
||||
schema?: string,
|
||||
options?: {
|
||||
maxRows?: number;
|
||||
catalog?: string;
|
||||
fetchSize?: number;
|
||||
pageSize?: number;
|
||||
resultSessionId?: string;
|
||||
@@ -1118,20 +1121,22 @@ export async function cancelQuery(executionId: string): Promise<boolean> {
|
||||
return invoke("cancel_query", { executionId });
|
||||
}
|
||||
|
||||
export async function closeQuerySession(connectionId: string, database: string, sessionId: string, clientSessionId?: string): Promise<boolean> {
|
||||
export async function closeQuerySession(connectionId: string, database: string, sessionId: string, clientSessionId?: string, catalog?: string): Promise<boolean> {
|
||||
return invoke("close_query_session", {
|
||||
connectionId,
|
||||
database,
|
||||
sessionId,
|
||||
clientSessionId,
|
||||
catalog,
|
||||
});
|
||||
}
|
||||
|
||||
export async function closeClientConnectionSession(connectionId: string, database: string, clientSessionId: string): Promise<boolean> {
|
||||
export async function closeClientConnectionSession(connectionId: string, database: string, clientSessionId: string, catalog?: string): Promise<boolean> {
|
||||
return invoke("close_client_connection_session", {
|
||||
connectionId,
|
||||
database,
|
||||
clientSessionId,
|
||||
catalog,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1158,17 +1163,18 @@ export async function executeScriptWith2pc(connectionId: string, database: strin
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeInTransaction(connectionId: string, database: string, statements: string[], schema?: string): Promise<QueryResult> {
|
||||
export async function executeInTransaction(connectionId: string, database: string, statements: string[], schema?: string, catalog?: string): Promise<QueryResult> {
|
||||
return invoke("execute_in_transaction", {
|
||||
connectionId,
|
||||
database,
|
||||
statements,
|
||||
schema,
|
||||
catalog,
|
||||
});
|
||||
}
|
||||
|
||||
export async function beginManualTransaction(connectionId: string, database: string, schema?: string): Promise<string> {
|
||||
return invoke("begin_manual_transaction", { connectionId, database, schema });
|
||||
export async function beginManualTransaction(connectionId: string, database: string, schema?: string, catalog?: string): Promise<string> {
|
||||
return invoke("begin_manual_transaction", { connectionId, database, schema, catalog });
|
||||
}
|
||||
|
||||
export async function executeInManualTransaction(txnSessionId: string, sql: string, database: string, schema?: string, maxRows?: number): Promise<QueryResult[]> {
|
||||
|
||||
@@ -13,7 +13,7 @@ export interface NewQueryTarget {
|
||||
export type NewQueryContextSource = "tab" | "sidebar";
|
||||
|
||||
interface ResolveNewQueryTargetInput {
|
||||
activeTab?: Pick<QueryTab, "connectionId" | "database" | "schema" | "catalog">;
|
||||
activeTab?: Pick<QueryTab, "connectionId" | "database" | "schema" | "catalog" | "objectBrowser" | "tableMeta">;
|
||||
selectedTreeNode?: Pick<TreeNode, "connectionId" | "database" | "schema" | "catalog"> | null;
|
||||
activeConnectionId?: string | null;
|
||||
connections: Pick<ConnectionConfig, "id" | "database">[];
|
||||
@@ -49,16 +49,18 @@ export function resolveNewQueryTarget(input: ResolveNewQueryTargetInput): NewQue
|
||||
: null;
|
||||
}
|
||||
|
||||
function targetFromContext(context: Pick<QueryTab | TreeNode, "connectionId" | "database" | "schema" | "catalog"> | undefined, connections: Pick<ConnectionConfig, "id" | "database">[]): NewQueryTarget | null {
|
||||
function targetFromContext(context: Pick<QueryTab, "connectionId" | "database" | "schema" | "catalog" | "objectBrowser" | "tableMeta"> | Pick<TreeNode, "connectionId" | "database" | "schema" | "catalog"> | undefined, connections: Pick<ConnectionConfig, "id" | "database">[]): NewQueryTarget | null {
|
||||
if (!context?.connectionId) return null;
|
||||
const connection = connections.find((item) => item.id === context.connectionId);
|
||||
if (!connection) return null;
|
||||
const database = context.database || resolveDefaultDatabase(connection, []);
|
||||
const objectBrowser = "objectBrowser" in context ? context.objectBrowser : undefined;
|
||||
const tableMeta = "tableMeta" in context ? context.tableMeta : undefined;
|
||||
return {
|
||||
connectionId: context.connectionId,
|
||||
database,
|
||||
schema: "schema" in context ? (context as { schema?: string }).schema : undefined,
|
||||
catalog: "catalog" in context ? (context as { catalog?: string }).catalog : undefined,
|
||||
schema: context.schema ?? objectBrowser?.schema ?? tableMeta?.schema,
|
||||
catalog: context.catalog ?? objectBrowser?.catalog ?? tableMeta?.catalog,
|
||||
shouldRefreshDefaultDatabase: !context.database,
|
||||
};
|
||||
}
|
||||
@@ -72,7 +74,7 @@ export interface NewQueryTable {
|
||||
}
|
||||
|
||||
export interface ResolveNewQueryTableInput {
|
||||
activeTab?: Pick<QueryTab, "mode" | "connectionId" | "database" | "schema" | "tableMeta" | "structureTableName" | "title"> | null;
|
||||
activeTab?: Pick<QueryTab, "mode" | "connectionId" | "database" | "schema" | "catalog" | "tableMeta" | "structureTableName" | "title"> | null;
|
||||
selectedTreeNode?: Pick<TreeNode, "type" | "connectionId" | "database" | "schema" | "catalog" | "tableName" | "label"> | null;
|
||||
preferredSource?: NewQueryContextSource;
|
||||
}
|
||||
@@ -106,7 +108,7 @@ function tableFromTab(tab: ResolveNewQueryTableInput["activeTab"]): NewQueryTabl
|
||||
if (tab.mode === "structure") {
|
||||
const tableName = (tab.structureTableName || "").trim();
|
||||
if (!tableName) return null;
|
||||
return { connectionId: tab.connectionId, database: tab.database, schema: tab.schema, tableName };
|
||||
return { connectionId: tab.connectionId, database: tab.database, schema: tab.schema, catalog: tab.catalog, tableName };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -137,8 +139,8 @@ export function resolveNewQueryTable(input: ResolveNewQueryTableInput): NewQuery
|
||||
* the same per-dialect identifier quoting and schema/catalog qualification used
|
||||
* by the table-data view.
|
||||
*/
|
||||
export function buildSelectAllSql(databaseType: DatabaseType | undefined, table: Pick<NewQueryTable, "schema" | "catalog" | "tableName">): string {
|
||||
const ref = qualifiedTableName({ databaseType, schema: table.schema, catalog: table.catalog, tableName: table.tableName });
|
||||
export function buildSelectAllSql(databaseType: DatabaseType | undefined, table: Pick<NewQueryTable, "schema" | "catalog" | "tableName"> & Partial<Pick<NewQueryTable, "database">>): string {
|
||||
const ref = qualifiedTableName({ databaseType, database: table.database, schema: table.schema, catalog: table.catalog, tableName: table.tableName });
|
||||
return `SELECT * FROM ${ref}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ import { qualifiedTableName, quoteTableIdentifier } from "@/lib/table/tableSelec
|
||||
export interface TableSqlTemplateOptions {
|
||||
databaseType?: DatabaseType;
|
||||
schema?: string;
|
||||
catalog?: string;
|
||||
database?: string;
|
||||
tableName: string;
|
||||
columns?: ColumnInfo[];
|
||||
tableType?: string;
|
||||
@@ -96,6 +98,8 @@ export function buildTableDeleteTemplate(options: TableSqlTemplateOptions): stri
|
||||
function templateTableName(options: TableSqlTemplateOptions): string {
|
||||
return qualifiedTableName({
|
||||
databaseType: options.databaseType,
|
||||
catalog: options.catalog,
|
||||
database: options.database,
|
||||
schema: options.schema,
|
||||
tableName: options.tableName,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user