fix(sqlserver): preserve unqualified table schema for editing

This commit is contained in:
zipg authored and GitHub committed 2026-07-31 15:25:24 +08:00
1 parent ac6b53365b
commit e48902fa06
6 files changed
+167 -13

No files matched your search

+12 -3
View File
@@ -2771,6 +2771,7 @@ export const useQueryStore = defineStore("query", () => {
source: EditableQuerySource;
analysis: EditableQueryInfo;
request: TableMetadataRequest;
writeSchema?: string;
};
interface EditableQueryExecutionPreparation {
@@ -2794,7 +2795,11 @@ export const useQueryStore = defineStore("query", () => {
// with a qualified source.
const qualifiedSourceDatabase = dbType === "sqlserver" ? source.catalog : connectionUsesDatabaseObjectTreeMode(conn) ? source.schema : undefined;
const metadataDatabase = qualifiedSourceDatabase || executionDatabase || conn?.database || tab.database;
let schema = source.schema || tab.schema;
// SQL Server does not apply the query tab's selected schema to an
// unqualified object reference. Resolve metadata through the login's
// default schema (with the driver's dbo fallback) so metadata and writes
// target the same object as the original SELECT.
let schema = source.schema || (dbType === "sqlserver" ? "" : tab.schema);
if (!schema) {
if (dbType === "postgres" || dbType === "kwdb") schema = "public";
else schema = "";
@@ -2802,7 +2807,7 @@ export const useQueryStore = defineStore("query", () => {
// Oracle-family connection databases are service names, not schemas. When
// the query does not qualify a schema, let the driver resolve the current
// login user's schema instead of looking up metadata under the service name.
const resolvedSchema = ORACLE_LIKE_METADATA_TYPES.has(dbType) && !schema ? "" : metadataSchemaForConnection(conn, metadataDatabase, schema || undefined);
const resolvedSchema = (dbType === "sqlserver" && !source.schema) || (ORACLE_LIKE_METADATA_TYPES.has(dbType) && !schema) ? "" : metadataSchemaForConnection(conn, metadataDatabase, schema || undefined);
const metadataSchema = normalizeOracleLikeMetadataIdentifier(dbType, resolvedSchema || undefined, source.schema ? source.schemaQuoted : false) || "";
const metadataTableName = normalizeOracleLikeMetadataIdentifier(dbType, source.tableName, source.tableNameQuoted)!;
const metadataCatalog = normalizeOracleLikeMetadataIdentifier(dbType, source.catalog, source.catalogQuoted);
@@ -2812,10 +2817,14 @@ export const useQueryStore = defineStore("query", () => {
schema: metadataSchema || undefined,
tableName: metadataTableName,
};
// Keep SQL Server writes unqualified unless the SELECT source explicitly
// named a schema, so SELECT and UPDATE resolve the same object.
const writeSchema = dbType === "sqlserver" && !source.schema ? undefined : metadataSchema || undefined;
const knownTableType = tab.tableMeta?.tableName.toLowerCase() === metadataTableName.toLowerCase() && normalizeOptionalSchema(tab.tableMeta.schema) === normalizeOptionalSchema(metadataSchema) ? tab.tableMeta.tableType : undefined;
return {
source: metadataSource,
analysis: normalizeOracleLikeQueryAnalysis(dbType, cloneAnalysisForSource(analysis, metadataSource), metadataSchema || undefined, metadataTableName),
writeSchema,
request: {
connectionId: tab.connectionId!,
database: metadataDatabase,
@@ -2836,7 +2845,7 @@ export const useQueryStore = defineStore("query", () => {
tableMeta: {
catalog: target.request.catalog,
database: target.request.database,
schema: target.request.schema || undefined,
schema: target.writeSchema,
tableName: target.request.tableName,
tableType: metadata.tableType,
columns: metadata.columns,