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.4 KB
Raw Blame
import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage";
import type { SqlParameterInput } from "@/lib/sql/sqlParameters";

const STORAGE_KEY = "dbx-sql-parameter-history";
export const MAX_SQL_PARAMETER_HISTORY = 8;

interface StoredSqlParameterHistory {
  version: 1;
  parameters: Record<string, SqlParameterInput[]>;
}

function emptyHistory(): StoredSqlParameterHistory {
  return { version: 1, parameters: {} };
}

function readHistory(): StoredSqlParameterHistory {
  const raw = safeLocalStorageGet(STORAGE_KEY);
  if (!raw) return emptyHistory();
  try {
    const parsed = JSON.parse(raw) as Partial<StoredSqlParameterHistory>;
    if (parsed.version !== 1 || !parsed.parameters || typeof parsed.parameters !== "object") return emptyHistory();
    return { version: 1, parameters: parsed.parameters };
  } catch {
    return emptyHistory();
  }
}

function writeHistory(history: StoredSqlParameterHistory) {
  safeLocalStorageSet(STORAGE_KEY, JSON.stringify(history));
}

function normalizeParameterName(name: string): string {
  return name.trim().toLowerCase();
}

function normalizeInput(input: SqlParameterInput): SqlParameterInput | null {
  if (input.kind === "null") return { kind: "null", value: "NULL" };
  const value = input.value.trim();
  if (!value) return null;
  return { kind: input.kind, value };
}

export function loadSqlParameterHistory(name: string): SqlParameterInput[] {
  return (readHistory().parameters[normalizeParameterName(name)] ?? []).slice(0, MAX_SQL_PARAMETER_HISTORY);
}

export function rememberSqlParameterValue(name: string, input: SqlParameterInput): SqlParameterInput[] {
  const normalized = normalizeInput(input);
  if (!normalized) return loadSqlParameterHistory(name);

  const history = readHistory();
  const key = normalizeParameterName(name);
  const previous = history.parameters[key] ?? [];
  history.parameters[key] = [normalized, ...previous.filter((entry) => entry.value !== normalized.value || entry.kind !== normalized.kind)].slice(0, MAX_SQL_PARAMETER_HISTORY);
  writeHistory(history);
  return history.parameters[key];
}

export function rememberSqlParameterValues(values: Record<string, SqlParameterInput>): Record<string, SqlParameterInput[]> {
  const result: Record<string, SqlParameterInput[]> = {};
  for (const [name, input] of Object.entries(values)) {
    result[name] = rememberSqlParameterValue(name, input);
  }
  return result;
}