Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
sqlcとLLMによる型安全なSQL生成
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
進捗ゼミ
March 20, 2025
0
260
sqlcとLLMによる型安全なSQL生成
D-Plus Osaka #2で登壇したときの資料です
進捗ゼミ
March 20, 2025
Tweet
Share
More Decks by 進捗ゼミ
See All by 進捗ゼミ
スキマAIでスライドを!
progresscicada
0
3
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
150
The agentic SEO stack - context over prompts
schlessera
0
720
Reality Check: Gamification 10 Years Later
codingconduct
0
2.1k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
430
Git: the NoSQL Database
bkeepers
PRO
432
67k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
10k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
910
KATA
mclloyd
PRO
35
15k
Documentation Writing (for coders)
carmenintech
77
5.3k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.1k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
260
The Cost Of JavaScript in 2023
addyosmani
55
9.8k
Transcript
sqlcとレイヤードアーキテクチャによ る 型安全なSQL生成 京都大学 足利聡太
自己紹介 名前:足利聡太 京都大学B4 Twitter:進捗ゼミ @ProgressSemi 最近の趣味:ポケポケ 技術書典18に向けて準備中… AIでシステム開発をスケールさせる本を書きました (技術書典オンラインマーケットで発売中)
レイヤードアーキテクチャ
目標 infraレイヤーの自動実装
前提条件 Entityとは? • Domainに属する意味を持つオブジェクト全般 • 例:動画配信サービスにおけるUser、Channel、Slot(放送枠) Infraの責務 データ・Entity変換
type UserRepository interface { GetUserByUserUID(ctx context.Context, tx Tx, userUID string)
(*entity.User, error) CreateUser(ctx context.Context, tx Tx, userUID string) (*entity.User, error) } これを書いたらあとは自動で完成してほしい!!
LLM 非常に賢く安価なコード生成 お世辞にもDXが良くないSQLから脱却できる 無条件で信用できない • ハルシネーション • 言語で指示するゆえの結果の不安定性 • いちいちプロンプトを書くのが辛い
sqlc SQLを実行する型付きGoコードを生成 生SQLを書かざるを得ない • 型エラーによりハルシネーションに強い • 生成したSQLを直接確認できるためデバッグが容易
いいとこどり しましょう
解決策:クエリ生成
解決策:コード生成
Why sqlc? 型安全:ハルシネーションを回避・精度向上 クエリ生成とコード生成を分離 • 精度向上 • レビューが容易 • 技術的負債を回避
AIは、ORMよりSQLに詳しい sqlcの動作にはDB全体のスキーマ定義が必要⇒AIに食わせる
interface定義 公式の静的解析ツールが豊富 • "go/ast" • "go/parser" • "go/printer" • "go/token"
「infraディレクトリ配下のXXXRepositoryという名前のインタ ーフェース」
Entityの取得 pkg/domain/entityにドメインモデルを配置 静的解析で定義を自動的に抽出する 「entity配下のファイル名と 同じ名前のExportedな型」
生成されたクエリ -- name: GetUserByUserUID :one SELECT id, created_at, updated_at, user_uid
FROM users WHERE user_uid = $1; -- name: CreateUser :exec INSERT INTO users (created_at, updated_at, user_uid) VALUES (UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), $1);
sqlcの生成物 type User struct { ID int64 CreatedAt int64 UpdatedAt
int64 UserUid string } const createUser = `-- name: CreateUser :exec INSERT INTO users (created_at, updated_at, user_uid) VALUES (UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), $1)` func (q *Queries) CreateUser(ctx context.Context, userUid string) error { _, err := q.db.ExecContext(ctx, createUser, userUid) return err }
あとはやるだけ!
静的解析による自動プログラミング 「関数の実装が完成した」で終わりではない! • var _ UserRepository = UserRepositoryImpl{}を検出し、 対応するメソッドを置き換える •
各関数のインポートするパッケージを収集し、importを生成 • 公式のフォーマッタ"golang.org/x/tools/imports"で整形 静的解析なので、確実に自動化が完了する
インターフェースを書くだけで 自動で実装が完成した!
デモは懇親会で!
おまけ:AIフレンドリーフレームワーク構想 アーキテクチャの制約があるとAIに有利 • pkg/domain/entityにEntityがある • infraディレクトリに実装対象の”XXXRepository”を実装する • schema.sqlにDBのスキーマが記述されている アーキテクチャ制約×特化したAIワークフロー コードを持たない次世代フレームワーク
ご清聴ありがとうございました