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.8 KB
Raw Blame
/**
 * Build the editor content after appending AI-generated SQL to existing editor SQL.
 * Preserves the existing editor content exactly and adds only the newline separator
 * needed to leave a blank line before the appended SQL.
 */
export function buildAppendedEditorSql(currentEditorSql: string, newSql: string): string {
  if (!currentEditorSql) return newSql;

  let separator = "\n\n";
  if (currentEditorSql.endsWith("\r\n\r\n") || currentEditorSql.endsWith("\n\n")) {
    separator = "";
  } else if (currentEditorSql.endsWith("\r\n")) {
    separator = "\r\n";
  } else if (currentEditorSql.endsWith("\n")) {
    separator = "\n";
  }

  return `${currentEditorSql}${separator}${newSql}`;
}

/**
 * Append AI-generated SQL unless the exact text already exists as a standalone
 * editor block. AI appends use a blank line as their block separator, which
 * avoids treating a matching fragment inside a larger statement as a duplicate.
 */
export function buildDeduplicatedAppendedEditorSql(currentEditorSql: string, newSql: string): string {
  if (!newSql || containsStandaloneSqlBlock(currentEditorSql, newSql)) return currentEditorSql;
  return buildAppendedEditorSql(currentEditorSql, newSql);
}

function containsStandaloneSqlBlock(editorSql: string, sql: string): boolean {
  let from = 0;
  while (from <= editorSql.length - sql.length) {
    const index = editorSql.indexOf(sql, from);
    if (index < 0) return false;

    const before = editorSql.slice(0, index);
    const after = editorSql.slice(index + sql.length);
    const startsAtBlockBoundary = index === 0 || /(?:\r?\n){2}$/.test(before);
    const endsAtBlockBoundary = after.length === 0 || /^(?:\r?\n){2}/.test(after);
    if (startsAtBlockBoundary && endsAtBlockBoundary) return true;

    from = index + 1;
  }
  return false;
}