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
進捗ゼミ
March 20, 2025
290
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
1
110
スキマAIでスライドを!
progresscicada
0
11
Featured
See All Featured
Building Applications with DynamoDB
mza
96
7.2k
Marketing to machines
jonoalderson
1
5.6k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
270
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
380
Faster Mobile Websites
deanohume
310
32k
Prompt Engineering for Job Search
mfonobong
0
390
Designing Experiences People Love
moore
143
24k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
For a Future-Friendly Web
brad_frost
183
10k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
920
YesSQL, Process and Tooling at Scale
rocio
174
15k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
620
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ワークフロー コードを持たない次世代フレームワーク
ご清聴ありがとうございました