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
1.1k
いまさらdatabase/sql / 2018-12-18
golang.tokyo #20
rock619
December 18, 2018
Tweet
Share
Other Decks in Technology
See All in Technology
整頓のジレンマとの戦い〜Tidy First?で振り返る事業とキャリアの歩み〜/Fighting the tidiness dilemma〜Business and Career Milestones Reflected on in Tidy First?〜
bitkey
2
16k
Should Our Project Join the CNCF? (Japanese Recap)
whywaita
PRO
0
340
Tokyo_reInforce_2025_recap_iam_access_analyzer
hiashisan
0
180
いつの間にか入れ替わってる!?新しいAWS Security Hubとは?
cmusudakeisuke
0
120
開発生産性を測る前にやるべきこと - 組織改善の実践 / Before Measuring Dev Productivity
kaonavi
9
4.3k
SmartNewsにおける 1000+ノード規模 K8s基盤 でのコスト最適化 – Spot・Gravitonの大規模導入への挑戦
vsanna2
0
130
Flutter向けPDFビューア、pdfrxのpdfium WASM対応について
espresso3389
0
130
Backlog ユーザー棚卸しRTA、多分これが一番早いと思います
__allllllllez__
1
150
2025 AWS Jr. Championが振り返るAWS Summit
kazukiadachi
0
110
Beyond Kaniko: Navigating Unprivileged Container Image Creation
f30
0
130
Lufthansa ®️ USA Contact Numbers: Complete 2025 Support Guide
lufthanahelpsupport
0
190
生成AI開発案件におけるClineの業務活用事例とTips
shinya337
0
250
Featured
See All Featured
Done Done
chrislema
184
16k
Making Projects Easy
brettharned
116
6.3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.4k
Git: the NoSQL Database
bkeepers
PRO
430
65k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
54k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
RailsConf 2023
tenderlove
30
1.1k
A Tale of Four Properties
chriscoyier
160
23k
How to train your dragon (web standard)
notwaldorf
95
6.1k
Writing Fast Ruby
sferik
628
62k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
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