DATABASE/SQL
• github.com/VividCortex
• @xaprb
• baron@vividcortex.com
• linkedin.com/in/xaprb
Optimization, Backups,
Replication, and ...
• Docs: golang.org/pkg/database/sql/
• Tutorial: go-database-sql.org/
• MySQL Driver: github.com/go-sql-driver/mysql/
RESO...
THINGS
DB
• Not a connection.
• Open() Close()
• Query() QueryRow() Exec()
ROWS
• Next()
• Scan()
PATTERNS
package main
import (
! _ "github.com/go-sql-driver/mysql"
! "database/sql"
! "log"
)
func main() {
! db, err := sql.Open(...
var str string
err = db.QueryRow(
"select hello from world where id = 1").
Scan(&str)
if err != nil && err != sql.ErrNoRow...
q := "select id, hello from world where id = ?"
rows, err := db.Query(q, 1)
if err != nil {
! log.Fatal(err)
}
defer rows....
for rows.Next() {
! // scan
}
if err = rows.Err(); err != nil {
rows.Close()
log.Fatal(err)
}
stmt, err := db.Prepare(
! "select id, hello from world where id = ?")
if err != nil {
! log.Fatal(err)
}
defer stmt.Close...
stmt, err := db.Prepare(
! "insert into world(hello) values(?)")
if err != nil {
! log.Fatal(err)
}
defer stmt.Close()
res...
THINGSTO KNOW
THINGSTO KNOW
• There is a connection pool.
• You can’t use uint64 with high bit set in parameters.
DON’T BE LAZY
// Don’t do this!
for i := 0; i < 50; i++ {
! _, err := db.Query("DELETE FROM hello.world")
}
// Use this instead!
for i :...
MORE HELPFUL ADVICE
• Don’t defer() in long-running functions.
• Don’t use cxn state. UseTx to bind to a connection.
• Don...
SURPRISES
RETRY. RETRY. RETRY. RETRY. RETRY.
RETRY. RETRY. RETRY. RETRY. RETRY.
// Exec executes a query without returning any
rows.
// The args are for any placeholder parameters in
the query.
func (db...
PREPARED STATEMENTS
NULL
This page intentionally left blank.
var s sql.NullString
err := db.QueryRow(
"SELECT name FROM foo WHERE id=?",
id).Scan(&s)
if s.Valid {
// use s.String
} el...
CUSTOMTYPES
CUSTOMTYPES
• You can implement theValuer and Scanner interfaces.
• Why?Transparently transform data in <=> out of the DB....
RESOURCES
• http://golang.org/pkg/database/sql/
• http://go-database-sql.org/
• https://github.com/go-sql-driver/mysql
• h...
IMAGE CREDITS
• http://www.flickr.com/photos/simens/6306917636/
• http://www.flickr.com/photos/dexxus/5794905716/
• http://w...
Upcoming SlideShare
Loading in...5
×

Go database/sql

2,912

Published on

I will show how to use Go's database/sql package, with MySQL as an example. Although the documentation is good, it's dense. I'll discuss idiomatic database/sql code, and cover some topics that can save you time and frustration, and perhaps even prevent serious mistakes.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,912
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Go database/sql

  1. 1. DATABASE/SQL
  2. 2. • github.com/VividCortex • @xaprb • baron@vividcortex.com • linkedin.com/in/xaprb Optimization, Backups, Replication, and more Baron Schwartz, Peter Zaitsev & Vadim Tkachenko High Performance MySQL 3rd Edition CoversVersion 5.5 ME
  3. 3. • Docs: golang.org/pkg/database/sql/ • Tutorial: go-database-sql.org/ • MySQL Driver: github.com/go-sql-driver/mysql/ RESOURCES
  4. 4. THINGS
  5. 5. DB • Not a connection. • Open() Close() • Query() QueryRow() Exec()
  6. 6. ROWS • Next() • Scan()
  7. 7. PATTERNS
  8. 8. package main import ( ! _ "github.com/go-sql-driver/mysql" ! "database/sql" ! "log" ) func main() { ! db, err := sql.Open("mysql", ! ! "user:password@tcp(127.0.0.1:3306)/test") ! if err != nil { ! ! log.Println(err) ! } ! // What do we have here? ! defer db.Close() }
  9. 9. var str string err = db.QueryRow( "select hello from world where id = 1"). Scan(&str) if err != nil && err != sql.ErrNoRows { log.Println(err) } log.Println(str)
  10. 10. q := "select id, hello from world where id = ?" rows, err := db.Query(q, 1) if err != nil { ! log.Fatal(err) } defer rows.Close() var id int var str string for rows.Next() { ! err = rows.Scan(&id, &str) ! if err != nil { ! ! log.Fatal(err) ! } ! // Use the variables scanned from the row }
  11. 11. for rows.Next() { ! // scan } if err = rows.Err(); err != nil { rows.Close() log.Fatal(err) }
  12. 12. stmt, err := db.Prepare( ! "select id, hello from world where id = ?") if err != nil { ! log.Fatal(err) } defer stmt.Close() rows, err := stmt.Query(1) if err != nil { ! log.Fatal(err) } defer rows.Close() for rows.Next() { ! // ... }
  13. 13. stmt, err := db.Prepare( ! "insert into world(hello) values(?)") if err != nil { ! log.Fatal(err) } defer stmt.Close() res, err := stmt.Exec("hello, Dolly") if err != nil { ! log.Fatal(err) }
  14. 14. THINGSTO KNOW
  15. 15. THINGSTO KNOW • There is a connection pool. • You can’t use uint64 with high bit set in parameters.
  16. 16. DON’T BE LAZY
  17. 17. // Don’t do this! for i := 0; i < 50; i++ { ! _, err := db.Query("DELETE FROM hello.world") } // Use this instead! for i := 0; i < 50; i++ { ! _, err := db.Exec("DELETE FROM hello.world") }
  18. 18. MORE HELPFUL ADVICE • Don’t defer() in long-running functions. • Don’t use cxn state. UseTx to bind to a connection. • Don’t use BEGIN and COMMIT via SQL.
  19. 19. SURPRISES
  20. 20. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY. RETRY.
  21. 21. // Exec executes a query without returning any rows. // The args are for any placeholder parameters in the query. func (db *DB) Exec(query string, args ...interface{}) (Result, error) { ! var res Result ! var err error ! for i := 0; i < 10; i++ { ! ! res, err = db.exec(query, args) ! ! if err != driver.ErrBadConn { ! ! ! break ! ! } ! } ! return res, err }
  22. 22. PREPARED STATEMENTS
  23. 23. NULL This page intentionally left blank.
  24. 24. var s sql.NullString err := db.QueryRow( "SELECT name FROM foo WHERE id=?", id).Scan(&s) if s.Valid { // use s.String } else { // NULL value }
  25. 25. CUSTOMTYPES
  26. 26. CUSTOMTYPES • You can implement theValuer and Scanner interfaces. • Why?Transparently transform data in <=> out of the DB. • Compress and uncompress. • Marshall/unmarshall JSON or another type. • Encrypt and decrypt. • See e.g. http://jmoiron.net/blog/built-in-interfaces/
  27. 27. RESOURCES • http://golang.org/pkg/database/sql/ • http://go-database-sql.org/ • https://github.com/go-sql-driver/mysql • http://jmoiron.net/blog/ • @VividCortex * not biased, not biased, not biased
  28. 28. IMAGE CREDITS • http://www.flickr.com/photos/simens/6306917636/ • http://www.flickr.com/photos/dexxus/5794905716/ • http://www.flickr.com/photos/sebastian_bergmann/202396633/ • http://www.flickr.com/photos/doug88888/4794114114/ • http://www.flickr.com/photos/oatsy40/6443878013/ • http://www.sxc.hu/photo/1160562/ • Google Maps (screenshot) • http://www.flickr.com/photos/estherase/13553883/ • http://www.flickr.com/photos/paperpariah/4150220583/ • http://www.flickr.com/photos/zooboing/4743616313/ • http://www.flickr.com/photos/dseneste/5912382808/ • http://www.flickr.com/photos/clickykbd/66165381/sizes/l/ • http://www.flickr.com/photos/mamnaimie/5576980406/ • https://www.flickr.com/photos/zachstern/87431231
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×