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。

2.6 KB
Raw Blame
import type { ConnectionConfig, TreeNode, TreeNodeType } from "@/types/database";

export type MysqlObjectTemplateKind = "procedure" | "function" | "trigger";

export interface MysqlObjectTemplate {
  kind: MysqlObjectTemplateKind;
  titleKey: "contextMenu.createProcedure" | "contextMenu.createFunction" | "contextMenu.createTrigger";
  sql: string;
}

type MysqlTemplateConnection = Partial<Pick<ConnectionConfig, "db_type" | "driver_profile">>;
type MysqlTemplateNode = Pick<TreeNode, "type" | "database" | "tableName">;

const templateKindByGroup: Partial<Record<TreeNodeType, MysqlObjectTemplateKind>> = {
  "group-procedures": "procedure",
  "group-functions": "function",
  "group-triggers": "trigger",
};

export function supportsMysqlObjectTemplates(connection?: MysqlTemplateConnection): boolean {
  if (connection?.db_type !== "mysql") return false;
  const profile = connection.driver_profile?.trim().toLowerCase();
  return !profile || profile === "mysql" || profile === "custom_mysql";
}

export function mysqlObjectTemplateForGroup(connection: MysqlTemplateConnection | undefined, node: MysqlTemplateNode): MysqlObjectTemplate | null {
  if (!supportsMysqlObjectTemplates(connection) || !node.database) return null;
  const kind = templateKindByGroup[node.type];
  if (!kind) return null;
  return buildMysqlObjectTemplate(kind, node.database, node.tableName);
}

export function buildMysqlObjectTemplate(kind: MysqlObjectTemplateKind, database: string, tableName?: string): MysqlObjectTemplate {
  const qualifiedDatabase = quoteMysqlIdentifier(database);

  if (kind === "procedure") {
    const name = "new_procedure";
    return {
      kind,
      titleKey: "contextMenu.createProcedure",
      sql: `DELIMITER $$

CREATE PROCEDURE ${qualifiedDatabase}.${quoteMysqlIdentifier(name)}()
BEGIN
  SELECT 1;
END$$

DELIMITER ;`,
    };
  }

  if (kind === "function") {
    const name = "new_function";
    return {
      kind,
      titleKey: "contextMenu.createFunction",
      sql: `DELIMITER $$

CREATE FUNCTION ${qualifiedDatabase}.${quoteMysqlIdentifier(name)}()
RETURNS INT
DETERMINISTIC
BEGIN
  RETURN 0;
END$$

DELIMITER ;`,
    };
  }

  const name = "new_trigger";
  return {
    kind,
    titleKey: "contextMenu.createTrigger",
    sql: `DELIMITER $$

CREATE TRIGGER ${qualifiedDatabase}.${quoteMysqlIdentifier(name)}
BEFORE INSERT ON ${qualifiedDatabase}.${quoteMysqlIdentifier(tableName || "table_name")}
FOR EACH ROW
BEGIN
  SET @dbx_trigger_placeholder = 1;
END$$

DELIMITER ;`,
  };
}

function quoteMysqlIdentifier(identifier: string): string {
  return `\`${identifier.replaceAll("`", "``")}\``;
}