Upgrade to Pro — share decks privately, control downloads, hide ads and more …

database/sqlでNullを扱う歴史とsql.Null[T]の登場 / sql.Nul...

database/sqlでNullを扱う歴史とsql.Null[T]の登場 / sql.Null[T] history

mackee

May 17, 2024
Tweet

More Decks by mackee

Other Decks in Programming

Transcript

  1. driver.ScanͰNULLΛड͚Δʹ͸ʁ • driver.Scanner.Scanʹuntyped-nil͕ೖͬͯ͘ΔͷͰɺͦΕͰValidͷ൑ ఆΛߦ͏ • sql.Null**ʹnil͕ೖͬͨ৔߹͸Validʹfalse͕ೖΓɺ஋͕ೖΔ fi eldʹ͸θ ϩ஋͕ೖΔΑ͏ʹͳ͍ͬͯΔ •

    driver.ScannerͰ͸ͳ͘ϙΠϯλܕͰ΋nullΛड͚ͨΒnil͕ೖΔ • Ͱ΋θϩ஋nilͱݟ෼͚͕͔ͭͳ͍͔΋ʁ ؾ͕͢Δ͚ͩͳͷͰ۩ମతͳ έʔε͕͋ͬͨΒڭ͍͑ͯͩ͘͞
  2. from Go1.22 Release Notes The new Null[T] type provide a

    way to scan nullable columns for any column types.
  3. 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νʔϜͷօ͞Μ͋Γ͕ͱ͏ʂ
  4. FAQ • Q: ࣮࣭Option/MaybeܕͰ͸ʁ • A: Issueʹ΋ͦΜͳٞ࿦͕͋Γ·͕ͨ͠ɺSQLΛॻ͘ͱ͖ɺDB͔Βͷ ΫΤϦ݁ՌΛಡΉ࣌ʹͷΈ࢖͍·͠ΐ͏Ͷ • Q:

    ͜Ε͕ೖͬͨܦҢ͸ʁ • A: ΋ͱ΋ͱsql.NullUInt64ͷఏҊ͕͞Εͨ࣌ʹʮδΣωϦΫεΛ଴ͬ ͯͶʯͱ͍͏࿩ʹͳ͍ͬͯͨ