Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
[gunosy.go]database/sql
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
ysekky
June 25, 2014
Programming
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
[gunosy.go]database/sql
ysekky
June 25, 2014
More Decks by ysekky
See All by ysekky
スタートアップの開発サイクルに学ぶ 研究活動の進め方 / research practices inspired by startup business strategy
ysekky
0
2.5k
[論文紹介] A Method to Anonymize Business Metrics to Publishing Implicit Feedback Datasets (Recsys2020) / recsys20-reading-gunosy-datapub
ysekky
3
2.9k
JSAI2020 OS-12 広告とAI オープニング / JSAI2020-OS-12-ads-and-ai-opening
ysekky
0
2.3k
JSAI2020インダストリアルセッション - Gunosyにおける研究開発 / jsai2020-gunosy-rd-examples
ysekky
1
840
ウェブサービス事業者における研究開発インターン[株式会社Gunosy] - テキストアナリティクスシンポジウム2019 / research-intern-case-study-at-gunosy
ysekky
0
3.1k
Gunosyにおけるニュース記事推薦/ news-recommendation-in-gunosy-webdbf2019
ysekky
1
1.6k
DEIM2019技術報告セッション - Gunosyの研究開発 / deim-2019-sponsor-session-gunosy-research
ysekky
0
1.3k
Analysis of Bias in Gathering Information Between User Attributes in News Application (ABCCS 2018)
ysekky
1
2.5k
世代による政治ニュース記事の閲覧傾向の違いの分析 - JSAI2018 / Analysis of differences in viewing behavior of politics news by age
ysekky
0
4.2k
Other Decks in Programming
See All in Programming
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
8
5.5k
WebRTC映像をAirPlayに対応させる挑戦.pdf
monolithic_adam
0
350
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
230
APNsからLive Activityを開始する話
yumnumm
0
150
プロダクトコードからライブラリの境界を見つける
elmetal
PRO
0
110
パズルゲームの作り方 / how to make puzzle games
kaityo256
PRO
2
270
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
480
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
780
SREの越境 / SRE Collaboration
y0hgi
2
300
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
470
R8 の設定見直しでアプリサイズ削減
andpad
0
190
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
350
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
2
11k
First, design no harm
axbom
PRO
2
1.3k
How to train your dragon (web standard)
notwaldorf
96
6.9k
A designer walks into a library…
pauljervisheath
211
25k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Docker and Python
trallard
47
4.2k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
370
How to Talk to Developers About Accessibility
jct
2
560
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
740
Ethics towards AI in product and experience design
skipperchong
2
420
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
390
Transcript
database package Yoshifumi Seki Gunosy.go #3 2014.06.25
自己紹介 • 関 喜史 • 株式会社Gunosy, 社会人博士在学中 •
分析系エンジニア – ユーザ行動解析/コンテンツ評価/情報推薦 • 普段はPython.最近Julia勉強中 • Go歴3日
database • database/sql • database/sql/driver • SQLベースのデータベースに対する包括的な インタフェースを提供している
• driverはsqlパッケージのインタフェースを各 データベースで使用するために実装するパッ ケージのインタフェースを定義している
今日の発表 • database/sql/driverはdriver作りたい人向けな のでゆるふわユーザにはあんまり関係ないと して割愛します>< • Database/sqlの使い方について紹介します
実際に動かしてみる • SQLiteのDriver • hKps://github.com/maKn/go-‐sqlite3/blob/ master/_example/simple/simple.go
func Open • sql.Open(driverName, dataSourceName) (*DB, error) – ex. sql.Open(“sqlite3”,
“./foo.csv”) – データベースに接続する – driverNameはdriverごとに規定されている – DB型の変数を返す • sqliteの場合 – ex: sql.Open(“sqlite3”, “../foo.csv”)
Openメソッド内でdriverNameをキーとしてdrivers変数にアクセスしている drivers変数はdatabase/sql内でグローバルに定義されている Registerメソッドによって各ドライバのdriverNameとともに設定される
maKn/go-‐sqlite3/sqlite3.goのinitで上記のように呼び出されている fo-‐sql-‐driver/mysql/driver.goのinitで上記のように呼び出されている driverNameは各ライブラリが固有で決める
type DB • func Begin • func Close
• func Driver • func Excec • func Ping • func Prepare • func Query • func QueryRow • func Set MaxIdleConns • func Set MaxOpenConns
Exec, Query, QueryRow • トランザクションを伴わない処理(SELECTなど)を実行する • プレースホルダは使えるが,driverのライブラリによって仕 様がことなることがある
• func(*DB) Exec(query string, args… interface{}) (Result, error) – 返り値を伴わない命令を実行する – Create Tableなど • func(*DB) Query(query string, args…, interface{})(*Rows, error) – 複数行を得る.通常のSELECT文などで用いる • func(*DB) QueryRow(query string, args…, interface{}) *Row – 最大で1つしか結果を得ないSELECT文などで用いる – エラーはRowにあるScanメソッドで取得する
Result, Rows, Row • type Result – LastInsertId() (int64, error)
– RowsAffected() (int64, error) • type Row – func(*Row) Scan(dest…interface {}) error • columnsをdestで指定した変数に渡す. • 1行以上マッチした場合は最初の1行が指定される • マッチしなかった場合はエラーを得る
Result, Rows, Row • type Rows – func(*Rows) Close()
error • Rowsを閉じてこれ以上イテレートできなくする • Next()でFalseが呼ばれると自動でCloseされる – func(*Rows) Columns() ([]string, error) • カラム名のリストを返す – func(*Rows) Err() error • イテレーション中に発生したエラーを返す – func(*Rows) Next() bool • Scanメソッドで読み込む行の参照を1つ進める – func(*Rows) Scan(dest… interface{}) error • 現在参照している行を読み込む
Example: Query
Example: QueryRow
• func (*DB) Close() error – DBを閉じる • func
(*DB) Driver() driver.Driver – driverを返す • func (*DB) Ping() error – connecgonが有効かを確認する • func(*DB) SetMaxIdleConns(n int) – idle connecgon poolの最大値を決める • func(* DB) SetMaxOpenConns(n int) – dbがopenするコネクション数の最大値を決める
Prepare • func(*DB) Parepare(query string) (* Stmt, error)
– Prepared statementを作る.
type Stmt • Prepared Statement • typeStmt – func(s
*Stmt) Close error – func(s *Stmt) Exec(args …interface{}) (Result, error) – func(s *Stmt) Query(args …interface{}) (*Rows, error) – func(s *Stmt) QueryRow(args …interface{}) *Row
func Begin • func (db *DB) Begin (*Tx, error)
– starts a transacgon • type Tx – in progress database transacgon • Beginでtransacgonを得る.InsertやUpdateな どのtransacgonを伴う処理はBeginで得た transacgonに対して行う
type Tx • In-‐progress database transacgon • CommitかRollbackで終了する
• Commit, Rollbackの後に操作すると ErrTxDoneが変える • type Tx – func(tx *Tx) Commit() error • トランザクションをコミットする – Func(tx *Tx) Exec(args …interface{}) (Result, error) – Func(tx Tx) Prepare(query string) (*Stmt, error) • Tx内でしか使えない – Func(tx *Tx) Query(query, args …interface{}) (*Rows, error) – Func(tx *Tx) QueryRow(args …interface{}) *Row – Func(tx *Tx) Rollback() error • トランザクションをロールバックする
Func(*Tx) Stmt(stmt *Stmt) *Stmt • Stmt returns a transacgon-‐specific prepared
statement from an exisgng stategme