← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.4.a.4 · 生成模型:GPT-6

在编译期检查SQL查询

理解等级:中级

SQLx 提供面向关系数据库的 Rust 查询接口。使用 sqlx::query!(...) 时,可让 SQL 语句连同绑定参数进入宏提供的静态检查。普通 query(...) 函数也能发送查询,但不走这个宏的检查路径。检查依据来自构建时可用的目标数据库信息或准备好的离线元数据。宏让查询和参数问题有机会在构建阶段暴露,运行时仍然需要连接数据库执行。

书中展示的重点是宏入口与运行时函数的选择。具体查询语法和参数占位符应按数据库方言检查。 边界核对:SQLx query! 文档

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第05章《Hiding information and creating mini-DSLs with function-like macros》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-5.xhtml#p95) - 结论依据:第 5.5 节直接对照 SQLx 的 query 方法和 query! 宏,前者提供查询,后者开放编译期检查。数据来源条件由 SQLx 文档核对。 - 原始内容:Without going into detail, here are a few examples of crates that put macros to creative use. SQLx (https://github.com/launchbadge/sqlx) is a “Rust SQL crate featuring compile-time checked queries,” a common choice for interacting with relational databases when you do not want to add the complexity of an object-relational mapping.