Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(grid): qualify generated save and copy SQL with database name

zipg420cb80e65b0
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue
index b61f13b22c..ee9b2a708c 100644
--- a/apps/desktop/src/components/editor/QueryEditor.vue
+++ b/apps/desktop/src/components/editor/QueryEditor.vue
@@ -48,7 +48,7 @@ import { looksLikeDmlStatement } from "@/lib/sql/dmlChangePreview";
import { expandToSqlStatementWindow } from "@/lib/sql/insertValueHints";
import { insertValueHintColumnNames } from "@/lib/sql/insertValueHintColumns";
import { canFormatSqlForDatabaseType, formatSqlForDisplay, formatSqlForEditing, compressSqlText, sqlFormatDialectForDbType, type SqlFormatDialect } from "@/lib/sql/sqlFormatter";
-import { omitDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
+import { applyDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
import { detectAndFormatStructured } from "@/lib/sql/autoFormat";
import { enabledSqlParameterSyntaxes, resolveSqlVariableSyntaxToggles } from "@/lib/sql/sqlVariableSyntax";
import { blankLineDeletionChanges, replaceSelectedEditorText } from "@/lib/editor/queryEditorTextEdits";
@@ -3561,7 +3561,7 @@ async function resolveSqlHoverTooltip(currentView: EditorViewType, pos: number)
// the aligned column layout from reformatHoverDdl.
const isViewObject = objectMetadataRequest.objectType === "VIEW" || objectMetadataRequest.objectType === "MATERIALIZED_VIEW";
const formatted = isViewObject ? await formatSqlForDisplay(rawDdl, formatDialect, settingsStore.editorSettings.sqlFormatter) : reformatHoverDdl(rawDdl, quoteQualifiedName(hoverQualifiedName));
- const unqualified = omitDdlDatabaseQualifier(formatted, formatDialect, props.databaseType, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.catalog);
+ const unqualified = applyDdlDatabaseQualifier(formatted, formatDialect, props.databaseType, settingsStore.editorSettings.generateSqlIncludeDatabaseName, hoverDatabase, props.catalog);
sqlContent = settingsStore.editorSettings.generateSqlQuoteIdentifiers ? unqualified : omitDdlIdentifierQuotes(unqualified, formatDialect);
}
} catch (error) {
@@ -3595,7 +3595,7 @@ async function resolveSqlHoverTooltip(currentView: EditorViewType, pos: number)
}
if (fullColumns.length > 0) {
sqlContent = buildHoverTableSql(quoteQualifiedName(hoverQualifiedName), fullColumns, fullIndexes, tableComment);
- sqlContent = omitDdlDatabaseQualifier(sqlContent, formatDialect, props.databaseType, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.catalog);
+ sqlContent = applyDdlDatabaseQualifier(sqlContent, formatDialect, props.databaseType, settingsStore.editorSettings.generateSqlIncludeDatabaseName, hoverDatabase, props.catalog);
if (!settingsStore.editorSettings.generateSqlQuoteIdentifiers) sqlContent = omitDdlIdentifierQuotes(sqlContent, formatDialect);
metadataLoadFailed = false;
}
diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue
index e91304b351..e7d9627355 100644
--- a/apps/desktop/src/components/grid/DataGrid.vue
+++ b/apps/desktop/src/components/grid/DataGrid.vue
@@ -3719,6 +3719,7 @@ const editor = useDataGridEditor({
rowStatusFilter,
dataGridQuickEntryEnabled: computed(() => settingsStore.editorSettings.dataGridQuickEntry),
confirmDangerousRowDeletion: computed(() => settingsStore.editorSettings.confirmDangerousSqlExecution),
+ includeDatabaseNameInSaveSql: computed(() => settingsStore.editorSettings.generateSqlIncludeDatabaseName),
initialEditColumn: firstVisibleColumnIndex,
cellEditorText: cellEditorTextForValue,
normalizeEditorInput: (value) => (props.mongoCollectionGrid ? mongoDocumentGridInputValue(value) : value),
diff --git a/apps/desktop/src/components/objects/DdlViewDialog.vue b/apps/desktop/src/components/objects/DdlViewDialog.vue
index 8488a158c1..69425e2f0e 100644
--- a/apps/desktop/src/components/objects/DdlViewDialog.vue
+++ b/apps/desktop/src/components/objects/DdlViewDialog.vue
@@ -11,7 +11,7 @@ import { loadEditorTheme, editorFontTheme } from "@/lib/editor/editorThemes";
import { createDbxCodeMirrorSqlDialect } from "@/lib/editor/codemirrorSqlDialect";
import { copyToClipboard } from "@/lib/common/clipboard";
import { formatSqlForDisplay, type SqlFormatDialect } from "@/lib/sql/sqlFormatter";
-import { ddlFormatDialectFor, omitDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
+import { applyDdlDatabaseQualifier, ddlFormatDialectFor, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
import { loadObjectDdl } from "@/lib/metadata/objectDdlCache";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
@@ -131,7 +131,7 @@ async function loadDdl(force = false) {
);
const formatDialect = ddlFormatDialectFor({ formatDialect: props.formatDialect, databaseType: props.databaseType, highlightDialect: props.dialect });
const formatted = await formatSqlForDisplay(ddl, formatDialect, settingsStore.editorSettings.sqlFormatter);
- const unqualified = omitDdlDatabaseQualifier(formatted, formatDialect, props.databaseType, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.catalog);
+ const unqualified = applyDdlDatabaseQualifier(formatted, formatDialect, props.databaseType, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.database, props.catalog);
rawDdlContent.value = settingsStore.editorSettings.generateSqlQuoteIdentifiers ? unqualified : omitDdlIdentifierQuotes(unqualified, formatDialect);
} catch (e: any) {
ddlError.value = e?.message || String(e);
diff --git a/apps/desktop/src/components/objects/ObjectBrowser.vue b/apps/desktop/src/components/objects/ObjectBrowser.vue
index 69fb44eed9..604cf1e01a 100644
--- a/apps/desktop/src/components/objects/ObjectBrowser.vue
+++ b/apps/desktop/src/components/objects/ObjectBrowser.vue
@@ -122,7 +122,7 @@ import { useQueryStore } from "@/stores/queryStore";
import QueryEditor from "@/components/editor/QueryEditor.vue";
import MySqlEventEditor from "@/components/objects/MySqlEventEditor.vue";
import { sqlFormatDialectForDbType, type SqlFormatDialect } from "@/lib/sql/sqlFormatter";
-import { omitDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
+import { applyDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
import { isCancelSearchShortcut } from "@/lib/editor/keyboardShortcuts";
import { executeWithProductionSqlGuard } from "@/lib/database/productionExecutionGuard";
import { connectionIsEffectivelyReadOnly } from "@/lib/database/readOnlyWriteAccess";
@@ -1194,7 +1194,7 @@ async function fetchTableDdl(force = settingsStore.editorSettings.refreshDdlOnOp
const { ddl } = await loadObjectDdl(tableMetadataRequest(row), { force });
if (sidePanelGuard.isStale(epoch)) return;
const formatDialect = sqlFormatDialectForDbType(effectiveDatabaseType.value);
- const unqualified = omitDdlDatabaseQualifier(ddl, formatDialect, effectiveDatabaseType.value, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.catalog);
+ const unqualified = applyDdlDatabaseQualifier(ddl, formatDialect, effectiveDatabaseType.value, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.database, props.catalog);
rawTableDdlContent.value = settingsStore.editorSettings.generateSqlQuoteIdentifiers ? unqualified : omitDdlIdentifierQuotes(unqualified, formatDialect);
loadedSuccessfully = true;
} catch (e: any) {
diff --git a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
index baa68e14f0..acd50ed9d0 100644
--- a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
+++ b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
@@ -2188,7 +2188,7 @@ async function openSidebarMultiTableDdlTab(targets: Array<TreeNode & { connectio
async (ddl, target) => {
const formatDialect = sqlFormatDialectForDbType(databaseTypeForNode(target));
const formatted = await formatSqlForDisplay(ddl, formatDialect, settingsStore.editorSettings.sqlFormatter);
- return formatSidebarDdlTemplateForDisplay(formatted, formatDialect, databaseTypeForNode(target), settingsStore.editorSettings.generateSqlIncludeDatabaseName, settingsStore.editorSettings.generateSqlQuoteIdentifiers, target.catalog);
+ return formatSidebarDdlTemplateForDisplay(formatted, formatDialect, databaseTypeForNode(target), settingsStore.editorSettings.generateSqlIncludeDatabaseName, target.database, settingsStore.editorSettings.generateSqlQuoteIdentifiers, target.catalog);
},
);
connectionStore.activeConnectionId = tabTarget.connectionId;
diff --git a/apps/desktop/src/components/structure/TableStructureEditor.vue b/apps/desktop/src/components/structure/TableStructureEditor.vue
index 0d16aabede..7bcd257a08 100644
--- a/apps/desktop/src/components/structure/TableStructureEditor.vue
+++ b/apps/desktop/src/components/structure/TableStructureEditor.vue
@@ -31,7 +31,7 @@ import { useToast } from "@/composables/useToast";
import { useVerticalOverlayScrollbar } from "@/composables/useVerticalOverlayScrollbar";
import { type SqlHighlighter, createShikiSqlHighlighter } from "@/lib/sql/sqlHighlighter";
import { joinSqlStatementsForScript } from "@/lib/sql/sqlBatchScript";
-import { formatGeneratedDdlIdentifierQuotes, omitDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
+import { applyDdlDatabaseQualifier, formatGeneratedDdlIdentifierQuotes, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
import { splitSqlStatementRanges } from "@/lib/sql/sqlStatementRanges";
import { copyToClipboard } from "@/lib/common/clipboard";
import DataGridCopyColumnNamesDialog from "@/components/grid/DataGridCopyColumnNamesDialog.vue";
@@ -371,7 +371,7 @@ function scheduleDdlEditorInit() {
* generated from the pending structure changes.
*/
function formatDdlForDisplay(sql: string, dialect: SqlFormatDialect, generated = false): string {
- const unqualified = omitDdlDatabaseQualifier(sql, dialect, databaseType.value, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.catalog);
+ const unqualified = applyDdlDatabaseQualifier(sql, dialect, databaseType.value, settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.database, props.catalog);
if (settingsStore.editorSettings.generateSqlQuoteIdentifiers) return unqualified;
return generated ? formatGeneratedDdlIdentifierQuotes(unqualified, dialect, false) : omitDdlIdentifierQuotes(unqualified, dialect);
}
diff --git a/apps/desktop/src/composables/__tests__/useDataGridEditor.saveSqlDatabaseName.spec.ts b/apps/desktop/src/composables/__tests__/useDataGridEditor.saveSqlDatabaseName.spec.ts
new file mode 100644
index 0000000000..7c740374d7
--- /dev/null
+++ b/apps/desktop/src/composables/__tests__/useDataGridEditor.saveSqlDatabaseName.spec.ts
@@ -0,0 +1,100 @@
+// @vitest-environment happy-dom
+import { computed, ref } from "vue";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { useDataGridEditor } from "@/composables/useDataGridEditor";
+import type { CellValue } from "@/lib/dataGrid/cellValue";
+
+const mocks = vi.hoisted(() => ({
+ getConfig: vi.fn(),
+ prepareDataGridSave: vi.fn(),
+ executeBatch: vi.fn(),
+}));
+
+vi.mock("@/lib/backend/api", () => ({
+ prepareDataGridSave: mocks.prepareDataGridSave,
+ executeBatch: mocks.executeBatch,
+ executeConditionalUpdate: vi.fn(),
+ cancelConditionalUpdate: vi.fn(),
+ executeInTransaction: vi.fn(),
+ executeInManualTransaction: vi.fn(),
+ executeQuery: vi.fn(),
+ unlockConnectionWrites: vi.fn(),
+ lockConnectionWrites: vi.fn(),
+ connectionWriteUnlockState: vi.fn().mockResolvedValue(0),
+}));
+vi.mock("@/stores/connectionStore", () => ({
+ useConnectionStore: () => ({ getConfig: mocks.getConfig }),
+}));
+vi.mock("@/stores/historyStore", () => ({
+ useHistoryStore: () => ({ add: vi.fn() }),
+}));
+vi.mock("@/stores/productionSafetyStore", () => ({
+ useProductionSafetyStore: () => ({}),
+}));
+
+function createEditor(includeDatabaseNameInSaveSql: boolean) {
+ const rows = ref<CellValue[][]>([["alpha"]]);
+ let editor: ReturnType<typeof useDataGridEditor>;
+ editor = useDataGridEditor({
+ result: computed(() => ({ columns: ["name"], rows: rows.value })),
+ editable: computed(() => true),
+ databaseType: computed(() => "mysql"),
+ connectionId: computed(() => "connection-1"),
+ database: computed(() => "dbx"),
+ tableMeta: computed(() => ({
+ database: "dbx",
+ tableName: "filter_probe",
+ columns: [{ name: "name", data_type: "varchar(50)" }],
+ primaryKeys: ["id"],
+ })),
+ sourceColumns: computed(() => undefined),
+ includeDatabaseNameInSaveSql: computed(() => includeDatabaseNameInSaveSql),
+ onExecuteSql: computed(() => undefined),
+ sql: computed(() => undefined),
+ searchText: ref(""),
+ whereFilterInput: ref(""),
+ currentWhereInput: computed(() => undefined),
+ orderByInput: ref(""),
+ rowStatusFilter: ref("all"),
+ pageSize: ref(100),
+ currentPage: ref(1),
+ onCellValueChanged: undefined,
+ getRowItem: (rowId) => {
+ if (rowId < 0) return undefined;
+ const row = rows.value[rowId];
+ if (!row) return undefined;
+ const changes = editor.dirtyRows.value.get(rowId);
+ return {
+ id: rowId,
+ sourceIndex: rowId,
+ data: row.map((value, columnIndex) => (changes?.has(columnIndex) ? (changes.get(columnIndex) ?? null) : value)),
+ isNew: false,
+ isDeleted: false,
+ isDirtyCol: row.map((_, columnIndex) => changes?.has(columnIndex) ?? false),
+ status: changes?.size ? "edited" : "normal",
+ };
+ },
+ emit: vi.fn(),
+ });
+ editor.applyCellValue(0, 0, "changed");
+ return editor;
+}
+
+describe("useDataGridEditor save SQL database qualification", () => {
+ beforeEach(() => {
+ mocks.prepareDataGridSave.mockReset();
+ mocks.executeBatch.mockReset();
+ mocks.getConfig.mockReset();
+ mocks.getConfig.mockReturnValue(undefined);
+ mocks.prepareDataGridSave.mockResolvedValue({ statements: ["UPDATE x"], rollbackStatements: [] });
+ mocks.executeBatch.mockResolvedValue([]);
+ });
+
+ it("forwards the include-database-name setting to the save statement builder", async () => {
+ await createEditor(true).saveChanges();
+ expect(mocks.prepareDataGridSave.mock.calls[0]![0]).toMatchObject({ includeDatabaseName: true });
+
+ await createEditor(false).saveChanges();
+ expect(mocks.prepareDataGridSave.mock.calls[1]![0]).toMatchObject({ includeDatabaseName: false });
+ });
+});
diff --git a/apps/desktop/src/composables/useDataGridEditor.ts b/apps/desktop/src/composables/useDataGridEditor.ts
index 3240971dd0..fe056b462b 100644
--- a/apps/desktop/src/composables/useDataGridEditor.ts
+++ b/apps/desktop/src/composables/useDataGridEditor.ts
@@ -119,6 +119,8 @@ export interface UseDataGridEditorOptions {
rowStatusFilter: Ref<RowStatusFilter>;
dataGridQuickEntryEnabled?: ComputedRef<boolean>;
confirmDangerousRowDeletion?: ComputedRef<boolean>;
+ /** `生成 SQL 时包含数据库名` — qualify saved tables with their database. */
+ includeDatabaseNameInSaveSql?: ComputedRef<boolean>;
initialEditColumn?: ComputedRef<number>;
/** Converts a grid value to the text presented by the cell editor. */
cellEditorText?: (value: CellValue, columnIndex: number) => string;
@@ -246,6 +248,7 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
rowStatusFilter,
dataGridQuickEntryEnabled = computed(() => false),
confirmDangerousRowDeletion = computed(() => true),
+ includeDatabaseNameInSaveSql = computed(() => false),
initialEditColumn,
cellEditorText,
normalizeEditorInput,
@@ -1572,6 +1575,7 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
dirtyRows: [...snapshot.dirtyRows.entries()].map(([rowIndex, changes]) => [rowIndex, [...changes.entries()]] as [number, Array<[number, CellValue]>]),
deletedRows: [...snapshot.deletedRows],
newRows: snapshot.newRows,
+ includeDatabaseName: includeDatabaseNameInSaveSql.value,
};
}
diff --git a/apps/desktop/src/composables/useDataGridTableMetadataLoaders.ts b/apps/desktop/src/composables/useDataGridTableMetadataLoaders.ts
index 5eeeadb2d0..09244e05c7 100644
--- a/apps/desktop/src/composables/useDataGridTableMetadataLoaders.ts
+++ b/apps/desktop/src/composables/useDataGridTableMetadataLoaders.ts
@@ -1,6 +1,6 @@
import { computed, type ComputedRef, type Ref } from "vue";
import * as api from "@/lib/backend/api";
-import { omitDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
+import { applyDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
import { sqlFormatDialectForDbType } from "@/lib/sql/sqlFormatter";
import { loadObjectDdl } from "@/lib/metadata/objectDdlCache";
import { loadObjectMetadataFacet } from "@/lib/metadata/objectMetadataCache";
@@ -119,7 +119,7 @@ export function useDataGridTableMetadataLoaders(options: DataGridTableMetadataLo
try {
const { ddl } = await loadObjectDdl(request, { force });
const formatDialect = sqlFormatDialectForDbType(options.resolvedDatabaseType.value);
- const unqualified = omitDdlDatabaseQualifier(ddl, formatDialect, options.resolvedDatabaseType.value, options.settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.tableMeta?.catalog);
+ const unqualified = applyDdlDatabaseQualifier(ddl, formatDialect, options.resolvedDatabaseType.value, options.settingsStore.editorSettings.generateSqlIncludeDatabaseName, props.tableMeta?.database, props.tableMeta?.catalog);
state.ddlContent.value = options.settingsStore.editorSettings.generateSqlQuoteIdentifiers ? unqualified : omitDdlIdentifierQuotes(unqualified, formatDialect);
} catch (error: any) {
state.ddlContent.value = `-- Error: ${error}`;
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index af02e0a96c..b7272372da 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -7798,7 +7798,7 @@ export default {
prefillNewQueryWithSelect: "Prefill new query with SELECT *",
prefillNewQueryWithSelectDescription: "When creating a new query, prefill the editor with SELECT * FROM <table> based on the active table tab or the table selected in the sidebar.",
generateSqlIncludeDatabaseName: "Include database name in generated SQL",
- generateSqlIncludeDatabaseNameDescription: "When supported by the database, generated table SQL uses database.table instead of only the table name.",
+ generateSqlIncludeDatabaseNameDescription: "When supported by the database, generated table SQL uses the qualified name (database.schema.table on SQL Server) instead of only the table name.",
generateSqlQuoteIdentifiers: "Quote identifiers in generated SQL",
generateSqlQuoteIdentifiersDescription: "When disabled, new query table references omit dialect-specific quotes. Reserved words, special characters, and case-sensitive names may require quotes to run.",
formatSqlOnSqlFileSave: "Format SQL when saving SQL files",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index fee216348b..227aef95c8 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -7768,7 +7768,7 @@ export default withEnglishFallback({
prefillNewQueryWithSelect: "新建查询时预填充 SELECT 语句",
prefillNewQueryWithSelectDescription: "新建查询时,根据当前激活的数据表标签页或侧边栏选中的表,自动在编辑器中填充 SELECT * FROM <表名>。",
generateSqlIncludeDatabaseName: "生成 SQL 时包含数据库名",
- generateSqlIncludeDatabaseNameDescription: "数据库支持时,生成的表 SQL 将使用“数据库名.表名”,而不只包含表名。",
+ generateSqlIncludeDatabaseNameDescription: "数据库支持时,生成的表 SQL 将使用限定名(SQL Server 为“数据库名.schema.表名”),而不只包含表名。",
generateSqlQuoteIdentifiers: "生成 SQL 时为标识符添加引号",
generateSqlQuoteIdentifiersDescription: "关闭后,新建查询中的表引用将不再使用数据库方言引号。保留字、特殊字符或大小写敏感名称可能仍需手动添加引号。",
formatSqlOnSqlFileSave: "保存 SQL 文件时格式化 SQL",
diff --git a/apps/desktop/src/lib/__tests__/sidebar/sidebarDdlTemplate.spec.ts b/apps/desktop/src/lib/__tests__/sidebar/sidebarDdlTemplate.spec.ts
index 8fc94b2dd9..376b75f630 100644
--- a/apps/desktop/src/lib/__tests__/sidebar/sidebarDdlTemplate.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sidebar/sidebarDdlTemplate.spec.ts
@@ -30,7 +30,7 @@ describe("sidebar DDL template", () => {
const sql = await buildSidebarDdlTemplateSql(
[{ databaseType: "oracle" as const, catalog: undefined }],
async () => 'CREATE TABLE "SYSTEM"."TEST" ("ID" NUMBER)',
- async (ddl, target) => formatSidebarDdlTemplateForDisplay(ddl, "oracle", target.databaseType, false, true, target.catalog),
+ async (ddl, target) => formatSidebarDdlTemplateForDisplay(ddl, "oracle", target.databaseType, false, undefined, true, target.catalog),
);
expect(sql).toBe('CREATE TABLE "TEST" ("ID" NUMBER)');
@@ -44,7 +44,7 @@ describe("sidebar DDL template", () => {
const sql = await buildSidebarDdlTemplateSql(
targets,
async (target) => `CREATE TABLE \`iceberg\`.\`analytics\`.\`${target.name}\` (\`id\` bigint)`,
- async (ddl, target) => formatSidebarDdlTemplateForDisplay(ddl, "mysql", "starrocks", false, false, target.catalog),
+ async (ddl, target) => formatSidebarDdlTemplateForDisplay(ddl, "mysql", "starrocks", false, undefined, false, target.catalog),
);
expect(sql).toBe("CREATE TABLE iceberg.analytics.events (id bigint);\n\nCREATE TABLE iceberg.analytics.sessions (id bigint);\n");
diff --git a/apps/desktop/src/lib/__tests__/table/tableSelectSql.spec.ts b/apps/desktop/src/lib/__tests__/table/tableSelectSql.spec.ts
index 5995d4f8a7..3165b03869 100644
--- a/apps/desktop/src/lib/__tests__/table/tableSelectSql.spec.ts
+++ b/apps/desktop/src/lib/__tests__/table/tableSelectSql.spec.ts
@@ -44,6 +44,24 @@ describe("qualifiedTableName — optional database qualification", () => {
});
});
+describe("qualifiedTableName — SQL Server three-part names", () => {
+ it("prefixes the database ahead of the schema once enabled", () => {
+ expect(qualifiedTableName({ databaseType: "sqlserver", database: "dbx", schema: "dbo", tableName: "gen_table" })).toBe("[dbo].[gen_table]");
+ expect(qualifiedTableName({ databaseType: "sqlserver", database: "dbx", schema: "dbo", tableName: "gen_table", includeDatabaseName: true })).toBe("[dbx].[dbo].[gen_table]");
+ });
+
+ it("keeps the schema-only form when the database is unknown", () => {
+ expect(qualifiedTableName({ databaseType: "sqlserver", database: "dbx", schema: "dbo", tableName: "gen_table", includeDatabaseName: true, quoteIdentifiers: false })).toBe("dbx.dbo.gen_table");
+ expect(qualifiedTableName({ databaseType: "sqlserver", schema: "dbo", tableName: "gen_table", includeDatabaseName: true })).toBe("[dbo].[gen_table]");
+ expect(qualifiedTableName({ databaseType: "sqlserver", database: "dbx", tableName: "gen_table", includeDatabaseName: true })).toBe("[gen_table]");
+ });
+
+ it("never stacks the local database on a linked-server schema", () => {
+ const schema = encodeSqlServerLinkedSchema({ server: "ERP", catalog: "Finance", schema: "dbo" });
+ expect(qualifiedTableName({ databaseType: "sqlserver", database: "dbx", schema, tableName: "orders", includeDatabaseName: true })).toBe("[ERP].[Finance].[dbo].[orders]");
+ });
+});
+
describe("qualifyTableReferencesInSql", () => {
it("qualifies FROM and JOIN sources while preserving aliases", () => {
expect(
@@ -114,6 +132,37 @@ describe("qualifyTableReferencesInSql", () => {
});
});
+describe("qualifyTableReferencesInSql — SQL Server three-part names (#9262)", () => {
+ const options = { databaseType: "sqlserver" as const, database: "dbx", includeDatabaseName: true };
+
+ it("prefixes the database ahead of the schema already named", () => {
+ expect(qualifyTableReferencesInSql("SELECT TOP (100) * FROM [dbo].[gen_table]", options)).toBe("SELECT TOP (100) * FROM [dbx].[dbo].[gen_table]");
+ });
+
+ it("qualifies every FROM/JOIN source while keeping aliases", () => {
+ expect(qualifyTableReferencesInSql("SELECT * FROM [dbo].[orders] AS o JOIN [sales].[items] AS i ON i.order_id = o.id", options)).toBe("SELECT * FROM [dbx].[dbo].[orders] AS o JOIN [dbx].[sales].[items] AS i ON i.order_id = o.id");
+ });
+
+ it("handles unquoted and spaced qualifiers", () => {
+ expect(qualifyTableReferencesInSql("SELECT * FROM dbo.gen_table", options)).toBe("SELECT * FROM [dbx].dbo.gen_table");
+ });
+
+ it("leaves already database-qualified and three-part names alone", () => {
+ expect(qualifyTableReferencesInSql("SELECT * FROM [other].[dbo].[gen_table]", options)).toBe("SELECT * FROM [other].[dbo].[gen_table]");
+ expect(qualifyTableReferencesInSql("SELECT * FROM [dbx_test].[dbo].[gen_table]", options)).toBe("SELECT * FROM [dbx_test].[dbo].[gen_table]");
+ });
+
+ it("leaves schema-less names alone because `db.table` is not a SQL Server reference", () => {
+ expect(qualifyTableReferencesInSql("SELECT * FROM [gen_table]", options)).toBe("SELECT * FROM [gen_table]");
+ expect(qualifyTableReferencesInSql("SELECT * FROM [dbx]..[gen_table]", options)).toBe("SELECT * FROM [dbx]..[gen_table]");
+ });
+
+ it("skips CTEs and stays inert when the setting is off", () => {
+ expect(qualifyTableReferencesInSql("WITH cte AS (SELECT * FROM [dbo].[gen_table]) SELECT * FROM cte", options)).toBe("WITH cte AS (SELECT * FROM [dbx].[dbo].[gen_table]) SELECT * FROM cte");
+ expect(qualifyTableReferencesInSql("SELECT * FROM [dbo].[gen_table]", { ...options, includeDatabaseName: false })).toBe("SELECT * FROM [dbo].[gen_table]");
+ });
+});
+
describe("qualifiedTableName — SQLite attached databases", () => {
it("qualifies tables with the attached database alias", () => {
expect(qualifiedTableName({ databaseType: "sqlite", schema: "analytics", tableName: "events" })).toBe('"analytics"."events"');
diff --git a/apps/desktop/src/lib/dataGrid/dataGridSql.ts b/apps/desktop/src/lib/dataGrid/dataGridSql.ts
index ff590487a6..3aa82875f5 100644
--- a/apps/desktop/src/lib/dataGrid/dataGridSql.ts
+++ b/apps/desktop/src/lib/dataGrid/dataGridSql.ts
@@ -33,6 +33,8 @@ export interface DataGridSaveStatementOptions {
dirtyRows: Array<[number, Array<[number, GridCellValue]>]>;
deletedRows: number[];
newRows: GridCellValue[][];
+ /** `生成 SQL 时包含数据库名`: qualify `database.table` engines in the save SQL. */
+ includeDatabaseName?: boolean;
}
export interface DataGridCopyUpdateStatementOptions {
diff --git a/apps/desktop/src/lib/dataGrid/joinedRowChanges.ts b/apps/desktop/src/lib/dataGrid/joinedRowChanges.ts
index 84f2d7cff3..655f2424c6 100644
--- a/apps/desktop/src/lib/dataGrid/joinedRowChanges.ts
+++ b/apps/desktop/src/lib/dataGrid/joinedRowChanges.ts
@@ -9,7 +9,7 @@ export interface JoinedSaveTarget {
/** Feed per-source changes through the existing SQL builder, including undo SQL. */
export function joinedSaveOptions(
targets: JoinedSaveTarget[],
- options: Pick<DataGridSaveStatementOptions, "databaseType" | "identifierQuote" | "columns"> & {
+ options: Pick<DataGridSaveStatementOptions, "databaseType" | "identifierQuote" | "columns" | "includeDatabaseName"> & {
rows: CellValue[][];
dirtyRows: Map<number, Map<number, CellValue>>;
newRows: CellValue[][];
@@ -32,6 +32,7 @@ export function joinedSaveOptions(
dirtyRows: [[update.rowIndex, [...update.changes]]],
newRows: [],
deletedRows: [],
+ includeDatabaseName: options.includeDatabaseName,
}));
}
diff --git a/apps/desktop/src/lib/sidebar/sidebarDdlTemplate.ts b/apps/desktop/src/lib/sidebar/sidebarDdlTemplate.ts
index 26a5c56a51..46dfb96249 100644
--- a/apps/desktop/src/lib/sidebar/sidebarDdlTemplate.ts
+++ b/apps/desktop/src/lib/sidebar/sidebarDdlTemplate.ts
@@ -1,5 +1,5 @@
import { joinExportedDdls } from "@/lib/export/ddlExport";
-import { omitDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
+import { applyDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
import type { SqlFormatDialect } from "@/lib/sql/sqlFormatter";
import type { DatabaseType } from "@/types/database";
@@ -21,7 +21,7 @@ export async function buildSidebarDdlTemplateSql<T>(targets: readonly T[], loadD
return parts.length === 1 ? parts[0]! : joinExportedDdls(parts);
}
-export function formatSidebarDdlTemplateForDisplay(sql: string, dialect: SqlFormatDialect, databaseType: DatabaseType | undefined, includeDatabaseName: boolean, quoteIdentifiers: boolean, catalog?: string): string {
- const unqualified = omitDdlDatabaseQualifier(sql, dialect, databaseType, includeDatabaseName, catalog);
+export function formatSidebarDdlTemplateForDisplay(sql: string, dialect: SqlFormatDialect, databaseType: DatabaseType | undefined, includeDatabaseName: boolean, database: string | undefined, quoteIdentifiers: boolean, catalog?: string): string {
+ const unqualified = applyDdlDatabaseQualifier(sql, dialect, databaseType, includeDatabaseName, database, catalog);
return quoteIdentifiers ? unqualified : omitDdlIdentifierQuotes(unqualified, dialect);
}
diff --git a/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts b/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts
index eb935f9a04..2214532890 100644
--- a/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts
+++ b/apps/desktop/src/lib/sql/__tests__/ddlDisplay.spec.ts
@@ -1,56 +1,90 @@
import { describe, expect, it } from "vitest";
-import { ddlFormatDialectFor, formatGeneratedDdlIdentifierQuotes, omitDdlDatabaseQualifier, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
+import { applyDdlDatabaseQualifier, ddlFormatDialectFor, formatGeneratedDdlIdentifierQuotes, omitDdlIdentifierQuotes } from "@/lib/sql/ddlDisplay";
-describe("omitDdlDatabaseQualifier", () => {
+describe("applyDdlDatabaseQualifier", () => {
it("drops the schema qualifier from Oracle table DDL without touching tablespace references", () => {
const ddl = 'CREATE TABLE "SYSTEM"."TEST" ("ABC" CLOB) TABLESPACE "SYSTEM";';
- expect(omitDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe('CREATE TABLE "TEST" ("ABC" CLOB) TABLESPACE "SYSTEM";');
+ expect(applyDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe('CREATE TABLE "TEST" ("ABC" CLOB) TABLESPACE "SYSTEM";');
});
it("drops the qualifier from generated ALTER statements and keeps column references", () => {
const ddl = 'ALTER TABLE "SYSTEM"."TEST" ADD ("CDE" CLOB); COMMENT ON COLUMN "SYSTEM"."TEST"."CDE" IS \'note\';';
- expect(omitDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe('ALTER TABLE "TEST" ADD ("CDE" CLOB); COMMENT ON COLUMN "TEST"."CDE" IS \'note\';');
+ expect(applyDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe('ALTER TABLE "TEST" ADD ("CDE" CLOB); COMMENT ON COLUMN "TEST"."CDE" IS \'note\';');
});
it("drops the qualifier from PostgreSQL, MySQL, and Dameng table references", () => {
- expect(omitDdlDatabaseQualifier('CREATE TABLE "public"."users" ("id" integer)', "postgres", "postgres", false)).toBe('CREATE TABLE "users" ("id" integer)');
- expect(omitDdlDatabaseQualifier("CREATE TABLE `analytics`.`events` (`id` int)", "mysql", "mysql", false)).toBe("CREATE TABLE `events` (`id` int)");
- expect(omitDdlDatabaseQualifier('ALTER TABLE "APP"."T1" ADD ("C1" INT);', "dameng", "dameng", false)).toBe('ALTER TABLE "T1" ADD ("C1" INT);');
+ expect(applyDdlDatabaseQualifier('CREATE TABLE "public"."users" ("id" integer)', "postgres", "postgres", false)).toBe('CREATE TABLE "users" ("id" integer)');
+ expect(applyDdlDatabaseQualifier("CREATE TABLE `analytics`.`events` (`id` int)", "mysql", "mysql", false)).toBe("CREATE TABLE `events` (`id` int)");
+ expect(applyDdlDatabaseQualifier('ALTER TABLE "APP"."T1" ADD ("C1" INT);', "dameng", "dameng", false)).toBe('ALTER TABLE "T1" ADD ("C1" INT);');
});
it("drops both qualifiers of CREATE INDEX ... ON and DROP INDEX targets", () => {
const ddl = 'CREATE INDEX "SYSTEM"."IDX_T" ON "SYSTEM"."TEST" ("ABC"); DROP INDEX "SYSTEM"."IDX_T";';
- expect(omitDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe('CREATE INDEX "IDX_T" ON "TEST" ("ABC"); DROP INDEX "IDX_T";');
+ expect(applyDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe('CREATE INDEX "IDX_T" ON "TEST" ("ABC"); DROP INDEX "IDX_T";');
});
it("handles comma-separated DROP TABLE lists and TRUNCATE", () => {
- expect(omitDdlDatabaseQualifier('DROP TABLE "public"."a", "public"."b";', "postgres", "postgres", false)).toBe('DROP TABLE "a", "b";');
- expect(omitDdlDatabaseQualifier('TRUNCATE TABLE "public"."a";', "postgres", "postgres", false)).toBe('TRUNCATE TABLE "a";');
+ expect(applyDdlDatabaseQualifier('DROP TABLE "public"."a", "public"."b";', "postgres", "postgres", false)).toBe('DROP TABLE "a", "b";');
+ expect(applyDdlDatabaseQualifier('TRUNCATE TABLE "public"."a";', "postgres", "postgres", false)).toBe('TRUNCATE TABLE "a";');
});
it("keeps the qualifier when the preference is enabled, the dialect needs it, or the type is unknown", () => {
const ddl = 'ALTER TABLE "SYSTEM"."TEST" ADD ("CDE" CLOB);';
- expect(omitDdlDatabaseQualifier(ddl, "oracle", "oracle", true)).toBe(ddl);
- expect(omitDdlDatabaseQualifier("ALTER TABLE [dbo].[t] ADD [c] int;", "sqlserver", "sqlserver", false)).toBe("ALTER TABLE [dbo].[t] ADD [c] int;");
- expect(omitDdlDatabaseQualifier(ddl, "oracle", undefined, false)).toBe(ddl);
+ expect(applyDdlDatabaseQualifier(ddl, "oracle", "oracle", true)).toBe(ddl);
+ expect(applyDdlDatabaseQualifier("ALTER TABLE [dbo].[t] ADD [c] int;", "sqlserver", "sqlserver", false)).toBe("ALTER TABLE [dbo].[t] ADD [c] int;");
+ expect(applyDdlDatabaseQualifier(ddl, "oracle", undefined, false)).toBe(ddl);
});
it("keeps Doris and StarRocks external-catalog qualifiers", () => {
const dorisDdl = "ALTER TABLE `iceberg`.`analytics`.`events` ADD COLUMN `source` STRING;";
const starrocksDdl = "CREATE TABLE `hive`.`events` (`id` bigint);";
- expect(omitDdlDatabaseQualifier(dorisDdl, "mysql", "doris", false, "iceberg")).toBe(dorisDdl);
- expect(omitDdlDatabaseQualifier(starrocksDdl, "mysql", "starrocks", false, "hive")).toBe(starrocksDdl);
+ expect(applyDdlDatabaseQualifier(dorisDdl, "mysql", "doris", false, undefined, "iceberg")).toBe(dorisDdl);
+ expect(applyDdlDatabaseQualifier(starrocksDdl, "mysql", "starrocks", false, undefined, "hive")).toBe(starrocksDdl);
+ expect(applyDdlDatabaseQualifier("ALTER TABLE `events` ADD COLUMN `source` STRING;", "mysql", "starrocks", true, "analytics", "hive")).toBe("ALTER TABLE `events` ADD COLUMN `source` STRING;");
});
it("drops Doris and StarRocks internal-catalog database qualifiers", () => {
- expect(omitDdlDatabaseQualifier("ALTER TABLE `analytics`.`events` ADD COLUMN `source` STRING;", "mysql", "doris", false, "internal")).toBe("ALTER TABLE `events` ADD COLUMN `source` STRING;");
- expect(omitDdlDatabaseQualifier("CREATE TABLE `analytics`.`events` (`id` bigint);", "mysql", "starrocks", false, "internal")).toBe("CREATE TABLE `events` (`id` bigint);");
+ expect(applyDdlDatabaseQualifier("ALTER TABLE `analytics`.`events` ADD COLUMN `source` STRING;", "mysql", "doris", false, undefined, "internal")).toBe("ALTER TABLE `events` ADD COLUMN `source` STRING;");
+ expect(applyDdlDatabaseQualifier("CREATE TABLE `analytics`.`events` (`id` bigint);", "mysql", "starrocks", false, undefined, "internal")).toBe("CREATE TABLE `events` (`id` bigint);");
});
it("leaves statements it does not understand untouched", () => {
const ddl = "CREATE SEQUENCE SYSTEM.SEQ START WITH 1; SELECT SYSTEM.TEST.ID FROM SYSTEM.TEST;";
- expect(omitDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe(ddl);
+ expect(applyDdlDatabaseQualifier(ddl, "oracle", "oracle", false)).toBe(ddl);
+ });
+
+ // issue #9262: with the preference on, the server never returns the database
+ // for MySQL (`SHOW CREATE TABLE` cannot include it) and SQL Server DDL is
+ // generated as `schema.table`, so the display layer has to add the segment.
+ it("inserts the database prefix into MySQL DDL when the preference is enabled", () => {
+ expect(applyDdlDatabaseQualifier("CREATE TABLE `test1` (`id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)) ENGINE=InnoDB;", "mysql", "mysql", true, "dbx")).toBe("CREATE TABLE `dbx`.`test1` (`id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)) ENGINE=InnoDB;");
+ expect(applyDdlDatabaseQualifier("ALTER TABLE `test1` ADD COLUMN `nick` varchar(20);", "mysql", "mysql", true, "dbx")).toBe("ALTER TABLE `dbx`.`test1` ADD COLUMN `nick` varchar(20);");
+ expect(applyDdlDatabaseQualifier("DROP TABLE `a`, `b`;", "mysql", "mysql", true, "dbx")).toBe("DROP TABLE `dbx`.`a`, `dbx`.`b`;");
+ expect(applyDdlDatabaseQualifier("CREATE INDEX `idx_t` ON `test1` (`name`);", "mysql", "mysql", true, "dbx")).toBe("CREATE INDEX `idx_t` ON `dbx`.`test1` (`name`);");
+ });
+
+ it("inserts the database prefix into SQL Server DDL when the preference is enabled", () => {
+ expect(applyDdlDatabaseQualifier("CREATE TABLE [dbo].[AcceptanceProductLog] ([uID] uniqueidentifier NOT NULL);", "sqlserver", "sqlserver", true, "dbx")).toBe("CREATE TABLE [dbx].[dbo].[AcceptanceProductLog] ([uID] uniqueidentifier NOT NULL);");
+ expect(applyDdlDatabaseQualifier("ALTER TABLE [dbo].[t] ADD [c] int;", "sqlserver", "sqlserver", true, "dbx")).toBe("ALTER TABLE [dbx].[dbo].[t] ADD [c] int;");
+ // The SQL Server DDL generator quotes names that need it.
+ expect(applyDdlDatabaseQualifier("CREATE TABLE [dbo].[player states] ([role id] int);", "sqlserver", "sqlserver", true, "db x")).toBe("CREATE TABLE [db x].[dbo].[player states] ([role id] int);");
+ });
+
+ it("does not double-qualify DDL that already carries the database", () => {
+ const mysqlDdl = "CREATE TABLE `dbx`.`test1` (`id` int);";
+ expect(applyDdlDatabaseQualifier(mysqlDdl, "mysql", "mysql", true, "dbx")).toBe(mysqlDdl);
+ const sqlserverDdl = "CREATE TABLE [dbx].[dbo].[t] ([id] int);";
+ expect(applyDdlDatabaseQualifier(sqlserverDdl, "sqlserver", "sqlserver", true, "dbx")).toBe(sqlserverDdl);
+ });
+
+ it("only completes names whose remaining shape can still be valid", () => {
+ // SQL Server without the schema segment cannot become `database.schema.table`.
+ expect(applyDdlDatabaseQualifier("CREATE TABLE [t] ([id] int);", "sqlserver", "sqlserver", true, "dbx")).toBe("CREATE TABLE [t] ([id] int);");
+ // Engines that never take a leading database segment stay untouched.
+ expect(applyDdlDatabaseQualifier('CREATE TABLE "public"."users" ("id" integer);', "postgres", "postgres", true, "dbx")).toBe('CREATE TABLE "public"."users" ("id" integer);');
+ // No database name resolved — nothing to insert.
+ expect(applyDdlDatabaseQualifier("CREATE TABLE `test1` (`id` int);", "mysql", "mysql", true, undefined)).toBe("CREATE TABLE `test1` (`id` int);");
});
});
diff --git a/apps/desktop/src/lib/sql/ddlDisplay.ts b/apps/desktop/src/lib/sql/ddlDisplay.ts
index 561d9a49ab..b40bd6b2de 100644
--- a/apps/desktop/src/lib/sql/ddlDisplay.ts
+++ b/apps/desktop/src/lib/sql/ddlDisplay.ts
@@ -2,7 +2,7 @@ import { requiresDamengIdentifierQuote, requiresMysqlIdentifierQuote, requiresOr
import { tokenIsIdentifier, tokenizeSqlSemantic, unquoteSqlSemanticIdentifier } from "@/lib/sql/semantic/tokens";
import type { SqlSemanticToken } from "@/lib/sql/semantic/types";
import { sqlFormatDialectForDbType, type SqlFormatDialect } from "@/lib/sql/sqlFormatter";
-import { dropsSchemaQualifier } from "@/lib/table/tableSelectSql";
+import { dropsSchemaQualifier, quoteTableIdentifier } from "@/lib/table/tableSelectSql";
import type { DatabaseType } from "@/types/database";
const SIMPLE_SQLSERVER_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
@@ -153,7 +153,24 @@ function collectDdlQualifierRemoval(tokens: readonly SqlSemanticToken[], parts:
out.push({ start: tokens[parts[0]!]!.span.start, end: tokens[parts[firstKept]!]!.span.start });
}
-function collectDdlStatementQualifierRemovals(tokens: readonly SqlSemanticToken[], from: number, to: number, out: DdlSpan[]): void {
+interface DdlNameTarget {
+ /** Token indexes of every identifier part of the qualified name. */
+ parts: number[];
+ /**
+ * Trailing parts that must survive a rewrite. `COMMENT ON COLUMN t.c` keeps
+ * two (`t.c`), everything else keeps the bare object name.
+ */
+ keep: number;
+ /**
+ * Whether a leading database segment may be *inserted*. Index names are
+ * scoped to their table (`CREATE INDEX db.idx` is invalid on MySQL and
+ * SQL Server), so they stay untouched when the preference is enabled.
+ */
+ qualifiable: boolean;
+}
+
+/** Every qualified name a DDL statement addresses, in source order. */
+function collectDdlStatementNameTargets(tokens: readonly SqlSemanticToken[], from: number, to: number, out: DdlNameTarget[]): void {
let index = from;
while (index < to && tokens[index]!.kind === "comment") index += 1;
const head = tokens[index];
@@ -173,12 +190,12 @@ function collectDdlStatementQualifierRemovals(tokens: readonly SqlSemanticToken[
cursor = skipDdlWords(tokens, cursor + 1, to, ["if", "not", "exists"]);
const nameParts = readDdlQualifiedName(tokens, cursor, to);
if (!nameParts) return;
- collectDdlQualifierRemoval(tokens, nameParts, 1, out);
+ out.push({ parts: nameParts, keep: 1, qualifiable: objectWord.normalized !== "index" });
if (objectWord.normalized === "index") {
const onIndex = findDdlKeyword(tokens, nameParts[nameParts.length - 1]! + 1, to, "on");
if (onIndex < 0) return;
const tableParts = readDdlQualifiedName(tokens, onIndex + 1, to);
- if (tableParts) collectDdlQualifierRemoval(tokens, tableParts, 1, out);
+ if (tableParts) out.push({ parts: tableParts, keep: 1, qualifiable: true });
}
return;
}
@@ -187,7 +204,7 @@ function collectDdlStatementQualifierRemovals(tokens: readonly SqlSemanticToken[
const objectWord = tokens[index + 1];
if (!objectWord || objectWord.kind !== "word" || !DDL_ALTER_OBJECT_WORDS.has(objectWord.normalized)) return;
const nameParts = readDdlQualifiedName(tokens, skipDdlWords(tokens, index + 2, to, ["if", "exists"]), to);
- if (nameParts) collectDdlQualifierRemoval(tokens, nameParts, 1, out);
+ if (nameParts) out.push({ parts: nameParts, keep: 1, qualifiable: objectWord.normalized !== "index" });
return;
}
@@ -198,7 +215,7 @@ function collectDdlStatementQualifierRemovals(tokens: readonly SqlSemanticToken[
for (;;) {
const nameParts = readDdlQualifiedName(tokens, cursor, to);
if (!nameParts) return;
- collectDdlQualifierRemoval(tokens, nameParts, 1, out);
+ out.push({ parts: nameParts, keep: 1, qualifiable: objectWord.normalized !== "index" });
cursor = nameParts[nameParts.length - 1]! + 1;
if (tokens[cursor]?.text !== ",") return;
cursor += 1;
@@ -209,7 +226,7 @@ function collectDdlStatementQualifierRemovals(tokens: readonly SqlSemanticToken[
let cursor = index + 1;
if (tokens[cursor]?.kind === "word" && ["table", "only"].includes(tokens[cursor]!.normalized)) cursor += 1;
const nameParts = readDdlQualifiedName(tokens, cursor, to);
- if (nameParts) collectDdlQualifierRemoval(tokens, nameParts, 1, out);
+ if (nameParts) out.push({ parts: nameParts, keep: 1, qualifiable: true });
return;
}
@@ -221,19 +238,100 @@ function collectDdlStatementQualifierRemovals(tokens: readonly SqlSemanticToken[
if (!isColumn && !DDL_COMMENT_OBJECT_WORDS.has(objectWord.normalized)) return;
const nameParts = readDdlQualifiedName(tokens, index + 3, to);
// `COMMENT ON COLUMN t.c` keeps `t.c`, so only the schema qualifier goes.
- if (nameParts) collectDdlQualifierRemoval(tokens, nameParts, isColumn ? 2 : 1, out);
+ if (nameParts) out.push({ parts: nameParts, keep: isColumn ? 2 : 1, qualifiable: false });
+ }
+}
+
+/** Every qualified name addressed by the DDL statements in `sql`, in source order. */
+function ddlStatementNameTargets(sql: string, dialect: SqlFormatDialect): { targets: DdlNameTarget[]; tokens: readonly SqlSemanticToken[] } | undefined {
+ const tokens = tokenizeSqlSemantic(sql, dialect === "sqlserver" ? "sqlserver" : dialect);
+ if (tokens.some((token) => token.closed === false)) return undefined;
+ const targets: DdlNameTarget[] = [];
+ let statementStart = 0;
+ for (let index = 0; index <= tokens.length; index += 1) {
+ const token = tokens[index];
+ if (token && !(token.kind === "punctuation" && token.text === ";" && token.depth === 0)) continue;
+ collectDdlStatementNameTargets(tokens, statementStart, index, targets);
+ statementStart = index + 1;
+ }
+ return { targets, tokens };
+}
+
+function collectDdlStatementQualifierRemovals(tokens: readonly SqlSemanticToken[], from: number, to: number, out: DdlSpan[]): void {
+ const targets: DdlNameTarget[] = [];
+ collectDdlStatementNameTargets(tokens, from, to, targets);
+ for (const target of targets) collectDdlQualifierRemoval(tokens, target.parts, target.keep, out);
+}
+
+function applyDdlSpans(sql: string, spans: readonly { start: number; end: number; text?: string }[]): string {
+ const ordered = [...spans].sort((left, right) => left.start - right.start);
+ let result = sql;
+ for (let index = ordered.length - 1; index >= 0; index -= 1) {
+ const span = ordered[index]!;
+ result = `${result.slice(0, span.start)}${span.text ?? ""}${result.slice(span.end)}`;
+ }
+ return result;
+}
+
+/** Engines whose fully qualified table name is `database.table`. */
+const DDL_DATABASE_PREFIXED_TYPES = new Set<DatabaseType>(["mysql", "goldendb", "clickhouse"]);
+/** Engines whose fully qualified table name is `database.schema.table`. */
+const DDL_DATABASE_SCHEMA_PREFIXED_TYPES = new Set<DatabaseType>(["sqlserver"]);
+
+/**
+ * Number of parts a fully qualified target name has on this engine, or
+ * `undefined` when the engine cannot take a leading database segment (then the
+ * DDL stays untouched in both directions).
+ */
+function ddlQualifiedPartCount(databaseType: DatabaseType | undefined, catalog?: string): number | undefined {
+ if (databaseType === undefined) return undefined;
+ if (DDL_DATABASE_SCHEMA_PREFIXED_TYPES.has(databaseType)) return 3;
+ if (DDL_DATABASE_PREFIXED_TYPES.has(databaseType)) return 2;
+ if (databaseType === "doris" || databaseType === "starrocks") {
+ // An external catalog already supplies the leading segment
+ // (`catalog.database.table`), so the DDL must not grow a second one.
+ const external = catalog?.trim();
+ return !external || external === "internal" ? 2 : undefined;
}
+ return undefined;
}
/**
- * Drops the schema/database qualifier from the target object of DDL statements
- * (`CREATE/ALTER/DROP TABLE`, `CREATE INDEX ... ON`, `COMMENT ON ...`,
- * `TRUNCATE TABLE`) so table DDL honors the "Include database name in generated
- * SQL" preference the same way generated SELECT and copy-as-INSERT SQL do
- * (#9421). Databases whose objects cannot be addressed without their qualifier
- * keep it, mirroring `dropsSchemaQualifier`.
+ * Applies the "Include database name in generated SQL" preference to the target
+ * object of DDL statements (`CREATE/ALTER/DROP TABLE`, `CREATE INDEX ... ON`,
+ * `COMMENT ON ...`, `TRUNCATE TABLE`), the same way generated SELECT and
+ * copy-as-INSERT SQL honor it.
+ *
+ * With the preference off the schema/database qualifier is dropped (#9421).
+ * With it on, engines whose server never returns the database — MySQL's
+ * `SHOW CREATE TABLE` cannot include it — and SQL Server, whose generated DDL
+ * stops at `schema.table`, get the leading `database` segment inserted, so a
+ * SQL Server table reads `[dbx].[dbo].[t]` (#9262). Databases whose objects
+ * cannot be addressed without their qualifier keep it, mirroring
+ * `dropsSchemaQualifier`.
*/
-export function omitDdlDatabaseQualifier(sql: string, dialect: SqlFormatDialect, databaseType: DatabaseType | undefined, includeDatabaseName: boolean, catalog?: string): string {
+export function applyDdlDatabaseQualifier(sql: string, dialect: SqlFormatDialect, databaseType: DatabaseType | undefined, includeDatabaseName: boolean, database?: string, catalog?: string): string {
+ if (includeDatabaseName) {
+ const qualifiedParts = ddlQualifiedPartCount(databaseType, catalog);
+ const databaseName = database?.trim();
+ if (qualifiedParts === undefined || !databaseName) return sql;
+ const parsed = ddlStatementNameTargets(sql, dialect);
+ if (!parsed) return sql;
+ const prefix = `${quoteTableIdentifier(databaseType, databaseName)}.`;
+ const insertions: Array<DdlSpan & { text: string }> = [];
+ for (const target of parsed.targets) {
+ // Only a table-like target can be completed: adding a segment to a bare
+ // or half-qualified name would invent a namespace the DDL never named,
+ // and a `COMMENT ON COLUMN t.c` target is not a database-addressable
+ // object.
+ if (!target.qualifiable || target.keep !== 1 || target.parts.length !== qualifiedParts - 1) continue;
+ const at = parsed.tokens[target.parts[0]!]!.span.start;
+ insertions.push({ start: at, end: at, text: prefix });
+ }
+ if (insertions.length === 0) return sql;
+ return applyDdlSpans(sql, insertions);
+ }
+
if (!dropsSchemaQualifier(databaseType, includeDatabaseName, catalog)) return sql;
const tokens = tokenizeSqlSemantic(sql, dialect === "sqlserver" ? "sqlserver" : dialect);
if (tokens.some((token) => token.closed === false)) return sql;
@@ -248,11 +346,5 @@ export function omitDdlDatabaseQualifier(sql: string, dialect: SqlFormatDialect,
}
if (removals.length === 0) return sql;
- removals.sort((left, right) => left.start - right.start);
- let result = sql;
- for (let index = removals.length - 1; index >= 0; index -= 1) {
- const removal = removals[index]!;
- result = `${result.slice(0, removal.start)}${result.slice(removal.end)}`;
- }
- return result;
+ return applyDdlSpans(sql, removals);
}
diff --git a/apps/desktop/src/lib/table/tableSelectSql.ts b/apps/desktop/src/lib/table/tableSelectSql.ts
index 7af12302e9..6aa03e55ef 100644
--- a/apps/desktop/src/lib/table/tableSelectSql.ts
+++ b/apps/desktop/src/lib/table/tableSelectSql.ts
@@ -40,6 +40,11 @@ export interface BuildTableSelectSqlOptions {
const DATABASE_QUALIFIED_TABLE_TYPES = new Set<DatabaseType>(["mysql", "clickhouse", "doris", "starrocks", "goldendb"]);
+// SQL Server is the one engine whose generated SQL needs both namespaces at
+// once: `database.schema.table`. The other engines above address a table with a
+// single additional segment (`database.table`).
+const DATABASE_SCHEMA_PREFIXED_TABLE_TYPES = new Set<DatabaseType>(["sqlserver"]);
+
// `includeDatabaseName === false` drops the schema qualifier — the "database
// name" on schema-aware engines — except for databases that can only address
// objects through their full qualified name (`catalog.schema.table` /
@@ -257,6 +262,14 @@ export function qualifiedTableName(options: Pick<BuildTableSelectSqlOptions, "da
if (linked) {
return quoteIdentifiers === false ? [linked.server, linked.catalog, linked.schema, tableName].map((name) => quoteTableIdentifierIfNeeded(databaseType, name)).join(".") : sqlServerLinkedTableName(linked, tableName);
}
+ // issue #9262: SQL Server can address every table on the connection as
+ // `database.schema.table`, and the setting opts that three-part form in.
+ // A linked-server schema already carries `server|catalog|schema`, so it
+ // must never gain the local database on top.
+ const trimmedDatabase = includeDatabaseName ? database?.trim() : undefined;
+ if (trimmedDatabase) {
+ return `${quoteTable(trimmedDatabase)}.${quoteTable(schema)}.${quoteTable(tableName)}`;
+ }
}
// The schema qualifier is the "database name" on schema-aware engines
// (Oracle's SYSTEM, PG's public, ...). `dropsSchemaQualifier` keeps it
@@ -328,20 +341,47 @@ function sqlCteVisibilities(tokens: readonly SqlSemanticToken[], sqlLength: numb
return visibilities;
}
+/**
+ * Counts the qualifier segments a table name already carries and reports where a
+ * database prefix has to be inserted. `sqlSemanticTableNameSpans` only exposes
+ * the final segment, so the qualifier chain is rebuilt from the surrounding
+ * tokens (`[dbo].[t]` -> one segment starting at `[dbo]`).
+ */
+function sqlTableNameQualifier(tokens: readonly SqlSemanticToken[], index: number): { parts: number; start: number } {
+ let parts = 0;
+ let cursor = index;
+ let start = tokens[index]?.span.start ?? 0;
+ while (cursor >= 2 && tokens[cursor - 1]?.text === "." && tokenIsIdentifier(tokens[cursor - 2])) {
+ cursor -= 2;
+ parts += 1;
+ start = tokens[cursor]?.span.start ?? start;
+ }
+ return { parts, start };
+}
+
/**
* Qualifies physical table sources shown in a result footer without changing
* the SQL that was actually executed. The semantic model deliberately skips
* CTE names, strings, and comments that can happen to contain FROM/JOIN text.
+ *
+ * MySQL-family engines have the active database inserted in front of a one-part
+ * table name. SQL Server instead keeps the schema it already names and gains the
+ * database in front of it (`[dbo].[t]` -> `[db].[dbo].[t]`), because
+ * `db.table` is not a valid SQL Server reference. Names that omit the schema or
+ * already carry a database stay untouched.
*/
export function qualifyTableReferencesInSql(sql: string, options: Pick<BuildTableSelectSqlOptions, "databaseType" | "database" | "includeDatabaseName">): string {
- if (!options.includeDatabaseName || !options.databaseType || !DATABASE_QUALIFIED_TABLE_TYPES.has(options.databaseType) || !options.database?.trim()) return sql;
- const database = quoteTableIdentifier(options.databaseType, options.database.trim());
+ const databaseType = options.databaseType;
+ if (!options.includeDatabaseName || !databaseType || !options.database?.trim()) return sql;
+ const schemaPrefixed = DATABASE_SCHEMA_PREFIXED_TABLE_TYPES.has(databaseType);
+ if (!schemaPrefixed && !DATABASE_QUALIFIED_TABLE_TYPES.has(databaseType)) return sql;
+ const database = quoteTableIdentifier(databaseType, options.database.trim());
// Build replacements from right to left so that every semantic span still
- // points at the original source text. Only one-part physical table names
- // need the active database prefix; CTEs and already-qualified tables do not.
+ // points at the original source text. CTEs and already-qualified tables never
+ // gain a prefix.
const semanticOptions = {
- databaseType: options.databaseType,
- dialect: options.databaseType === "goldendb" ? "mysql" : undefined,
+ databaseType,
+ dialect: databaseType === "goldendb" ? "mysql" : undefined,
} as const;
const dialectId = sqlSemanticDialectFor(semanticOptions).id;
const replacements = sqlStatementSpans(sql, dialectId)
@@ -350,25 +390,27 @@ export function qualifyTableReferencesInSql(sql: string, options: Pick<BuildTabl
const tokens = tokenizeSqlSemantic(statementSql, dialectId);
const cteVisibilities = sqlCteVisibilities(tokens, statementSql.length);
const isCteReference = (name: string, span: { start: number; end: number }): boolean => cteVisibilities.some((cte) => cte.name.toLowerCase() === name.toLowerCase() && span.start >= cte.visibleFrom && span.end <= cte.visibleUntil);
- const tokensBySpan = new Map(tokens.map((token) => [`${token.span.start}:${token.span.end}`, token]));
+ const tokenIndexBySpan = new Map(tokens.map((token, index) => [`${token.span.start}:${token.span.end}`, index]));
return sqlSemanticTableNameSpans(statementSql, semanticOptions)
- .map((span) => ({ span, token: tokensBySpan.get(`${span.start}:${span.end}`) }))
- .filter(({ span, token }) => {
- if (!token || isCteReference(unquoteSqlSemanticIdentifier(token), span)) return false;
- // sqlSemanticTableNameSpans returns the final segment in a qualified
- // name, so a preceding dot identifies a database-qualified source.
- return !statementSql.slice(0, span.start).trimEnd().endsWith(".");
+ .map((span) => ({ span, index: tokenIndexBySpan.get(`${span.start}:${span.end}`) }))
+ .filter(({ span, index }) => {
+ if (index === undefined || isCteReference(unquoteSqlSemanticIdentifier(tokens[index]!), span)) return false;
+ return schemaPrefixed ? sqlTableNameQualifier(tokens, index).parts === 1 : sqlTableNameQualifier(tokens, index).parts === 0;
})
- .map(({ span, token }) => ({
- span: { start: start + span.start, end: start + span.end },
- tableName: unquoteSqlSemanticIdentifier(token!),
- }));
+ .map(({ span, index }) => {
+ const tableName = unquoteSqlSemanticIdentifier(tokens[index!]!);
+ if (schemaPrefixed) {
+ const qualifier = sqlTableNameQualifier(tokens, index!);
+ return { start: start + qualifier.start, end: start + qualifier.start, replacement: `${database}.` };
+ }
+ return { start: start + span.start, end: start + span.end, replacement: `${database}.${quoteTableIdentifier(databaseType, tableName)}` };
+ });
})
- .filter(({ span }, index, all) => all.findIndex((candidate) => candidate.span.start === span.start && candidate.span.end === span.end) === index)
- .sort((left, right) => right.span.start - left.span.start);
+ .filter(({ start }, index, all) => all.findIndex((candidate) => candidate.start === start) === index)
+ .sort((left, right) => right.start - left.start);
- return replacements.reduce((qualifiedSql, { span, tableName }) => `${qualifiedSql.slice(0, span.start)}${database}.${quoteTableIdentifier(options.databaseType, tableName)}${qualifiedSql.slice(span.end)}`, sql);
+ return replacements.reduce((qualifiedSql, { start, end, replacement }) => `${qualifiedSql.slice(0, start)}${replacement}${qualifiedSql.slice(end)}`, sql);
}
export function metricSelector(metricName: string): string {
diff --git a/crates/dbx-core/tests/mysql_cross_database_editing.rs b/crates/dbx-core/tests/mysql_cross_database_editing.rs
index a60a7214b5..56fc08b9ec 100644
--- a/crates/dbx-core/tests/mysql_cross_database_editing.rs
+++ b/crates/dbx-core/tests/mysql_cross_database_editing.rs
@@ -86,6 +86,7 @@ fn mysql_cross_database_query_flow_preserves_target_database() {
dirty_rows: vec![(0, vec![(1, json!("after"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(save.execution_schema.as_deref(), Some("db_9"));
diff --git a/crates/dbx-sql/src/data_grid_extractors/sql.rs b/crates/dbx-sql/src/data_grid_extractors/sql.rs
index f792f85fa7..2218b5bc48 100644
--- a/crates/dbx-sql/src/data_grid_extractors/sql.rs
+++ b/crates/dbx-sql/src/data_grid_extractors/sql.rs
@@ -5,7 +5,7 @@ use super::{
};
use crate::data_grid_sql::{
build_column_predicate, build_data_grid_copy_insert_statement, build_data_grid_copy_update_statements,
- data_grid_qualified_table_name, format_grid_sql_literal_with_identifier_quote, is_auto_generated_column,
+ data_grid_generated_table_name, format_grid_sql_literal_with_identifier_quote, is_auto_generated_column,
is_grid_insert_omitted_column, is_non_identity_generated_column, supports_relational_copy_predicates,
DataGridCopyInsertStatementOptions, DataGridCopyUpdateStatementOptions, DataGridTableMeta,
};
@@ -121,6 +121,7 @@ pub(super) fn write_sql_updates(
columns: data.columns,
source_columns: Some(data.source_columns),
rows: data.rows,
+ include_database_name: context.request.options.sql.include_database_name,
});
if statements.len() != context.request.rows.len() {
return Err(DataGridExtractError::new(
@@ -371,13 +372,14 @@ pub(super) fn write_sql_select(
));
}
- let table = data_grid_qualified_table_name(
+ let table = data_grid_generated_table_name(
context.request.database_type,
table_meta.catalog.as_deref(),
table_meta.schema.as_deref(),
table_meta.database.as_deref(),
&table_meta.table_name,
context.request.identifier_quote.as_deref(),
+ context.request.options.sql.include_database_name,
);
if context.request.selection_kind == super::DataGridSelectionKind::Cells {
diff --git a/crates/dbx-sql/src/data_grid_extractors/tests.rs b/crates/dbx-sql/src/data_grid_extractors/tests.rs
index f35a9237ac..45a8973d9b 100644
--- a/crates/dbx-sql/src/data_grid_extractors/tests.rs
+++ b/crates/dbx-sql/src/data_grid_extractors/tests.rs
@@ -934,6 +934,7 @@ fn build_data_grid_copy_update_statements_returns_empty_for_mongodb() {
columns: vec!["id".to_string()],
source_columns: Some(vec![Some("id".to_string())]),
rows: vec![vec![json!(1)]],
+ include_database_name: false,
};
assert!(build_data_grid_copy_update_statements(options).is_empty());
}
diff --git a/crates/dbx-sql/src/data_grid_iotdb_sql.rs b/crates/dbx-sql/src/data_grid_iotdb_sql.rs
index 39207359f0..83915a9d5b 100644
--- a/crates/dbx-sql/src/data_grid_iotdb_sql.rs
+++ b/crates/dbx-sql/src/data_grid_iotdb_sql.rs
@@ -268,6 +268,7 @@ mod tests {
dirty_rows: Vec::new(),
deleted_rows: Vec::new(),
new_rows: Vec::new(),
+ include_database_name: false,
}
}
diff --git a/crates/dbx-sql/src/data_grid_sql.rs b/crates/dbx-sql/src/data_grid_sql.rs
index 4a404801d4..3401fcedcf 100644
--- a/crates/dbx-sql/src/data_grid_sql.rs
+++ b/crates/dbx-sql/src/data_grid_sql.rs
@@ -97,6 +97,11 @@ pub struct DataGridSaveStatementOptions {
pub deleted_rows: Vec<usize>,
#[serde(default)]
pub new_rows: Vec<Vec<Value>>,
+ /// `生成 SQL 时包含数据库名`: qualify the saved table with its database on
+ /// engines that address tables as `database.table` (MySQL family,
+ /// ClickHouse). Off by default so existing statements are unchanged.
+ #[serde(default)]
+ pub include_database_name: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -112,6 +117,11 @@ pub struct DataGridCopyUpdateStatementOptions {
pub source_columns: Option<Vec<Option<String>>>,
#[serde(default)]
pub rows: Vec<Vec<Value>>,
+ /// `生成 SQL 时包含数据库名`: qualify the copied table the same way the
+ /// save statements and the copy-as-INSERT statements do. Defaults to true so
+ /// callers that never strip the table metadata keep the historical shape.
+ #[serde(default = "default_include_database_name")]
+ pub include_database_name: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -423,13 +433,14 @@ pub fn build_data_grid_copy_update_statements(options: DataGridCopyUpdateStateme
return Vec::new();
}
- let table = data_grid_qualified_table_name(
+ let table = data_grid_generated_table_name(
options.database_type,
options.table_meta.catalog.as_deref(),
options.table_meta.schema.as_deref(),
options.table_meta.database.as_deref(),
&options.table_meta.table_name,
options.identifier_quote.as_deref(),
+ options.include_database_name,
);
let mut statements = Vec::new();
for row in &options.rows {
@@ -554,19 +565,17 @@ pub fn build_data_grid_copy_insert_statement(options: DataGridCopyInsertStatemen
} else {
meta.schema.as_deref()
};
- // ClickHouse uses a database qualifier rather than SQL schemas.
- if options.database_type == Some(DatabaseType::ClickHouse) {
- if let Some(database) = schema {
- return format!(
- "{}.{}",
- data_grid_identifier(options.database_type, database, options.identifier_quote.as_deref()),
- data_grid_identifier(
- options.database_type,
- &meta.table_name,
- options.identifier_quote.as_deref()
- ),
- );
- }
+ // ClickHouse (`database.table`) and SQL Server
+ // (`database.schema.table`) address tables across databases on the
+ // same connection, so the setting resolves the full name for them.
+ if let Some(qualified) = crate::sql_dialect::database_qualified_table_name(
+ options.database_type,
+ meta.catalog.as_deref(),
+ schema,
+ meta.database.as_deref(),
+ &meta.table_name,
+ ) {
+ return qualified;
}
data_grid_qualified_table_name(
options.database_type,
@@ -1463,14 +1472,7 @@ fn build_data_grid_save_statements(
driver_profile,
options.table_meta.schema.as_deref(),
);
- let table = data_grid_qualified_table_name(
- options.database_type,
- options.table_meta.catalog.as_deref(),
- schema,
- options.table_meta.database.as_deref(),
- &options.table_meta.table_name,
- options.identifier_quote.as_deref(),
- );
+ let table = data_grid_save_table_name(options, schema);
let mut statements = Vec::new();
let primary_key_set: Vec<String> =
options.table_meta.primary_keys.iter().map(|primary_key| normalize_column_name(primary_key)).collect();
@@ -1668,14 +1670,7 @@ fn build_data_grid_rollback_statements(
driver_profile,
options.table_meta.schema.as_deref(),
);
- let table = data_grid_qualified_table_name(
- options.database_type,
- options.table_meta.catalog.as_deref(),
- schema,
- options.table_meta.database.as_deref(),
- &options.table_meta.table_name,
- options.identifier_quote.as_deref(),
- );
+ let table = data_grid_save_table_name(options, schema);
let mut statements = Vec::new();
for row in &options.new_rows {
@@ -3507,6 +3502,56 @@ fn data_grid_identifier(database_type: Option<DatabaseType>, name: &str, identif
crate::sql_dialect::quote_table_data_identifier(database_type, name, identifier_quote)
}
+/// Table reference for every generated data-grid SQL surface that honors the
+/// `生成 SQL 时包含数据库名` setting: save / rollback / keyless-guard statements
+/// and the copy-as-INSERT/UPDATE/SELECT statements.
+///
+/// With the setting on, engines whose active database is normally omitted get a
+/// fully qualified name (`database.table`, or `database.schema.table` for SQL
+/// Server, whose tables stay addressable across databases). Every other engine —
+/// and the setting off — keeps the historical shape, so no existing SQL changes
+/// shape unless the user opted in.
+#[allow(clippy::too_many_arguments)]
+pub fn data_grid_generated_table_name(
+ database_type: Option<DatabaseType>,
+ catalog: Option<&str>,
+ schema: Option<&str>,
+ database: Option<&str>,
+ table_name: &str,
+ identifier_quote: Option<&str>,
+ include_database_name: bool,
+) -> String {
+ if include_database_name {
+ if let Some(qualified) =
+ crate::sql_dialect::database_qualified_table_name(database_type, catalog, schema, database, table_name)
+ {
+ return qualified;
+ }
+ }
+ data_grid_qualified_table_name(database_type, catalog, schema, database, table_name, identifier_quote)
+}
+
+/// Table reference for the grid's save / rollback / keyless-guard statements.
+///
+/// Mirrors the data-table SELECT label and the copy-as-INSERT statements: when
+/// `include_database_name` is on, engines addressed via `database.table`
+/// (MySQL family, ClickHouse) get the database prefix — taken from
+/// `table_meta.schema` when the table lives outside the connection's default
+/// database, otherwise from `table_meta.database` — and SQL Server gets the
+/// three-part `database.schema.table` form. Every other engine keeps the
+/// historical shape.
+fn data_grid_save_table_name(options: &DataGridSaveStatementOptions, schema: Option<&str>) -> String {
+ data_grid_generated_table_name(
+ options.database_type,
+ options.table_meta.catalog.as_deref(),
+ schema,
+ options.table_meta.database.as_deref(),
+ &options.table_meta.table_name,
+ options.identifier_quote.as_deref(),
+ options.include_database_name,
+ )
+}
+
pub fn data_grid_qualified_table_name(
database_type: Option<DatabaseType>,
catalog: Option<&str>,
@@ -3766,9 +3811,154 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
}
}
+ /// A MySQL grid tab keeps its namespace in `table_meta.database` (not
+ /// `schema`), so the save statements are the one generated-SQL surface that
+ /// never picked up `生成 SQL 时包含数据库名`. It must match the data-table
+ /// SELECT label and the copy-as-INSERT statements.
+ #[test]
+ fn mysql_data_grid_save_honors_include_database_name() {
+ let mut options = mysql_people_save_options(1);
+ options.table_meta.database = Some("appdb".to_string());
+ options.table_meta.schema = None;
+ options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
+
+ options.include_database_name = true;
+ let qualified = prepare_data_grid_save(options.clone());
+ assert_eq!(qualified.validation_error, None);
+ assert_eq!(qualified.statements, vec!["UPDATE `appdb`.`people` SET `status` = 'blocked' WHERE `id` = 1;"]);
+ assert_eq!(
+ qualified.rollback_statements,
+ vec!["UPDATE `appdb`.`people` SET `status` = 'active' WHERE `id` = 1 AND BINARY `status` = 'blocked';"]
+ );
+
+ options.include_database_name = false;
+ let bare = prepare_data_grid_save(options);
+ assert_eq!(bare.statements, vec!["UPDATE `people` SET `status` = 'blocked' WHERE `id` = 1;"]);
+ assert_eq!(
+ bare.rollback_statements,
+ vec!["UPDATE `people` SET `status` = 'active' WHERE `id` = 1 AND BINARY `status` = 'blocked';"]
+ );
+ }
+
+ #[test]
+ fn mysql_data_grid_save_qualifies_insert_delete_and_keyless_guard() {
+ let mut options = mysql_people_save_options(0);
+ options.table_meta.database = Some("appdb".to_string());
+ options.table_meta.schema = None;
+ options.include_database_name = true;
+ options.new_rows = vec![vec![json!(7), json!("created")]];
+ options.deleted_rows = vec![];
+
+ let inserted = prepare_data_grid_save(options.clone());
+ assert_eq!(inserted.statements, vec!["INSERT INTO `appdb`.`people` (`id`, `status`) VALUES (7, 'created');"]);
+
+ let mut keyless = options;
+ keyless.new_rows = vec![];
+ keyless.table_meta.primary_keys = vec![];
+ keyless.deleted_rows = vec![0];
+ let deleted = prepare_data_grid_save(keyless);
+ assert!(
+ deleted.statements.iter().all(|statement| statement.contains("`appdb`.`people`")),
+ "every statement must carry the database qualifier: {:?}",
+ deleted.statements
+ );
+ assert!(
+ deleted.keyless_guards.iter().all(|guard| guard.sql.contains("`appdb`.`people`")),
+ "the keyless guard counts rows in the same table reference: {:?}",
+ deleted.keyless_guards
+ );
+ }
+
+ /// Engines that already address tables through `schema.table` keep their
+ /// shape — the flag only adds the namespace those dialects cannot express.
+ #[test]
+ fn data_grid_save_leaves_schema_qualified_engines_unchanged() {
+ let mut options = mysql_people_save_options(1);
+ options.database_type = Some(DatabaseType::Postgres);
+ options.identifier_quote = None;
+ options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
+ options.include_database_name = true;
+
+ let result = prepare_data_grid_save(options);
+ assert_eq!(result.statements, vec!["UPDATE \"app\".\"people\" SET \"status\" = 'blocked' WHERE \"id\" = 1;"]);
+ }
+
+ /// A cross-database editable result (`SELECT * FROM db_9.users`) keeps its own
+ /// namespace in `schema` while `database` still holds the connection's default
+ /// database — the qualifier must follow the table, not the connection.
+ #[test]
+ fn mysql_data_grid_save_prefers_cross_database_schema() {
+ let mut options = mysql_people_save_options(1);
+ options.table_meta.schema = Some("db_9".to_string());
+ options.table_meta.database = Some("appdb".to_string());
+ options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
+ options.include_database_name = true;
+
+ let result = prepare_data_grid_save(options);
+ assert_eq!(result.statements, vec!["UPDATE `db_9`.`people` SET `status` = 'blocked' WHERE `id` = 1;"]);
+ assert_eq!(result.execution_schema.as_deref(), Some("db_9"));
+ }
+
+ /// issue #9262: SQL Server tables are addressable as
+ /// `database.schema.table`, so the setting must reach the three-part form on
+ /// the save / rollback / keyless-guard statements too.
+ #[test]
+ fn sqlserver_data_grid_save_honors_include_database_name() {
+ let mut options = mysql_people_save_options(1);
+ options.database_type = Some(DatabaseType::SqlServer);
+ options.table_meta.schema = Some("dbo".to_string());
+ options.table_meta.database = Some("dbx".to_string());
+ options.dirty_rows = vec![(0, vec![(1, json!("blocked"))])];
+
+ options.include_database_name = true;
+ let qualified = prepare_data_grid_save(options.clone());
+ assert_eq!(qualified.validation_error, None);
+ assert_eq!(qualified.statements, vec!["UPDATE [dbx].[dbo].[people] SET [status] = N'blocked' WHERE [id] = 1;"]);
+ assert_eq!(
+ qualified.rollback_statements,
+ vec!["UPDATE [dbx].[dbo].[people] SET [status] = N'active' WHERE [id] = 1 AND [status] = N'blocked';"]
+ );
+
+ options.include_database_name = false;
+ let bare = prepare_data_grid_save(options);
+ assert_eq!(bare.statements, vec!["UPDATE [dbo].[people] SET [status] = N'blocked' WHERE [id] = 1;"]);
+ }
+
+ #[test]
+ fn sqlserver_data_grid_save_qualifies_insert_and_keyless_guard() {
+ let mut options = mysql_people_save_options(0);
+ options.database_type = Some(DatabaseType::SqlServer);
+ options.table_meta.schema = Some("dbo".to_string());
+ options.table_meta.database = Some("dbx".to_string());
+ options.include_database_name = true;
+ options.new_rows = vec![vec![json!(7), json!("created")]];
+ let inserted = prepare_data_grid_save(options.clone());
+ assert_eq!(
+ inserted.statements,
+ vec!["INSERT INTO [dbx].[dbo].[people] ([id], [status]) VALUES (7, N'created');"]
+ );
+
+ let mut keyless = options;
+ keyless.new_rows = vec![];
+ keyless.table_meta.primary_keys = vec![];
+ keyless.deleted_rows = vec![0];
+ let deleted = prepare_data_grid_save(keyless);
+ assert!(
+ deleted.statements.iter().all(|statement| statement.contains("[dbx].[dbo].[people]")),
+ "every statement must carry the three-part name: {:?}",
+ deleted.statements
+ );
+ assert!(
+ deleted.keyless_guards.iter().all(|guard| guard.sql.contains("[dbx].[dbo].[people]")),
+ "the keyless guard counts rows in the same table reference: {:?}",
+ deleted.keyless_guards
+ );
+ }
+
#[test]
fn iris_data_grid_count_queries_the_table_without_wrapping_top_sql() {
assert_eq!(
@@ -3847,6 +4037,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("210101202201016555"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -3883,6 +4074,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0],
new_rows: vec![vec![json!(52), json!("inserted")]],
+ include_database_name: false,
});
assert_eq!(
@@ -3926,6 +4118,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("after"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -3968,6 +4161,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("1"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -4030,6 +4224,7 @@ mod tests {
columns: vec!["id".to_string(), "name".to_string(), "status".to_string()],
source_columns: None,
rows: vec![vec![json!(1), json!("Ada"), json!("active")]],
+ include_database_name: false,
});
assert_eq!(
statements,
@@ -4037,6 +4232,66 @@ mod tests {
);
}
+ /// The right-click `复制 → SQL UPDATE 语句` path must honor
+ /// `生成 SQL 时包含数据库名` exactly like the copy-as-INSERT statements do
+ /// (issue #9262).
+ #[test]
+ fn copy_update_honors_include_database_name() {
+ let options = DataGridCopyUpdateStatementOptions {
+ database_type: Some(DatabaseType::Mysql),
+ identifier_quote: None,
+ table_meta: DataGridTableMeta {
+ catalog: None,
+ database: Some("appdb".to_string()),
+ schema: None,
+ table_name: "people".to_string(),
+ primary_keys: vec!["id".to_string()],
+ columns: None,
+ },
+ columns: vec!["id".to_string(), "status".to_string()],
+ source_columns: None,
+ rows: vec![vec![json!(1), json!("blocked")]],
+ include_database_name: true,
+ };
+ assert_eq!(
+ build_data_grid_copy_update_statements(options.clone()),
+ vec!["UPDATE `appdb`.`people` SET `status` = 'blocked' WHERE `id` = 1;"]
+ );
+ // The frontend strips the metadata when the setting is off, but the
+ // option itself must also fall back to the historical bare shape.
+ assert_eq!(
+ build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
+ include_database_name: false,
+ ..options
+ }),
+ vec!["UPDATE `people` SET `status` = 'blocked' WHERE `id` = 1;"]
+ );
+ }
+
+ #[test]
+ fn sqlserver_copy_update_uses_three_part_name() {
+ let options = DataGridCopyUpdateStatementOptions {
+ database_type: Some(DatabaseType::SqlServer),
+ identifier_quote: None,
+ table_meta: DataGridTableMeta {
+ catalog: None,
+ database: Some("dbx".to_string()),
+ schema: Some("dbo".to_string()),
+ table_name: "player states".to_string(),
+ primary_keys: vec!["role id".to_string()],
+ columns: None,
+ },
+ columns: vec!["role id".to_string(), "state".to_string()],
+ source_columns: None,
+ rows: vec![vec![json!(42), json!("ready")]],
+ include_database_name: true,
+ };
+ assert_eq!(
+ build_data_grid_copy_update_statements(options),
+ vec!["UPDATE [dbx].[dbo].[player states] SET [state] = N'ready' WHERE [role id] = 42;"]
+ );
+ }
+
#[test]
fn builds_copy_insert_statement_without_primary_keys() {
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
@@ -4259,6 +4514,7 @@ mod tests {
columns: vec!["identifier".to_string(), "label".to_string()],
source_columns: Some(vec![Some("id".to_string()), Some("display_name".to_string())]),
rows: vec![vec![json!(1), json!("Ada")]],
+ include_database_name: false,
});
assert_eq!(statements, vec!["UPDATE `users` SET `display_name` = 'Ada' WHERE `id` = 1;"]);
}
@@ -4487,6 +4743,7 @@ mod tests {
vec![json!(2), json!({"nested": [1, 2]})],
vec![json!(3), json!([])],
],
+ include_database_name: false,
});
assert_eq!(
@@ -4515,6 +4772,7 @@ mod tests {
columns: vec!["id".to_string(), "payload".to_string()],
source_columns: None,
rows: vec![vec![json!(1), json!(r#"[1,2]"#)], vec![json!(2), json!(true)], vec![json!(3), Value::Null]],
+ include_database_name: false,
});
assert_eq!(
json_statements,
@@ -4539,6 +4797,7 @@ mod tests {
columns: vec!["id".to_string(), "payload".to_string()],
source_columns: None,
rows: vec![vec![json!(1), json!([1, 2])]],
+ include_database_name: false,
});
assert_eq!(generic_statements, vec!["UPDATE `arrays` SET `payload` = '{1,2}' WHERE `id` = 1;"]);
@@ -4681,7 +4940,7 @@ mod tests {
assert_eq!(
statement.as_deref(),
Some(
- "SET IDENTITY_INSERT [dbo].[gen_table] ON;\nINSERT INTO [dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');\nSET IDENTITY_INSERT [dbo].[gen_table] OFF;"
+ "SET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] ON;\nINSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] OFF;"
)
);
}
@@ -4695,7 +4954,7 @@ mod tests {
assert_eq!(
statement.as_deref(),
Some(
- "SET IDENTITY_INSERT [dbo].[gen_table] ON;\nINSERT INTO [dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');\nSET IDENTITY_INSERT [dbo].[gen_table] OFF;\nSET IDENTITY_INSERT [dbo].[gen_table] ON;\nINSERT INTO [dbo].[gen_table] ([table_id], [table_name]) VALUES (2, N't_user');\nSET IDENTITY_INSERT [dbo].[gen_table] OFF;"
+ "SET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] ON;\nINSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] OFF;\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] ON;\nINSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (2, N't_user');\nSET IDENTITY_INSERT [dbx_test].[dbo].[gen_table] OFF;"
)
);
}
@@ -4711,7 +4970,7 @@ mod tests {
let statement = build_data_grid_copy_insert_statement(options);
assert_eq!(
statement.as_deref(),
- Some("INSERT INTO [dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');")
+ Some("INSERT INTO [dbx_test].[dbo].[gen_table] ([table_id], [table_name]) VALUES (1, N't_destype');")
);
}
@@ -4769,6 +5028,7 @@ mod tests {
columns,
source_columns: None,
rows,
+ include_database_name: false,
});
assert_eq!(
updates,
@@ -5940,6 +6200,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!(4), Value::Null, Value::Null, Value::Null]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -5969,6 +6230,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Ada Lovelace"))])],
deleted_rows: vec![1],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6007,6 +6269,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!(7), json!("created")]],
+ include_database_name: false,
};
assert_eq!(effective_columns(&hive_options), vec![Some("payload.id".to_string()), Some("name".to_string())]);
assert!(prepare_data_grid_save(hive_options).rollback_statements[0].contains("`payload.id` = 7"));
@@ -6028,6 +6291,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
};
assert_eq!(effective_columns(&postgres_options), vec![Some("events.id".to_string())]);
}
@@ -6210,6 +6474,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(large_value))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6279,6 +6544,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("new"))]), (1, vec![(1, json!("new"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6310,6 +6576,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("144847503924137986"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6335,6 +6602,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(10280))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6367,6 +6635,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("02"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6404,6 +6673,7 @@ mod tests {
dirty_rows: vec![(0, vec![(0, json!("02"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -6438,6 +6708,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("02"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -6469,6 +6740,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("support"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6513,6 +6785,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("support-7")), (2, json!("SUPPORT")), (3, json!("support"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6570,6 +6843,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Ada Lovelace"))])],
deleted_rows: vec![1],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6609,6 +6883,7 @@ mod tests {
dirty_rows: vec![(0, vec![(2, json!("Ada Lovelace"))])],
deleted_rows: vec![1],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6640,6 +6915,7 @@ mod tests {
dirty_rows: vec![(0, vec![(2, json!("Grace"))])],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert!(result.validation_error.as_deref().is_some_and(|error| error.contains("missing: ID")));
@@ -6666,6 +6942,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Grace"))])],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert!(result.validation_error.as_deref().is_some_and(|error| error.contains("missing: ID")));
@@ -6696,6 +6973,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Grace"))])],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -6731,6 +7009,7 @@ mod tests {
dirty_rows: vec![(0, vec![(2, json!("Grace"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert!(result.validation_error.as_deref().is_some_and(|error| error.contains("missing: CKG023")));
@@ -6757,6 +7036,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("34-B-0048"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6789,6 +7069,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(googlesql.validation_error, None);
@@ -6815,6 +7096,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(postgres_dialect.validation_error, None);
@@ -6842,6 +7124,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Ada"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(no_reported_quote.validation_error, None);
@@ -6894,6 +7177,7 @@ mod tests {
columns: vec!["id".to_string(), "event_type".to_string()],
source_columns: None,
rows: vec![vec![json!(1), json!("logout")]],
+ include_database_name: false,
});
assert_eq!(statements, vec!["UPDATE `audit-schema`.`events` SET `event_type` = 'logout' WHERE `id` = 1;"]);
@@ -6918,6 +7202,7 @@ mod tests {
dirty_rows: vec![(0, vec![(0, json!("LY-SC01-260800002"))])],
deleted_rows: vec![],
new_rows: vec![vec![json!("dbx-insert-check"), json!(461936049002043_i64)]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -6951,6 +7236,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("2"))])],
deleted_rows: vec![1],
new_rows: vec![vec![json!(3), json!("new")]],
+ include_database_name: false,
},
Some("GBASE8S"),
);
@@ -6994,6 +7280,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("GBase 8s updated"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7028,6 +7315,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("new"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7056,6 +7344,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("new"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7085,6 +7374,7 @@ mod tests {
dirty_rows: vec![(0, vec![(2, json!("Ada Lovelace"))])],
deleted_rows: vec![1],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7128,6 +7418,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7156,6 +7447,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!(1), json!("2022-08-25T09:58:43Z")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7186,6 +7478,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("0xccdd"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7219,6 +7512,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("new"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7274,6 +7568,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0, 1],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7310,6 +7605,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7335,6 +7631,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -7363,6 +7660,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("早上"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
}
}
@@ -7524,6 +7822,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0, 1],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7672,6 +7971,7 @@ mod tests {
dirty_rows: vec![(0, vec![(0, json!(r#"{"name":"after"}"#))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
};
let update = prepare_data_grid_save(options.clone());
@@ -7726,6 +8026,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!([]))])],
deleted_rows: vec![],
new_rows: vec![vec![json!(2), json!({"nested": [1, 2]})]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7821,6 +8122,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("0"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.statements, vec!["UPDATE `public`.`flags` SET `flag` = b'0' WHERE `id` = 1;"],);
@@ -7912,6 +8214,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(r"line\n's"))])],
deleted_rows: vec![],
new_rows: vec![vec![json!(2), json!(r"\n")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7947,6 +8250,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(nested_json))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -7996,6 +8300,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(r".\SQL2016"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8063,6 +8368,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(true))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8096,6 +8402,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(""))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8125,6 +8432,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(""))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8154,6 +8462,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("111\n222"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8183,6 +8492,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("ready")), (2, json!("2026-05-04"))])],
deleted_rows: vec![0],
new_rows: vec![vec![json!(43), json!("new"), json!("2026-05-05")]],
+ include_database_name: false,
});
assert_eq!(
@@ -8214,6 +8524,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8258,6 +8569,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![1],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -8287,6 +8599,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8323,6 +8636,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(229.9))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8362,6 +8676,7 @@ mod tests {
dirty_rows: vec![(1, vec![(2, json!(229.9))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8408,6 +8723,7 @@ mod tests {
json!(1.0),
json!("codex-lab"),
]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8452,6 +8768,7 @@ mod tests {
vec![json!("device_a"), json!("2026-07-10T17:48:51.000+08:00"), json!(1), json!(221.0)],
vec![json!("device_a"), json!("2026-07-10T17:48:51.000+08:00"), json!(2), json!(222.0)],
],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8484,6 +8801,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![Value::Null, json!("2026-07-10T17:48:51.000+08:00"), json!(1.0)]],
+ include_database_name: false,
});
assert_eq!(
@@ -8513,6 +8831,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -8545,6 +8864,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(229.9))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -8578,6 +8898,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!(3))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, Some("TDengine row identifier columns cannot be edited.".to_string()));
@@ -8604,6 +8925,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
deleted_rows: vec![0],
new_rows: vec![vec![json!(2), json!("Grace")]],
+ include_database_name: false,
});
assert_eq!(
@@ -8637,6 +8959,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
deleted_rows: vec![0],
new_rows: vec![vec![json!(2), json!("Grace")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8672,6 +8995,7 @@ mod tests {
dirty_rows: vec![(0, vec![(0, json!(2))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -8709,6 +9033,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("2026-06-25")), (2, json!("Linus"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -8738,6 +9063,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("2026-06-25"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -8766,6 +9092,7 @@ mod tests {
columns: vec!["id".to_string(), "name".to_string()],
source_columns: None,
rows: vec![vec![json!(1), json!("Ada")]],
+ include_database_name: false,
});
assert_eq!(statements, vec!["ALTER TABLE `people` UPDATE `name` = 'Ada' WHERE `id` = 1;"]);
@@ -8789,6 +9116,7 @@ mod tests {
columns: vec!["id".to_string(), "status".to_string()],
source_columns: None,
rows: vec![vec![json!(1), json!("paid")]],
+ include_database_name: false,
});
assert_eq!(
copy_updates,
@@ -8823,6 +9151,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("paid"))])],
deleted_rows: vec![1],
new_rows: vec![vec![json!(4), json!("new")]],
+ include_database_name: false,
});
assert_eq!(
save.statements,
@@ -8854,6 +9183,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -8884,6 +9214,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("Linus"))])],
deleted_rows: vec![0],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -8919,6 +9250,7 @@ mod tests {
dirty_rows: vec![(0, vec![(2, json!("Luxembourg City")), (3, json!(999))])],
deleted_rows: vec![],
new_rows: vec![vec![json!("USA"), json!(2008), json!("United States"), json!(43000)]],
+ include_database_name: false,
});
assert_eq!(
@@ -8975,6 +9307,7 @@ mod tests {
)],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -9002,6 +9335,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("18"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -9029,6 +9363,7 @@ mod tests {
dirty_rows: vec![(0, vec![(0, json!("S471355(0)"))])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(
@@ -9062,6 +9397,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, json!("new"))])],
deleted_rows: vec![1],
new_rows: vec![vec![json!("3"), json!("inserted")]],
+ include_database_name: false,
});
assert_eq!(
@@ -9094,6 +9430,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!("ALB"), json!(2021), json!(0.913)]],
+ include_database_name: false,
});
assert_eq!(
@@ -9140,6 +9477,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!(72), json!("轻卡"), Value::Null]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9172,6 +9510,7 @@ mod tests {
dirty_rows: vec![(0, vec![(2, json!("new"))])],
deleted_rows: vec![1],
new_rows: vec![vec![Value::Null, json!(3), json!("inserted")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9207,6 +9546,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![Value::Null, json!("2026-06-12T00:00:00Z")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9235,6 +9575,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!(42), json!("2026-06-12T00:00:00Z")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9266,6 +9607,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!(""), json!("Ada")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9295,6 +9637,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![Value::Null, Value::Null, json!("created")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9324,6 +9667,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![Value::Null, Value::Null]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9354,6 +9698,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![json!(7), Value::Null, json!("created")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9383,6 +9728,7 @@ mod tests {
dirty_rows: vec![(0, vec![(1, Value::Null)])],
deleted_rows: vec![],
new_rows: vec![],
+ include_database_name: false,
});
assert_eq!(result.validation_error, Some(r#"Column "trigger_value" does not allow NULL."#.to_string()));
@@ -9411,6 +9757,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![Value::Null, json!("Ada")]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, None);
@@ -9439,6 +9786,7 @@ mod tests {
dirty_rows: vec![],
deleted_rows: vec![],
new_rows: vec![vec![Value::Null, Value::Null]],
+ include_database_name: false,
});
assert_eq!(result.validation_error, Some(r#"Column "LogTime" does not allow NULL."#.to_string()));
diff --git a/crates/dbx-sql/src/sql_dialect.rs b/crates/dbx-sql/src/sql_dialect.rs
index c58a2ab598..fca9918e2c 100644
--- a/crates/dbx-sql/src/sql_dialect.rs
+++ b/crates/dbx-sql/src/sql_dialect.rs
@@ -47,7 +47,8 @@ pub use table_select::{
build_table_select_sql, DBX_LARGE_VALUE_BYTES_COLUMN_PREFIX,
};
pub use table_select::{
- quote_table_data_identifier, table_data_qualified_table_name, table_data_schema, uses_connection_identifier_quote,
+ database_qualified_table_name, quote_table_data_identifier, table_data_qualified_table_name, table_data_schema,
+ uses_connection_identifier_quote,
};
pub use type_rewrite::{
apply_auto_inc_to_column_def, column_is_auto_increment, normalize_len_params, rewrite_column_type,
diff --git a/crates/dbx-sql/src/sql_dialect/table_select.rs b/crates/dbx-sql/src/sql_dialect/table_select.rs
index 76e0f08804..ea96d0d740 100644
--- a/crates/dbx-sql/src/sql_dialect/table_select.rs
+++ b/crates/dbx-sql/src/sql_dialect/table_select.rs
@@ -4,8 +4,8 @@ use super::capabilities::{
firebird_rows_clause, table_pagination_strategy, uses_oracle_row_id, uses_xugu_row_id, TablePaginationStrategy,
};
use super::identifiers::{
- normalize_where_input, qualified_table_name, qualified_table_name_with_catalog, quote_gaussdb_jdbc_identifier,
- quote_iris_identifier, quote_table_identifier,
+ normalize_where_input, parse_sqlserver_linked_schema_ref, qualified_table_name, qualified_table_name_with_catalog,
+ quote_gaussdb_jdbc_identifier, quote_iris_identifier, quote_table_identifier,
};
use super::types::{
TableDataSelectSqlOptions, TableSelectSqlOptions, DBX_NEO4J_ELEMENT_ID_COLUMN, DBX_ROWID_COLUMN,
@@ -221,6 +221,7 @@ pub fn build_table_data_select_sql_with_database(
database_qualified_table_name(
database_type,
options.catalog.as_deref(),
+ schema,
options.database.as_deref(),
&options.table_name,
)
@@ -438,12 +439,26 @@ fn default_time_series_predicate(database_type: Option<DatabaseType>) -> Option<
}
}
-/// Returns a `database.table` reference for engines whose active database is
-/// normally omitted from table-data SQL. Doris and StarRocks retain an external
-/// catalog prefix when one is selected.
-fn database_qualified_table_name(
+/// Returns the fully qualified reference for engines whose active database is
+/// normally omitted from generated table SQL:
+///
+/// - `database.table` for MySQL-compatible engines and ClickHouse;
+/// - `database.schema.table` for SQL Server, whose tables are addressable
+/// across databases on the same connection;
+/// - Doris and StarRocks keep their external catalog prefix
+/// (`catalog.database.table`).
+///
+/// Shared by every "generated table SQL" surface that honors the
+/// `生成 SQL 时包含数据库名` setting, so the grid label, the copy-as-INSERT/UPDATE/
+/// SELECT statements and the data-grid save statements stay in sync.
+///
+/// `schema` wins over `database` for the MySQL family: after a cross-database
+/// editable result (`SELECT * FROM db_9.users`) the table's own namespace lives
+/// in `schema` while `database` still holds the connection's default database.
+pub fn database_qualified_table_name(
database_type: Option<DatabaseType>,
catalog: Option<&str>,
+ schema: Option<&str>,
database: Option<&str>,
table_name: &str,
) -> Option<String> {
@@ -455,7 +470,28 @@ fn database_qualified_table_name(
quote_table_identifier(database_type, table_name)
)),
Some(DatabaseType::Mysql | DatabaseType::Goldendb | DatabaseType::Doris | DatabaseType::StarRocks) => {
- Some(qualified_table_name_with_catalog(database_type, catalog, Some(database), Some(database), table_name))
+ let namespace = schema.map(str::trim).filter(|schema| !schema.is_empty()).unwrap_or(database);
+ Some(qualified_table_name_with_catalog(
+ database_type,
+ catalog,
+ Some(namespace),
+ Some(namespace),
+ table_name,
+ ))
+ }
+ Some(DatabaseType::SqlServer) => {
+ // A linked-server schema already carries `server|catalog|schema`, so
+ // prefixing it with the local database would produce a bogus name.
+ let schema = schema.map(str::trim).filter(|schema| !schema.is_empty())?;
+ if parse_sqlserver_linked_schema_ref(schema).is_some() {
+ return None;
+ }
+ Some(format!(
+ "{}.{}.{}",
+ quote_table_identifier(database_type, database),
+ quote_table_identifier(database_type, schema),
+ quote_table_identifier(database_type, table_name)
+ ))
}
_ => None,
}
@@ -1011,6 +1047,78 @@ mod tests {
assert_eq!(build_table_data_select_sql_with_database(options, true), "SELECT * FROM `aaa`.`apis` LIMIT 10;");
}
+ /// issue #9262: SQL Server addresses tables as `database.schema.table`, and
+ /// the grid label must reach the three-part form once the user opted into
+ /// `生成 SQL 时包含数据库名`.
+ #[test]
+ fn sqlserver_table_data_select_optionally_qualifies_database() {
+ let options = TableDataSelectSqlOptions {
+ database_type: Some(DatabaseType::SqlServer),
+ schema: Some("dbo".to_string()),
+ database: Some("dbx".to_string()),
+ table_name: "AcceptanceProductLog".to_string(),
+ limit: Some(100),
+ ..Default::default()
+ };
+ assert_eq!(
+ build_table_data_select_sql(options.clone()),
+ "SELECT TOP (100) * FROM [dbo].[AcceptanceProductLog]"
+ );
+ assert_eq!(
+ build_table_data_select_sql_with_database(options, true),
+ "SELECT TOP (100) * FROM [dbx].[dbo].[AcceptanceProductLog]"
+ );
+ }
+
+ /// A linked-server schema already encodes `server|catalog|schema`; the local
+ /// database must never be prefixed on top of it.
+ #[test]
+ fn sqlserver_linked_schema_ignores_include_database_name() {
+ let options = TableDataSelectSqlOptions {
+ database_type: Some(DatabaseType::SqlServer),
+ schema: Some("__dbx_sqlserver_linked__:ERP|Finance|dbo".to_string()),
+ database: Some("dbx".to_string()),
+ table_name: "orders".to_string(),
+ limit: Some(100),
+ ..Default::default()
+ };
+ let sql = build_table_data_select_sql_with_database(options, true);
+ assert_eq!(sql, "SELECT TOP (100) * FROM [ERP].[Finance].[dbo].[orders]");
+ }
+
+ /// `database_qualified_table_name` is shared by every generated-SQL surface,
+ /// so pin its per-engine contract directly.
+ #[test]
+ fn database_qualified_table_name_matches_engine_naming() {
+ assert_eq!(
+ database_qualified_table_name(Some(DatabaseType::Mysql), None, None, Some("dbx"), "t").as_deref(),
+ Some("`dbx`.`t`")
+ );
+ // A cross-database editable result keeps its own namespace, not the
+ // connection's default database.
+ assert_eq!(
+ database_qualified_table_name(Some(DatabaseType::Mysql), None, Some("db_9"), Some("dbx"), "t").as_deref(),
+ Some("`db_9`.`t`")
+ );
+ assert_eq!(
+ database_qualified_table_name(Some(DatabaseType::ClickHouse), None, Some("default"), Some("dbx"), "t")
+ .as_deref(),
+ Some("`dbx`.`t`")
+ );
+ assert_eq!(
+ database_qualified_table_name(Some(DatabaseType::SqlServer), None, Some("dbo"), Some("dbx"), "t")
+ .as_deref(),
+ Some("[dbx].[dbo].[t]")
+ );
+ // SQL Server without a schema cannot build a valid three-part name.
+ assert_eq!(database_qualified_table_name(Some(DatabaseType::SqlServer), None, None, Some("dbx"), "t"), None);
+ assert_eq!(
+ database_qualified_table_name(Some(DatabaseType::Postgres), None, Some("public"), Some("dbx"), "t"),
+ None
+ );
+ assert_eq!(database_qualified_table_name(Some(DatabaseType::Mysql), None, None, None, "t"), None);
+ }
+
#[test]
fn doris_external_catalog_without_database_degrades_to_two_part() {
// When neither schema nor database is provided the name degrades to the
diff --git a/docs/screenshots/issue-9262-ddl-database-name.png b/docs/screenshots/issue-9262-ddl-database-name.png
new file mode 100644
index 0000000000..d4a3440a5d
Binary files /dev/null and b/docs/screenshots/issue-9262-ddl-database-name.png differ
diff --git a/docs/screenshots/issue-9262-save-sql-after.png b/docs/screenshots/issue-9262-save-sql-after.png
new file mode 100644
index 0000000000..34f2c42260
Binary files /dev/null and b/docs/screenshots/issue-9262-save-sql-after.png differ
diff --git a/docs/screenshots/issue-9262-save-sql-before.png b/docs/screenshots/issue-9262-save-sql-before.png
new file mode 100644
index 0000000000..cf4a825359
Binary files /dev/null and b/docs/screenshots/issue-9262-save-sql-before.png differ
diff --git a/docs/screenshots/issue-9262-save-sql-setting-off.png b/docs/screenshots/issue-9262-save-sql-setting-off.png
new file mode 100644
index 0000000000..cd5ce51b0f
Binary files /dev/null and b/docs/screenshots/issue-9262-save-sql-setting-off.png differ
diff --git a/docs/screenshots/issue-9262-sqlserver-save-sql.png b/docs/screenshots/issue-9262-sqlserver-save-sql.png
new file mode 100644
index 0000000000..9a62ae0d27
Binary files /dev/null and b/docs/screenshots/issue-9262-sqlserver-save-sql.png differ
diff --git a/docs/screenshots/issue-9262-sqlserver-table-sql.png b/docs/screenshots/issue-9262-sqlserver-table-sql.png
new file mode 100644
index 0000000000..e0b043d0f4
Binary files /dev/null and b/docs/screenshots/issue-9262-sqlserver-table-sql.png differ