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
database/sqlでNullを扱う歴史とsql.Null[T]の登場 / sql.Nul...
Search
mackee
May 17, 2024
Programming
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
database/sqlでNullを扱う歴史とsql.Null[T]の登場 / sql.Null[T] history
kamakura.go #6
https://kamakurago.connpass.com/event/316438/
mackee
May 17, 2024
More Decks by mackee
See All by mackee
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
870
perlをWebAssembly上で動かすと何が嬉しいの??? / Where does Perl-on-Wasm actually make sense?
mackee
0
1.5k
Agentに至る道 〜なぜLLMは自動でコードを書けるようになったのか〜
mackee
6
6.8k
今!ソフトウェアエンジニアがハードウェアに手を出すには
mackee
14
7.6k
ワンバイナリWebサービスのススメ
mackee
10
9.3k
tanukistack ライブコーディング / tanukistack live-coding
mackee
0
950
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
1.2k
perl for shell, awk and sed programmers
mackee
3
3k
今更GoのWebフレームワークを作ろうとしているワケ / Why am I trying to create a Go web framework now?
mackee
1
1.2k
Other Decks in Programming
See All in Programming
phperだってパンチカードでコーディングしたい
o0h
PRO
0
460
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
310
Next.js: The final boss of Web Performance
guaca
0
340
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
240
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
210
No More NoSuchMethodError
exoego
1
110
続・コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions-again
handlename
0
130
Modernizing iOS Apps
seyfoyun
0
120
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
4
1.2k
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3k
AmebaブログはKMPから卒業します
kmars0605
0
240
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
140
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
37
7.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Designing for Timeless Needs
cassininazir
1
530
First, design no harm
axbom
PRO
2
1.4k
Mobile First: as difficult as doing things right
swwweet
225
10k
Paper Plane (Part 1)
katiecoart
PRO
2
12k
Building Adaptive Systems
keathley
44
3.3k
The SEO identity crisis: Don't let AI make you average
varn
0
610
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
580
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
450
Test your architecture with Archunit
thirion
2
2.5k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
470
Transcript
database/sqlͰNULLΛѻ͏ ྺ࢙ͱsql.Null[T]ͷొ kamakura.go #6 @mackee_w a.k.a macopy 2024-05-17
Δਓ • X: @mackee_w • GitHub: @mackee • ໘ന๏ਓΧϠοΫ •
Tonamel αʔόʔαΠυΤϯδχΞ
database/sqlͰNULLΛѻ͏ ͱʁ
database/sqlͰNULLΛѻ͏
database/sqlͰNULLΛѻ͏ • sql.Null**ͱ͍͏ܕͰϥοϓͯ͠ग़͠ೖΕΛ͢Δ • ϙΠϯλܕͰͳ͍(͕ɺϙΠϯλܕͷnilΛαϙʔτ͍ͯ͠Δέʔε͋Δ) • sql.Null**Validͱ͍͏ϑΟʔϧυ͕͋Δ • Valid͕falseͩͱNull, trueͩͱNullͰͳ͍
• ValidͱผʹΛ֨ೲ͢ΔϑΟʔϧυ͕͋Δ • sql.NullStringͰ͋Ε `String string`, sql.NullInt64Ͱ͋Ε`Int64 int64`
ͦͦdatabase/sqlͰѻ͏ͬͯͳΜͩʁ • جຊతʹҎԼͷinterface͕࣮͞Ε͍ͯΔܕ • SQLͷͱͯ͠͏߹ database/sql/driver.Valuer • ΫΤϦͷ݁Ռ͔Βड͚औͬͨͷΛGoͷมʹόΠϯυ͢Δ߹ɺ database/sql.Scanner •
GoͷϓϦϛςΟϒܕdriverྀ͕͍ͯ͠Δܕͦͷ··ಥͬࠐΊΔ • ޙऀͷྫ: json.RawMessage(go-sql-driver/mysql)
driver.ValuerͰNULLΛSQLʹೖΕΔʹ • driver.Valuer.Value()͕nilΛฦͤྑ͍ • sql.Null**ͳͲͦ͏ͳ͍ͬͯΔ • driver.Valuer.Value()͕typed-nilΛฦͨ͠߹ʁ • driver͘͠database/sql/driverͰuntyped-nilʹม͞Ε͍ͯΔ ͷͰηʔϑ
• Ͱuntyped-nilΛฦͨ͠ํ͕(͓ߦّ)ྑ͍Ͱ͢Ͷ
driver.ScanͰNULLΛड͚Δʹʁ • driver.Scanner.Scanʹuntyped-nil͕ೖͬͯ͘ΔͷͰɺͦΕͰValidͷ ఆΛߦ͏ • sql.Null**ʹnil͕ೖͬͨ߹Validʹfalse͕ೖΓɺ͕ೖΔ fi eldʹθ ϩ͕ೖΔΑ͏ʹͳ͍ͬͯΔ •
driver.ScannerͰͳ͘ϙΠϯλܕͰnullΛड͚ͨΒnil͕ೖΔ • Ͱθϩnilͱݟ͚͕͔ͭͳ͍͔ʁ ؾ͕͢Δ͚ͩͳͷͰ۩ମతͳ έʔε͕͋ͬͨΒڭ͍͑ͯͩ͘͞
database/sqlʹ͓͚Δsql.Null**ͷྺ࢙ • sql.NullStringdatabase/sqlύοέʔδ͕࡞ΒΕͨ࣌Ͱଘࡏ͢Δ(2011/09/29) • ࣌sql.NullableStringͱ͍͏໊લͰScannerInfo interface͕࣮͞Ε͍ͯͨ • sql.NullInt64, sql.NullFloat64, sql.BoolՃ(2012/01/26)
• ͜ͷޙgo1͕ϦϦʔε • sql.NullTimeՃ(2019/04/06), sql.NullInt32Ճ(2019/04/27), sql.NullInt16, sql.NullBytesՃ(2021/05/05)
Β͘sql.NullTime͕ͳ͔ͬͨ • ͳ͔ͬͨͷͰgo-sql-driver/mysql.NullTimeΛ͍ͬͯͨਓ͍ͨͰ ͠ΐ͏(2013/04/26͔Βଘࡏ) • ͦͷଞsql.Null**(͜͜ʹҙͷܕ͕ೖΔ)͕ཉ͍͠ํ͍ͬͺ͍͍ͨͰ ͠ΐ͏ • ϥΠϒϥϦͳΓࣗલ࣮ͳΓ͕ඞཁͰ͋ͬͨ… •
ͦΕͦΕͰϓϩδΣΫτ͝ͱʹҧ͏ͱ͍͏ࠞཚͷͱʹ…
from Go1.22 Release Notes The new Null[T] type provide a
way to scan nullable columns for any column types.
sql.Null[T]ͱ • sql.Null[T(͜͜ʹҙͷܕ͕ೖΔ)]ͷΑ͏ʹ༻͢Δ͜ͱͰɺsql.Null**ͱಉ͡ޮ ྗΛ࣋ͭܕ • sql.Null**Λ૿͞ͳͯ͘ྑ͘ͳͬͨʂ • user de fi
ned typeͱͷΈ߹Θ͕ͤྑ͍ • type HogeID uint64 ͷΑ͏ͳIDܕ࡞ͬͨ߹ʹɺͦͷNullableͳܕͱͯ͠ `sql.Null[HogeID]`͕͑Δ • methane͞ΜฒͼʹGoνʔϜͷօ͞Μ͋Γ͕ͱ͏ʂ
FAQ • Q: ࣮࣭Option/MaybeܕͰʁ • A: IssueʹͦΜͳ͕ٞ͋Γ·͕ͨ͠ɺSQLΛॻ͘ͱ͖ɺDB͔Βͷ ΫΤϦ݁ՌΛಡΉ࣌ʹͷΈ͍·͠ΐ͏Ͷ • Q:
͜Ε͕ೖͬͨܦҢʁ • A: ͱͱsql.NullUInt64ͷఏҊ͕͞Εͨ࣌ʹʮδΣωϦΫεΛͬ ͯͶʯͱ͍͏ʹͳ͍ͬͯͨ
Ҏ্ʂ