sqlc
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSQLC Skill
SQLC 技能指南
This skill provides instructions for working with and database queries in the NCPS repository. NCPS supports multiple database engines (SQLite, PostgreSQL, MySQL), and is used to generate type-safe Go code from SQL queries for each engine.
sqlcsqlc本技能文档提供了在NCPS代码库中使用和进行数据库查询的相关说明。NCPS支持多种数据库引擎(SQLite、PostgreSQL、MySQL),用于为每个引擎从SQL查询生成类型安全的Go代码。
sqlcsqlcConfiguration
配置
- SQLC Config:
sqlc.yml - Queries:
- SQLite:
db/query.sqlite.sql - PostgreSQL:
db/query.postgres.sql - MySQL:
db/query.mysql.sql
- SQLite:
- Output:
- SQLite:
pkg/database/sqlitedb - PostgreSQL:
pkg/database/postgresdb - MySQL:
pkg/database/mysqldb
- SQLite:
- SQLC配置文件:
sqlc.yml - 查询文件:
- SQLite:
db/query.sqlite.sql - PostgreSQL:
db/query.postgres.sql - MySQL:
db/query.mysql.sql
- SQLite:
- 输出目录:
- SQLite:
pkg/database/sqlitedb - PostgreSQL:
pkg/database/postgresdb - MySQL:
pkg/database/mysqldb
- SQLite:
Workflow for Query Changes
查询变更工作流程
Any time a query file () is updated, you MUST follow these steps:
db/query.<engine>.sql每当更新查询文件()时,你必须遵循以下步骤:
db/query.<engine>.sql1. Generate SQLC Code
1. 生成SQLC代码
Run the command to update the generated Go files for all engines.
sqlc generatebash
sqlc generate运行命令来更新所有引擎对应的生成Go文件。
sqlc generatebash
sqlc generate2. Regenerate Database Wrappers and Models
2. 重新生成数据库包装器和模型
Run for the package. This command uses to automatically:
go generatepkg/databasegen-db-wrappers- Extract the interface from the
Querierbackend.postgresdb - Generate the common interface in
Querier.pkg/database/querier.go - Generate common domain models in .
pkg/database/models.go - Generate database wrappers (,
wrapper_sqlite.go,wrapper_postgres.go).wrapper_mysql.go
bash
go generate ./pkg/database[!IMPORTANT] Do NOT manually editorpkg/database/querier.go. They are fully automated.pkg/database/models.go
对包运行命令。该命令会使用自动完成以下操作:
pkg/databasego generategen-db-wrappers- 从后端提取
postgresdb接口。Querier - 在中生成通用的
pkg/database/querier.go接口。Querier - 在中生成通用领域模型。
pkg/database/models.go - 生成数据库包装器(、
wrapper_sqlite.go、wrapper_postgres.go)。wrapper_mysql.go
bash
go generate ./pkg/database[!IMPORTANT] 请勿手动编辑或pkg/database/querier.go文件,它们是完全自动化生成的。pkg/database/models.go
Best Practices
最佳实践
-
Consistency: Ensure that equivalent queries exist for all supported engines unless the feature is engine-specific.
-
Linting: Useto lint and format SQL files before running
sqlfluff.sqlc generatebashsqlfluff lint db/query.*.sql sqlfluff format db/query.*.sql
-
一致性:除非功能是特定引擎专属的,否则要确保所有支持的引擎都有对应的等效查询。
-
代码检查:在运行之前,使用
sqlc generate对SQL文件进行检查和格式化。sqlfluffbashsqlfluff lint db/query.*.sql sqlfluff format db/query.*.sql