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.1k
Other Decks in Programming
See All in Programming
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.5k
Webの地図
yosuke_furukawa
PRO
6
4.3k
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
220
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
150
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
56
38k
AHC070解法紹介
eijirou
0
110
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
Foundry Localでエージェント開発
seosoft
0
180
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
130
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
450
スマート反転とウェブアクセシビリティ
camiha
0
200
信頼性の目標を誰も求めてない
shubox
0
510
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
225
10k
AI: The stuff that nobody shows you
jnunemaker
PRO
9
990
Google's AI Overviews - The New Search
badams
0
1.6k
Speed Design
sergeychernyshev
33
2.1k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
WENDY [Excerpt]
tessaabrams
13
39k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The Invisible Side of Design
smashingmag
301
52k
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