feat(editor): add ExPaste paste-as-IN-condition helper (#2746)
* feat: add ExPaste SQL IN condition helper * fix: restrict ExPaste to SQL-like editors --------- Co-authored-by: zipg <4047349+zipg@users.noreply.github.com>
This commit is contained in:
1 parent
29495a7220
commit
705bacba04
17 files changed
+491
-8
No files matched your search
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
|
||||
import { connectionNamespaceCreationTarget, databaseNodeNamespaceCreationTarget } from "@/lib/database/databaseNamespaceCreation";
|
||||
import { editableDatabasePropertyGroups, editableSchemaPropertyGroups } from "@/lib/database/databasePropertyEditing";
|
||||
import { buildGetDatabaseCommentSql } from "@/lib/database/dbAdminSql";
|
||||
import { supportsTransaction } from "@/lib/database/databaseFeatureSupport";
|
||||
import { supportsSqlInListPaste, supportsTransaction } from "@/lib/database/databaseFeatureSupport";
|
||||
|
||||
describe("supportsTransaction", () => {
|
||||
it("returns true for supported database types", () => {
|
||||
@@ -30,6 +30,39 @@ describe("supportsTransaction", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("supportsSqlInListPaste", () => {
|
||||
it("allows generic and SQL-like editors", () => {
|
||||
expect(supportsSqlInListPaste(undefined)).toBe(true);
|
||||
expect(supportsSqlInListPaste("mysql")).toBe(true);
|
||||
expect(supportsSqlInListPaste("postgres")).toBe(true);
|
||||
expect(supportsSqlInListPaste("oracle")).toBe(true);
|
||||
expect(supportsSqlInListPaste("sqlserver")).toBe(true);
|
||||
expect(supportsSqlInListPaste("sqlite")).toBe(true);
|
||||
expect(supportsSqlInListPaste("cassandra")).toBe(true);
|
||||
expect(supportsSqlInListPaste("tdengine")).toBe(true);
|
||||
expect(supportsSqlInListPaste("iotdb")).toBe(true);
|
||||
expect(supportsSqlInListPaste("jdbc")).toBe(true);
|
||||
});
|
||||
|
||||
it("hides SQL IN list paste in non-SQL editors", () => {
|
||||
expect(supportsSqlInListPaste("redis")).toBe(false);
|
||||
expect(supportsSqlInListPaste("mongodb")).toBe(false);
|
||||
expect(supportsSqlInListPaste("elasticsearch")).toBe(false);
|
||||
expect(supportsSqlInListPaste("qdrant")).toBe(false);
|
||||
expect(supportsSqlInListPaste("milvus")).toBe(false);
|
||||
expect(supportsSqlInListPaste("weaviate")).toBe(false);
|
||||
expect(supportsSqlInListPaste("chromadb")).toBe(false);
|
||||
expect(supportsSqlInListPaste("etcd")).toBe(false);
|
||||
expect(supportsSqlInListPaste("zookeeper")).toBe(false);
|
||||
expect(supportsSqlInListPaste("mq")).toBe(false);
|
||||
expect(supportsSqlInListPaste("nacos")).toBe(false);
|
||||
});
|
||||
|
||||
it("excludes Neo4j because Cypher uses list syntax instead of SQL IN tuples", () => {
|
||||
expect(supportsSqlInListPaste("neo4j")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("database property editing", () => {
|
||||
it("allows MySQL-compatible charset and collation edits on database nodes", () => {
|
||||
expect(editableDatabasePropertyGroups({ db_type: "mysql" }, { type: "database", database: "app" })).toEqual(["charsetCollation"]);
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
|
||||
import { DEFAULT_SHORTCUT_SETTINGS, SHORTCUT_DEFINITIONS, findShortcutConflict, formatShortcut, normalizeShortcutSettings, shortcutToCodeMirrorKey, type ShortcutActionId } from "@/lib/editor/shortcutRegistry";
|
||||
|
||||
describe("shortcutRegistry editor actions", () => {
|
||||
const formatterEditorActionIds: ShortcutActionId[] = ["formatSql", "indentMore", "indentLess", "duplicateLine", "deleteLine", "moveLineUp", "moveLineDown", "copyLineUp", "copyLineDown", "undo", "redo", "selectAll", "uppercaseSelection", "lowercaseSelection"];
|
||||
const formatterEditorActionIds: ShortcutActionId[] = ["formatSql", "indentMore", "indentLess", "duplicateLine", "deleteLine", "moveLineUp", "moveLineDown", "copyLineUp", "copyLineDown", "undo", "redo", "selectAll", "uppercaseSelection", "lowercaseSelection", "exPasteSqlInCondition"];
|
||||
const sidebarShortcutActionIds: ShortcutActionId[] = ["copySidebarSelection", "pasteSidebarSelection", "editSidebarConnection"];
|
||||
|
||||
it("registers formatter editor shortcuts in the generic editor scope", () => {
|
||||
@@ -32,6 +32,7 @@ describe("shortcutRegistry editor actions", () => {
|
||||
expect(shortcuts.selectAll).toBe("Mod+A");
|
||||
expect(shortcuts.uppercaseSelection).toBe("Shift+Alt+U");
|
||||
expect(shortcuts.lowercaseSelection).toBe("Shift+Alt+L");
|
||||
expect(shortcuts.exPasteSqlInCondition).toBe("");
|
||||
});
|
||||
|
||||
it("detects conflicts between formatter editor shortcuts and other editor shortcuts", () => {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSqlInConditionFromPasteSource, insertTextForSqlInCondition, SQL_IN_LIST_PASTE_MAX_SOURCE_LENGTH, SQL_IN_LIST_PASTE_MAX_VALUES } from "@/lib/sql/sqlInListPaste";
|
||||
|
||||
describe("sqlInListPaste", () => {
|
||||
it("builds an IN condition from newline values", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("A\nB\nC")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('A', 'B', 'C')",
|
||||
valueCount: 3,
|
||||
});
|
||||
});
|
||||
|
||||
it("splits comma, tab, and newline separated clipboard content", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("A\tB\nC,D")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('A', 'B', 'C', 'D')",
|
||||
valueCount: 4,
|
||||
});
|
||||
});
|
||||
|
||||
it("splits simple slash-separated value lists", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("1/2/3")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN (1, 2, 3)",
|
||||
valueCount: 3,
|
||||
});
|
||||
expect(buildSqlInConditionFromPasteSource("A/B/C")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('A', 'B', 'C')",
|
||||
valueCount: 3,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not treat common dates, URLs, or absolute paths as value lists", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("2026/07/07")).toEqual({ ok: false, reason: "not-list" });
|
||||
expect(buildSqlInConditionFromPasteSource("http://example.com/a/b")).toEqual({ ok: false, reason: "not-list" });
|
||||
expect(buildSqlInConditionFromPasteSource("/Users/staff/dbx")).toEqual({ ok: false, reason: "not-list" });
|
||||
});
|
||||
|
||||
it("preserves numeric and NULL literals while quoting strings", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("1\n-2.5\n001\nnull\nA1")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN (1, -2.5, '001', NULL, 'A1')",
|
||||
valueCount: 5,
|
||||
});
|
||||
});
|
||||
|
||||
it("strips existing SQL list wrappers and escapes single quotes once", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("('O''Reilly', \"Bob\")")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('O''Reilly', 'Bob')",
|
||||
valueCount: 2,
|
||||
});
|
||||
expect(buildSqlInConditionFromPasteSource("IN ('A', 'B')")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('A', 'B')",
|
||||
valueCount: 2,
|
||||
});
|
||||
expect(buildSqlInConditionFromPasteSource("('A')")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('A')",
|
||||
valueCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not split delimiters inside quoted values", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("'A,B'\n'C\tD'")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('A,B', 'C\tD')",
|
||||
valueCount: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("treats apostrophes inside unquoted values as text", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("O'Reilly\nBob")).toEqual({
|
||||
ok: true,
|
||||
sql: "IN ('O''Reilly', 'Bob')",
|
||||
valueCount: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns empty for blank or delimiter-only content", () => {
|
||||
expect(buildSqlInConditionFromPasteSource(" \n,\t ")).toEqual({ ok: false, reason: "empty" });
|
||||
});
|
||||
|
||||
it("does not process unrelated single text values", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("paste some random text here")).toEqual({ ok: false, reason: "not-list" });
|
||||
expect(buildSqlInConditionFromPasteSource("O'Reilly")).toEqual({ ok: false, reason: "not-list" });
|
||||
});
|
||||
|
||||
it("guards very large paste input and value count", () => {
|
||||
expect(buildSqlInConditionFromPasteSource("x".repeat(SQL_IN_LIST_PASTE_MAX_SOURCE_LENGTH + 1))).toEqual({
|
||||
ok: false,
|
||||
reason: "too-large",
|
||||
limit: SQL_IN_LIST_PASTE_MAX_SOURCE_LENGTH,
|
||||
});
|
||||
expect(buildSqlInConditionFromPasteSource(Array.from({ length: SQL_IN_LIST_PASTE_MAX_VALUES + 1 }, (_, index) => String(index)).join("\n"))).toEqual({
|
||||
ok: false,
|
||||
reason: "too-many-values",
|
||||
limit: SQL_IN_LIST_PASTE_MAX_VALUES,
|
||||
});
|
||||
});
|
||||
|
||||
it("avoids duplicating IN when the cursor is already after IN or NOT IN", () => {
|
||||
expect(insertTextForSqlInCondition("IN ('A')", "where id in ")).toBe("('A')");
|
||||
expect(insertTextForSqlInCondition("IN ('A')", "where id not in")).toBe("('A')");
|
||||
expect(insertTextForSqlInCondition("IN ('A')", "where id")).toBe(" IN ('A')");
|
||||
expect(insertTextForSqlInCondition("IN ('A')", "where id = ")).toBe("IN ('A')");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user