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。

3.2 KB
Raw Blame
import { getActivePinia } from "pinia";
import { useReadOnlyUnlockStore, type WriteUnlockDurationSecs } from "@/stores/readOnlyUnlockStore";
import { classifyRedisCommandSafety } from "@/lib/redis/redisCommandSafety";
import { isRedisCommentLine } from "@/lib/redis/redisCommandTokenizer";
import { classifySqlRisk, isSqlRiskMutation } from "@/lib/sql/sqlRisk";
import type { DatabaseType } from "@/types/database";
import { mysqlDataCharsetEnabled, type MysqlDataCharsetConnection } from "@/lib/connection/mysqlDataCharset";
import { useToast } from "@/composables/useToast";

export type { WriteUnlockDurationSecs };

export const WRITE_UNLOCK_DURATIONS: WriteUnlockDurationSecs[] = [60, 300];

export function formatWriteUnlockRemaining(ms: number): string {
  const totalSeconds = Math.max(0, Math.ceil(ms / 1000));
  const minutes = Math.floor(totalSeconds / 60);
  const seconds = totalSeconds % 60;
  return `${minutes}:${String(seconds).padStart(2, "0")}`;
}

function unlockStoreOrNull() {
  try {
    if (!getActivePinia()) return null;
    return useReadOnlyUnlockStore();
  } catch {
    return null;
  }
}

export function isWriteUnlockActive(connectionId: string | undefined | null): boolean {
  if (!connectionId) return false;
  return unlockStoreOrNull()?.isUnlocked(connectionId) === true;
}

export function connectionIsEffectivelyReadOnly(connection?: ({ id?: string; read_only?: boolean } & MysqlDataCharsetConnection) | null): boolean {
  if (mysqlDataCharsetEnabled(connection)) return true;
  if (!connection?.read_only) return false;
  return !isWriteUnlockActive(connection.id);
}

export function sqlLooksLikeMutation(sql: string | undefined, databaseType?: DatabaseType): boolean {
  if (!sql?.trim()) return true;
  if (databaseType === "redis") {
    return sql.split("\n").some((line) => {
      const command = line.trim();
      return command.length > 0 && !isRedisCommentLine(command) && classifyRedisCommandSafety(command) !== "allowed";
    });
  }
  return isSqlRiskMutation(classifySqlRisk(sql, { dialect: databaseType }).risk);
}

export interface ReadOnlyWriteAccessOptions {
  connection?: ({ id?: string; name?: string; read_only?: boolean; db_type?: DatabaseType } & MysqlDataCharsetConnection) | null;
  sql?: string;
  source?: string;
  treatAsMutation?: boolean;
}

export async function ensureReadOnlyWriteAccess(options: ReadOnlyWriteAccessOptions): Promise<boolean> {
  const connection = options.connection;
  if (mysqlDataCharsetEnabled(connection)) {
    if (options.treatAsMutation !== true && !sqlLooksLikeMutation(options.sql, connection?.db_type)) return true;
    const { default: i18n } = await import("@/i18n");
    useToast().toast(i18n.global.t("connection.mysqlDataCharsetWriteBlocked"), 5000);
    return false;
  }
  if (!connection?.read_only) return true;
  if (!connection.id) return false;
  const store = unlockStoreOrNull();
  if (store?.isUnlocked(connection.id)) return true;
  const isMutation = options.treatAsMutation === true || sqlLooksLikeMutation(options.sql, connection.db_type);
  if (!isMutation) return true;
  if (!store) return false;
  return store.requestUnlock({
    connectionId: connection.id,
    connectionName: connection.name,
    sql: options.sql,
    source: options.source,
  });
}