Centralize per-database logic behind a single SqlDialect abstraction

未关闭
#1,317 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
冷清
技术栈
rust, sql
领域
backend, database

调研方向

Start with src/webserver/database/mod.rs and the listed match sites in sql.rs, parameter_extraction.rs, connect.rs, execute_queries.rs, csv_import.rs, and filesystem.rs; read the existing dialect and placeholder tests in src/webserver/database/sql.rs. The issue proposes a broad, incremental refactor, so first map which per-database behaviors each site owns. Done means those behaviors are centralized with exhaustive handling for every supported database, and the existing per-dialect tests still pass.

由索引模型根据 Issue 内容生成。

描述

Introduce a SqlDialect abstraction to make per-database logic a single source of truth

Per-database knowledge is currently scattered across many files and keyed on two parallel enums that nothing forces to agree: SupportedDatabase (8 variants: Sqlite, Duckdb, Oracle, Postgres, MySql, Mssql, Snowflake, Generic) and sqlx's AnyKind (5 variants). Because the dialect-specific data is spread over ~6 independent match sites, several keyed on the wrong enum, the compiler cannot enforce completeness: each site silently falls through to a _ default. Adding or fixing a dialect today means hunting down every site and hoping none was missed, and a missed arm is a silent correctness/data bug, not a compile error.

Proof: the references are real and scattered
$ grep -rcn -E "SupportedDatabase::(Sqlite|Postgres|MySql|Mssql|Duckdb|Oracle|Snowflake|Generic)" src | grep -v ':0' ; echo "total:" ; grep -rn -E "SupportedDatabase::(Sqlite|Postgres|MySql|Mssql|Duckdb|Oracle|Snowflake|Generic)" src | wc -l
src/webserver/database/sql.rs:37
src/webserver/database/sql/parameter_extraction.rs:9
src/filesystem.rs:3
total:      49

$ grep -rn -E "AnyKind::" src | wc -l
      24

49 per-variant SupportedDatabase references (37 in sql.rs alone) plus 24 AnyKind references, all to be kept consistent by hand.

The scattered sites

The enum mismatch directly produces fragile fallthroughs that only exist because the key type is wrong:

Proposed change

Introduce a single trait SqlDialect (or a method-rich impl on SupportedDatabase) that centralizes everything per-DB:

  • placeholder style and placeholder cast type
  • sqlparser Dialect
  • otel db.system.name
  • default pool size
  • capability flags: native JSON, COPY FROM STDIN, trace propagation, rollback statement, :: cast support, MSSQL concat rewriting

Crucially, key the placeholder data on SupportedDatabase and make DB_PLACEHOLDERS (and the other tables) an exhaustive match, so the compiler forces every dialect to be handled and the unreachable!/_ => Odbc/_ => Generic fallthroughs disappear. Adding or fixing a dialect then becomes "implement one trait / add the arm the compiler demands" instead of editing ~6 scattered sites that nothing forces to agree.

Risks and mitigations

  • Large but mechanical refactor. Mitigate by leaning on the existing dialect/placeholder tests in sql.rs (the ALL_DIALECTS table drives many of them) which already pin per-dialect behavior.
  • Land it incrementally, one capability at a time, so each step stays reviewable and bisectable.

Expected win

  • Single source of truth for per-DB behavior.
  • Compiler-enforced dialect completeness instead of hand-synced matches across two enums.
  • Removes the silent _ => Odbc / _ => Generic fallthroughs that can quietly mishandle (or corrupt data for) an unlisted dialect.

This refines the "split sql.rs by responsibility" suggestion in #1249: a SqlDialect abstraction is the natural seam to split along.

主要语言
Rust
星标
2.6k
派生
175
平均合并
14 小时 16 分钟
30 天内合并 PR
43

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

sqlpage/SQLPage 的其他 Issue

查看 sqlpage/SQLPage 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。