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
いまさらdatabase/sql / 2018-12-18
Search
rock619
December 18, 2018
Technology
1
1k
いまさらdatabase/sql / 2018-12-18
golang.tokyo #20
rock619
December 18, 2018
Tweet
Share
Other Decks in Technology
See All in Technology
require(ESM)とECMAScript仕様
uhyo
4
1k
社内アプリで Cloudflare D1を プロダクト運用してみた体験談(Tokyo)
haochenx
0
130
Gitlab本から学んだこと - そーだいなるプレイバック / gitlab-book
soudai
7
1.4k
Secrets of a PowerShell "Guru"
guyrleech
1
110
One engineer company with Ruby on Rails
rstankov
2
470
M5と自作基板をくっつけてみた〜M5 Japan Tour 2024 Spring 福冈 (Fukuoka|福岡)〜
keropiyo
1
250
.NET Profiler in 2024.
kkamegawa
2
2.5k
成長をサポートするピープルマネジメントのやり方
sioncojp
9
1.4k
Google Cloud Next '24 Recap(Cloud Run/k8s)
mokocm
0
370
自己改善からチームを動かす! 「セルフエンジニアリングマネージャー」のすゝめ
shoota
6
1.1k
TiDBにおけるテーブル設計と最適化の事例
cygames
0
230
ルーターでプレゼンする
puhitaku
1
3.4k
Featured
See All Featured
Visualization
eitanlees
137
14k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
26
2.3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
A better future with KSS
kneath
231
16k
Designing the Hi-DPI Web
ddemaree
276
33k
A designer walks into a library…
pauljervisheath
201
23k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
34
6.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
356
18k
Code Review Best Practice
trishagee
56
15k
Six Lessons from altMBA
skipperchong
22
3k
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
Transcript
いまさら database/sql いまさら database/sql 2018-12-18 rock619
DB 接続 DB 接続 import ( "database/sql" _ "github.com/go-sql-driver/mysql" )
func main() { db, err := sql.Open("mysql", "user:password@protocol(host: if err != nil { panic(err) } defer db.Close() }
database driver の import database driver の import database/sql/driver パッケージ
database driver のinterface が定義されている 使用するdriver のimport driver のinterface の実装 github.com/go-sql-driver/mysql/
sql.DB sql.DB コネクションのプールを管理してくれる 複数のゴルーチンから使っても安全
sql.Open() sql.Open() 1 回だけでいい いちいちClose() する必要はない コネクションが作られるわけではない 接続できるかはPing() で確かめる必要あり
Ping() Ping() err = db.Ping() if err != nil {
// do something here }
database/sql/driver database/sql/driver Queryer...? type Queryer interface { Query(query string, args
[]Value) (Rows, error) } https://www.google.com/search?q=queryer
QueryRow() QueryRow() var name string err = db.QueryRow("select name from
users where id = ?", 1).Sc if err != nil { return err } fmt.Println(name)
QueryRow() QueryRow() var name string err = db.QueryRow("select name from
users where id = ?", 1).Sc switch err { case sql.ErrNoRows: return "" case nil: return name default: return err }
Null Null type NullString struct { String string Valid bool
// Valid is true if String is not NULL }
Scanner Scanner // Scan implements the Scanner interface. func (ns
*NullString) Scan(value interface{}) error { if value == nil { ns.String, ns.Valid = "", false return nil } ns.Valid = true return convertAssign(&ns.String, value) }
Valuer Valuer // Value implements the driver Valuer interface. func
(ns NullString) Value() (driver.Value, error) { if !ns.Valid { return nil, nil } return ns.String, nil }
sql.DBStats sql.DBStats MaxOpenConnections SetMaxOpenConns() で設定できる デフォルト: 0 (0 以下だと無制限)
sql.DBStats sql.DBStats 1.11 で追加されたもの OpenConnections コネクション数 InUse 使用中のコネクション数 Idle OpenConnections
- InUse
sql.DBStats sql.DBStats WaitCount コネクション待ちが発生した回数 WaitDuration 待った総時間 MaxIdleClosed MaxIdleConns を超えたためclose されたコネク
ション数 MaxLifetimeClosed MaxLifetime を超えたためclose されたコネクシ ョン数
参考 参考 https://golang.org/pkg/database/sql/ https://golang.org/pkg/database/sql/driver/ https://github.com/golang/go/wiki/SQLInterface