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.2k
1
Share
いまさらdatabase/sql / 2018-12-18
golang.tokyo #20
rock619
December 18, 2018
Other Decks in Technology
See All in Technology
AIコーディング時代における、ソフトウェアサプライチェーン攻撃に対する防衛術(簡易版)
soysoysoyb
0
200
AgentCore×VPCでの設計パターンn選と勘所
har1101
4
360
AgentCore Managed Harness を使ってみよう
yakumo
2
280
ハーネスエンジニアリングをやりすぎた話 ~そのハーネスは解体された~
gotalab555
5
1.9k
小説執筆のハーネスエンジニアリング
yoshitetsu
0
880
生成AI時代のドキュメントに対する期待の整理と実践から得た学び / Rethinking Documentation for LLM: Lessons from Practice
bitkey
PRO
1
120
はじめての MagicPod生成AI機能 機能紹介から活用方法まで
magicpod
0
120
Choose your own adventure in agentic design patterns
glaforge
0
160
AIはハッカーを減らすのか、増やすのか?──現役ホワイトハッカーから見るAI時代のリアル【MEGU-Meet】
cscengineer
PRO
0
240
Keeping Ruby Running on Cygwin
fd0
0
190
Digital Independence: Why, When and How
wannesrams
0
190
Route 53 Global Resolver で高額課金発生!
otanikohei2023
0
130
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.2k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
200
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.3k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
530
Paper Plane
katiecoart
PRO
1
49k
Visualization
eitanlees
150
17k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
430
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
250
Agile that works and the tools we love
rasmusluckow
331
21k
A designer walks into a library…
pauljervisheath
211
24k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.4k
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