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(sql): truncate insert template placeholders to column length

xiaohuowa1b703526ea86
diff --git a/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.ts b/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.ts
index d83b771865..a47cd8a9ad 100644
--- a/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.ts
+++ b/apps/desktop/src/lib/__tests__/table/tableSqlTemplates.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
-import { buildTableDeleteTemplate, buildTableSelectTemplate, buildTableUpdateTemplate } from "@/lib/table/tableSqlTemplates";
+import { buildTableDeleteTemplate, buildTableInsertTemplate, buildTableSelectTemplate, buildTableUpdateTemplate } from "@/lib/table/tableSqlTemplates";
+import type { ColumnInfo } from "@/types/database";
describe("buildTableSelectTemplate", () => {
it("can omit identifier quotes for a new-query table reference", () => {
@@ -60,3 +61,45 @@ describe.each(["oracle", "oceanbase-oracle"] as const)("%s table template identi
expect(buildTableDeleteTemplate(options)).toBe('DELETE FROM "App"."Orders"\nWHERE "id" = 0;');
});
});
+
+describe("buildTableInsertTemplate string placeholder length", () => {
+ function column(partial: Partial<ColumnInfo> & Pick<ColumnInfo, "name" | "data_type">): ColumnInfo {
+ return { is_nullable: true, column_default: null, is_primary_key: false, extra: null, ...partial };
+ }
+
+ it("keeps the placeholder unchanged when length metadata is missing", () => {
+ const sql = buildTableInsertTemplate({
+ databaseType: "oracle",
+ tableName: "t",
+ columns: [column({ name: "CRYPTO_MODE", data_type: "varchar2" })],
+ });
+ expect(sql).toBe('INSERT INTO "t" ("CRYPTO_MODE")\nVALUES (\'CRYPTO_MODE_value\');');
+ });
+
+ it("truncates the name part so the placeholder fits the declared length", () => {
+ const sql = buildTableInsertTemplate({
+ databaseType: "oracle",
+ tableName: "t",
+ columns: [column({ name: "CRYPTO_MODE", data_type: "varchar2", character_maximum_length: 16 })],
+ });
+ expect(sql).toBe('INSERT INTO "t" ("CRYPTO_MODE")\nVALUES (\'CRYPTO_MOD_value\');');
+ });
+
+ it("clips the suffix when the column is shorter than the suffix", () => {
+ const sql = buildTableInsertTemplate({
+ databaseType: "oracle",
+ tableName: "t",
+ columns: [column({ name: "status", data_type: "varchar2", character_maximum_length: 3 })],
+ });
+ expect(sql).toBe('INSERT INTO "t" ("status")\nVALUES (\'_va\');');
+ });
+
+ it("budgets multi-byte column names by bytes", () => {
+ const sql = buildTableInsertTemplate({
+ databaseType: "oracle",
+ tableName: "t",
+ columns: [column({ name: "部门", data_type: "varchar2", character_maximum_length: 9 })],
+ });
+ expect(sql).toBe('INSERT INTO "t" ("部门")\nVALUES (\'部_value\');');
+ });
+});
diff --git a/apps/desktop/src/lib/table/tableSqlTemplates.ts b/apps/desktop/src/lib/table/tableSqlTemplates.ts
index 3fd4e5b220..b186379677 100644
--- a/apps/desktop/src/lib/table/tableSqlTemplates.ts
+++ b/apps/desktop/src/lib/table/tableSqlTemplates.ts
@@ -158,7 +158,42 @@ function columnPlaceholderValue(column: ColumnInfo, databaseType?: DatabaseType)
if (typeLooksSpatial(dataType)) return "NULL";
if (typeLooksArray(dataType)) return "'{}'";
- return `'${colName}_value'`;
+ return stringPlaceholderValue(colName, column.character_maximum_length);
+}
+
+// Byte-aware budget so byte-length columns (Oracle VARCHAR2 N BYTE) stay safe
+// even when the column name carries multi-byte characters.
+function utf8Length(value: string): number {
+ let length = 0;
+ for (const char of value) {
+ const code = char.codePointAt(0)!;
+ length += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
+ }
+ return length;
+}
+
+/**
+ * `col_value` placeholder clipped to the column's declared length: the name
+ * part shrinks so the recognizable `_value` suffix survives; when even the
+ * suffix cannot fit, it degrades to a clipped suffix so the statement stays
+ * executable.
+ */
+function stringPlaceholderValue(colName: string, maxLength: number | null | undefined): string {
+ const value = `${colName}_value`;
+ const limit = typeof maxLength === "number" && Number.isFinite(maxLength) && maxLength > 0 ? maxLength : null;
+ if (limit === null || utf8Length(value) <= limit) return `'${value}'`;
+ const suffixLength = utf8Length("_value");
+ if (suffixLength > limit) return `'${"_value".slice(0, limit)}'`;
+ const nameBudget = limit - suffixLength;
+ let name = "";
+ let used = 0;
+ for (const char of colName) {
+ const charLength = utf8Length(char);
+ if (used + charLength > nameBudget) break;
+ name += char;
+ used += charLength;
+ }
+ return `'${name}_value'`;
}
function typeLooksNumeric(dataType: string): boolean {