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。

1.7 KB
Raw Blame
export interface SqlSingleQuoteCaretOptions {
  previousValue: string;
  nextValue: string;
  selectionStart: number | null | undefined;
}

export type SqlSingleQuoteKeyAction = "pass" | "skipClosingQuote" | "insertEscapedQuote";

export function resolveSqlSingleQuoteKeyAction(options: { previousChar?: string; nextChar?: string; autoCloseBrackets?: boolean }): SqlSingleQuoteKeyAction {
  const { previousChar = "", nextChar = "", autoCloseBrackets = true } = options;
  if (!autoCloseBrackets) return "pass";
  if (previousChar === "'" && nextChar === "'") return "skipClosingQuote";
  if (previousChar === "'") return "insertEscapedQuote";
  return "pass";
}

export function insertedSqlSingleQuoteAtCaret(options: SqlSingleQuoteCaretOptions): boolean {
  const { previousValue, nextValue, selectionStart } = options;
  if (typeof selectionStart !== "number" || selectionStart < 1) return false;
  if (nextValue.charAt(selectionStart - 1) !== "'") return false;
  return nextValue.slice(0, selectionStart - 1) + nextValue.slice(selectionStart) === previousValue;
}

export function caretPositionInsideInsertedSqlSingleQuotes(options: SqlSingleQuoteCaretOptions): number | null {
  const { previousValue, nextValue, selectionStart } = options;
  if (typeof selectionStart !== "number" || selectionStart < 2) return null;
  if (nextValue.slice(selectionStart - 2, selectionStart) !== "''") return null;

  const singleQuoteInsertPrevious = nextValue.slice(0, selectionStart - 1) + nextValue.slice(selectionStart);
  if (singleQuoteInsertPrevious === previousValue) return selectionStart - 1;

  const pairInsertPrevious = nextValue.slice(0, selectionStart - 2) + nextValue.slice(selectionStart);
  if (pairInsertPrevious === previousValue) return selectionStart - 1;

  return null;
}