Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
sqlcとLLMによる型安全なSQL生成
Search
進捗ゼミ
March 20, 2025
320
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
sqlcとLLMによる型安全なSQL生成
D-Plus Osaka #2で登壇したときの資料です
進捗ゼミ
March 20, 2025
More Decks by 進捗ゼミ
See All by 進捗ゼミ
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
440
スキマAIでスライドを!
progresscicada
0
33
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
540
Ruling the World: When Life Gets Gamed
codingconduct
0
340
エンジニアに許された特別な時間の終わり
watany
109
250k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.1k
Visualization
eitanlees
152
17k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Building AI with AI
inesmontani
PRO
1
1.2k
Scaling GitHub
holman
464
140k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
RailsConf 2023
tenderlove
30
1.5k
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ワークフロー コードを持たない次世代フレームワーク
ご清聴ありがとうございました