SQLC
SQLC 設定と使用リファレンス。
YAML 設定
version: "2"
sql:
- engine: "sqlite"
queries: "db/query.sql"
schema: "db/schema.sql"
gen:
go:
# 基本設定
package: "sqlite"
out: "db/sqlite"
# コード生成オプション
emit_interface: true # Querier インターフェースを生成
emit_json_tags: true # JSON タグを生成
emit_prepared_queries: true # プリペアドステートメントを使用
emit_exact_table_names: false # テーブル名をそのまま使用
emit_empty_slices: true # 空の結果は空のスライスを返す
emit_exported_queries: true # SQL クエリをエクスポート
emit_result_struct_pointers: false # 結果をポインタとして返す
emit_params_struct_pointers: false # パラメータをポインタとして渡す
# 列挙型関連
emit_enum_valid_method: true # 列挙型検証メソッドを生成
emit_all_enum_values: true # すべての列挙値リストを生成
# フィールド命名
emit_pointers_for_null_types: true # NULL 値にポインタを使用
# コメント生成
emit_file_header_comments: true # ファイルヘッダーコメント
emit_doc_comments: true # ドキュメントコメントを生成
# フレームワーク統合
emit_db_tags: true # db タグを生成設定オプションの説明
emit_interface
Querier interface を生成、依存性注入とテストに役立つ。
type Querier interface {
GetBook(ctx context.Context, id int64) (Book, error)
ListBooks(ctx context.Context) ([]Book, error)
}emit_json_tags
struct に json タグを追加、シリアライズ/デシリアライズに便利。
type Book struct {
ID int64 `json:"id"`
Title string `json:"title"`
}emit_prepared_queries
プリペアドステートメントを生成、パフォーマンスを向上、SQL インジェクションを防止。
emit_exact_table_names
| 値 | 効果 |
|---|---|
| false (推奨) | type Book struct {} |
| true | type Books struct {} |
emit_empty_slices
クエリ結果がない場合、nil ではなく空のスライスを返す。
// true: books = []Book{}
// false: books = nil
var books []Bookemit_exported_queries
SQL クエリを定数としてエクスポート、実際に実行される SQL を確認しやすい。
const getBookSQL = `SELECT * FROM books WHERE id = $1`emit_result_struct_pointers
| 値 | 効果 |
|---|---|
| false | func GetBook() (Book, error) |
| true | func GetBook() (*Book, error) |
emit_params_struct_pointers
true に設定すると、生成されたクエリメソッドはポインタ引数を使用。
func (q *Queries) GetUserByID(ctx context.Context, arg *GetUserByIDParams) (*User, error)
func (q *Queries) CreateUser(ctx context.Context, arg *CreateUserParams) (*User, error)emit_enum_valid_method
列挙型に Valid() メソッドを自動生成。
func (e UserStatus) Valid() bool {
switch e {
case UserStatusActive, UserStatusInactive, UserStatusSuspended:
return true
}
return false
}emit_all_enum_values
すべての列挙値を含むスライスを生成。
var AllSubscriptionTier = []SubscriptionTier{
SubscriptionTierFree,
SubscriptionTierBasic,
SubscriptionTierPremium,
SubscriptionTierEnterprise,
}Annotations
| Annotation | 説明 |
|---|---|
:one | 単一の結果を返す、WHERE 主キークエリでよく使用 |
:many | 複数の結果を返す、スライス型を返す |
:exec | 変更操作を実行、結果を返さない |
:execresult | sql.Result を返す(LastInsertId, RowsAffected) |
:execrows | 影響を受けた行数を返す |
:execlastid | INSERT を実行、自動増分 ID を返す |
:batchexec | バッチ DML を実行 |
:batchone | バッチで単一クエリ |
:batchmany | バッチで複数クエリ |
データベースサポート
| Annotation | PostgreSQL | SQLite | MySQL |
|---|---|---|---|
:one | ✅ | ✅ | ✅ |
:many | ✅ | ✅ | ✅ |
:exec | ✅ | ✅ | ✅ |
:execresult | ✅ | ✅ | ✅ |
:execrows | ✅ | ✅ | ✅ |
:execlastid | ✅ | ✅ | ✅ |
:batchexec | ✅ | ❌ | ❌ |
:batchmany | ✅ | ❌ | ❌ |
:batchone | ✅ | ❌ | ❌ |
- batch* シリーズは PostgreSQL + pgx/v4、pgx/v5 ドライバのみサポート
- SQLite の :execresult、:execrows 機能は SQLite 自体の制限を受ける
- すべての annotation で生成されるコードには context.Context パラメータが含まれる