Last active
October 11, 2016 09:41
-
-
Save tenntenn/6789296 to your computer and use it in GitHub Desktop.
[Go言語] database/sqlパッケージを使ってみた ref: http://qiita.com/tenntenn/items/dddb13c15643454a7c3b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go get "github.com/go-sql-driver/mysql" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ "github.com/go-sql-driver/mysql" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cnnPool chan *sql.DB | |
func main() { | |
cnnPool = make(chan *sql.DB, 10) | |
for i := 0; i < cap(cnnPool); i++ { | |
cnnPool <- sql.Open("mysql", "user:password@tpc(host:port)/dbname") | |
} | |
} | |
func getFromDB() { | |
cnn := <-cnnPool | |
defer func() { | |
cnnPool <- cnn | |
}() | |
// cnnを使った処理 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cnn := sql.Open("mysql", "user:password@tcp(host:port)/dbname") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id := 100 | |
var name string | |
if err := cnn.QueryRow("SELECT name FROM person WHERE id = ?LIMIT 1", id).Scan(&name); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(id, name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rows, err := cnn.Query("SELECT id, name FROM person") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer rows.Close() | |
for rows.Next() { | |
var id int | |
var name string | |
if err := rows.Scan(&id, &name); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(id, name) | |
} | |
if err := rows.Err(); err != nil { | |
log.Fatal(err) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result, err := cnn.Exec("UPDATE person SET name = ? WHERE id = ?", "Hogera", 100) | |
if err != nil { | |
log.Fatal(err) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Result interface { | |
LastInsertId() (int64, error) | |
RowsAffected() (int64, error) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tx, err := cnn.Begin() | |
if err != nil { | |
log.Fatal(err) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tx.Commit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func hoge(tx *sql.Tx) { | |
defer func() { | |
// panicがおきたらロールバック | |
if err := recover(); err != nil { | |
tx.Rollback() | |
} | |
}() | |
// … | |
tx.Exec(…) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment