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
[gunosy.go]database/sql
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
830
ウェブサービス事業者における研究開発インターン[株式会社Gunosy] - テキストアナリティクスシンポジウム2019 / research-intern-case-study-at-gunosy
ysekky
0
3k
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.1k
Other Decks in Programming
See All in Programming
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
ドリフトを絶対に許さない(?)CDK運用 / CDK Ops with Zero Tolerance for Drifts (?)
akihisaikeda
1
180
Cloudflare is Agents
chimame
0
150
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
460
数百円から始めるRuby電子工作
tarosay
0
140
Google Apps Script で Ruby を動かす
kawahara
0
140
今さら聞けない .NET CLI
htkym
0
190
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
210
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
4.2k
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
210
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
150
Featured
See All Featured
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
360
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Agile that works and the tools we love
rasmusluckow
331
22k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
410
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
330
Chasing Engaging Ingredients in Design
codingconduct
0
260
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
360
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
430
The SEO Collaboration Effect
kristinabergwall1
1
520
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