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
SQL Firstなsqlc
Search
yushin
October 14, 2025
74
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SQL Firstなsqlc
Go Night Talks – After Conference 2025/10/14 で発表した資料です。
yushin
October 14, 2025
Transcript
10 slides
SQL First なsqlc 1
自己紹介 名前:yushin 経歴:golang 約2 年, python, typescript, kotlin, swift, ruby...
余談: 最近購入したcornix というキーボードが非常にカッコよくて、使いや すくて気に入っています 会場に持って来ているので気になっている方はぜひ 2
sqlc とは何か SQL から型安全なコードを生成する sqlc.yaml config ファイル(connection, engine... ) PostgreSQL,
MySQL, SQLite schema.sql database schema query.sql CRUD 等のquery を記載 3
schema Author schema.sql CREATE TABLE authors ( id BIGSERIAL PRIMARY
KEY, name text NOT NULL, bio text, created_at timestamp without time zone DEFAULT clock_timestamp() NOT NULL ); 4
schema Author models.go type Author struct { ID int64 Name
string Bio pgtype.Text CreatedAt time.Time } 内部でpgx/v5 というPostgreSQL driver を使用することができます。 pgtype.Text はNullable を表現しています。 5
query GetAuthor query.sql -- name: GetAuthor :one SELECT * FROM
authors WHERE id = $1 LIMIT 1; query.sql.go const getAuthor = `-- name: GetAuthor :one SELECT id, name, bio, created_at FROM authors WHERE id = $1 LIMIT 1 ` 6
query GetAuthor query.sql.go func (q *Queries) GetAuthor(ctx context.Context, id int64)
(Author, error) { row := q.db.QueryRow(ctx, getAuthor, id) var i Author err := row.Scan( &i.ID, &i.Name, &i.Bio, &i.CreatedAt, ) return i, err } 7
sqlc の特徴・比較 SQL スキーマ・クエリをもとにコードを生成 → go のコードとスキーマが完全一致する → コード生成時、またはコンパイル時にエラーを検知できる vs
GORM → Go の構造体または gorm タグを元にSQL を生成 → スキーマとズレがある場合、実行時にエラーを検知 vs sqlx → db タグを元に構造体へマッピング → SQL と構造体にズレがある場合、実行時にエラーを検知 8
sqlc メリット・デメリット メリット スキーマ・クエリを元に生成することによる型安全性 table 名, カラム名のタイポにも気づくことができる SQL クエリが .sql
ファイルとして分離されるので、SQL が見やすい デメリット 動的なクエリの構築が苦手 共通化やチェーンなどができない マイグレーションやフックなどの便利機能がない 9
ご清聴ありがとうございました 10
More Decks by yushin
See All by yushin
gopls で知る言語サーバー
yushin
0
440
Featured
See All Featured
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
490
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
450
The SEO identity crisis: Don't let AI make you average
varn
0
630
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
890
Designing Experiences People Love
moore
143
24k
Speed Design
sergeychernyshev
33
2.3k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
From π to Pie charts
rasagy
1
460
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.7k
Making Projects Easy
brettharned
120
6.9k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
590
The Limits of Empathy - UXLibs8
cassininazir
2
800