sqlc
Working with sqlc and database queries
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
SQLC Skill
This skill provides instructions for working with sqlc and database queries in the NCPS repository. NCPS supports multiple database engines (SQLite, PostgreSQL, MySQL), and sqlc is used to generate type-safe Go code from SQL queries for each engine.
Configuration
-
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:
Workflow for Query Changes
Any time a query file (db/query.<engine>.sql) is updated, you MUST follow these steps:
1. Generate SQLC Code
Run the sqlc generate command to update the generated Go files for all engines.
sqlc generate
2. Regenerate Database Wrappers and Models
Run go generate for the pkg/database package. This command uses gen-db-wrappers to automatically:
- Extract the
Querierinterface from thepostgresdbbackend. - Generate the common
Querierinterface inpkg/database/querier.go. - Generate common domain models in
pkg/database/models.go. - Generate database wrappers (
wrapper_sqlite.go,wrapper_postgres.go,wrapper_mysql.go).
go generate ./pkg/database
[!IMPORTANT] Do NOT manually edit
pkg/database/querier.goorpkg/database/models.go. They are fully automated.
Best Practices
-
Consistency: Ensure that equivalent queries exist for all supported engines unless the feature is engine-specific.
-
Linting: Use
sqlfluffto lint and format SQL files before runningsqlc generate.sqlfluff lint db/query.*.sql sqlfluff format db/query.*.sql
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!