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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
880
perlをWebAssembly上で動かすと何が嬉しいの??? / Where does Perl-on-Wasm actually make sense?
mackee
0
1.6k
Agentに至る道 〜なぜLLMは自動でコードを書けるようになったのか〜
mackee
6
6.9k
今!ソフトウェアエンジニアがハードウェアに手を出すには
mackee
14
7.7k
ワンバイナリWebサービスのススメ
mackee
10
9.3k
tanukistack ライブコーディング / tanukistack live-coding
mackee
0
980
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
Distance field of Julia set
kinakomoti321
0
300
Spec-driven Development with AI
simas
PRO
0
2.3k
Event is what happens
moro
2
3.8k
Kotlinize Is Not Just Converting to Kotlin
chigichan24
1
220
Seven New Principles of Software Engineering
yegor256
0
460
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
230
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
3
1.7k
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
410
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
220
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
600
小さなアプリを作って公開するまで
onozaty
0
140
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
15
11k
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
350
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.3k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Designing for Timeless Needs
cassininazir
1
550
The Language of Interfaces
destraynor
162
27k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
470
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
70
68k
Site-Speed That Sticks
csswizardry
13
1.6k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
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ͷఏҊ͕͞Εͨ࣌ʹʮδΣωϦΫεΛͬ ͯͶʯͱ͍͏ʹͳ͍ͬͯͨ
Ҏ্ʂ