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
Welcome to the LLM Club
koic
0
170
“社内”だけで完結していた私が、AWS Community Builder になるまで
nagisa53
1
390
エンジニア向け技術スタック情報
kauche
1
250
SalesforceArchitectGroupOsaka#20_CNX'25_Report
atomica7sei
0
170
LinkX_GitHubを基点にした_AI時代のプロジェクトマネジメント.pdf
iotcomjpadmin
0
170
Oracle Audit Vault and Database Firewall 20 概要
oracle4engineer
PRO
3
1.7k
Github Copilot エージェントモードで試してみた
ochtum
0
100
Delegating the chores of authenticating users to Keycloak
ahus1
0
120
Observability в PHP без боли. Олег Мифле, тимлид Altenar
lamodatech
0
350
AIエージェント最前線! Amazon Bedrock、Amazon Q、そしてMCPを使いこなそう
minorun365
PRO
14
5.1k
Prox Industries株式会社 会社紹介資料
proxindustries
0
290
生成AI時代の開発組織・技術・プロセス 〜 ログラスの挑戦と考察 〜
itohiro73
0
150
Featured
See All Featured
Building an army of robots
kneath
306
45k
Optimizing for Happiness
mojombo
379
70k
Code Reviewing Like a Champion
maltzj
524
40k
A Tale of Four Properties
chriscoyier
160
23k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Making Projects Easy
brettharned
116
6.3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
930
BBQ
matthewcrist
89
9.7k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
The Art of Programming - Codeland 2020
erikaheidi
54
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