Using MySQL with Go - Golang's Database/SQL Driver
-
Upload
vividcortex -
Category
Software
-
view
5.843 -
download
1
description
Transcript of Using MySQL with Go - Golang's Database/SQL Driver
æ
Building Apps with Go & MySQL
1
VividCortex Webinar - Baron Schwartz - November 11, 2014
å
About BaronFounder of @VividCortex
Author of High Performance MySQL
@xaprb on Twitter
http://www.linkedin.com/in/xaprb
Optimization, Backups, Replication, and more
Baron Schwartz, Peter Zaitsev &
Vadim Tkachenko
High PerformanceMySQL
3rd Edition
Covers Version 5.5
2
æ
VividCortex - MySQL Performance Management
3
å
What’s Nice About Go?
4
Interfaces
Concurrency primitives
Syntax
Performance
Static Linking
Tools
å
What’s Not As Nice?Hard to Google it (use “golang”)
Package versioning (use github.com/VividCortex/johnny-deps/)
Doesn’t have generics (not a big deal)
5
æ
Go Database Design Patterns
6
å
Things To KnowWhat is database/sql?
Where is the documentation?
How do I create a database connection?
How do I query a connection?
How do I retrieve results from a query?
What are the common operations?
7
å
Overview of database/sqlA generic, minimal interface for SQL-like databases
Verbs and nouns: Open, Prepare, Exec, Query, QueryRow, Scan, Close
DB, Stmt, Value, Row, Rows, Result
Tx, Begin, Commit, Rollback
Docs: http://golang.org/pkg/database/sql/
Tutorial: http://go-database-sql.org/
8
æ9package 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) } defer db.Close()
// Do something useful here. }
åOne-Shot Query
10
var str string err = db.QueryRow(
"select hello from hello.world where id = 1").Scan(&str) if err != nil && err != sql.ErrNoRows { log.Println(err) } log.Println(str)
åQuery...
11
var id int var str string q := "select id, hello from hello.world where id = ?"
rows, err := db.Query(q, 1) if err != nil { log.Fatal(err) } defer rows.Close()
å... Then Fetch Rows
12
for rows.Next() { err = rows.Scan(&id, &str) if err != nil { log.Fatal(err) } // Use the variables scanned from the row } if err = rows.Err(); err != nil { log.Fatal(err) }
åPrepare...
13
stmt, err := db.Prepare( "select id, hello from hello.world where id = ?") if err != nil { log.Fatal(err) } defer stmt.Close()
å... Then Execute & Fetch
14
rows, err := stmt.Query(1) if err != nil { log.Fatal(err) } defer rows.Close()
for rows.Next() { // ... }
åInsert...
15
stmt, err := db.Prepare( "insert into hello.world(hello) values(?)") if err != nil { log.Fatal(err) } defer stmt.Close()
res, err := stmt.Exec("hello, Dolly") if err != nil { log.Fatal(err) }
å... And Check Results
16
lastId, err := res.LastInsertId() if err != nil { log.Fatal(err) } rowCnt, err := res.RowsAffected() if err != nil { log.Fatal(err) }
æ
Docs: “Pleasantly Lightweight!”
17
æ
Connection Pooling
18
æ
Big Unsigned Integers
19
æ
Unexpected Connections
20
åResource Exhaustion
21
// 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") }
æ
Retry. Retry. Retry. Retry. Retry. Retry. Retry. Retry. Retry. Retry.
22
æ
Handling NULLs
23
åWorking with NULL
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 }
// If you don’t care to check whether it was NULL, // Just use s.String without checking, it’ll be ""
å
Boilerplate code everywhere
There’s no sql.NullUint64
Nullability is tricky, not future-proof
It’s not very Go-ish (no useful default zero-value)
25
NULL Makes Code Ugly
æ
ORMs - Not Really Suited for Go
26
æ
Interfaces - Learn Them, Love Them
27
e.g. https://vividcortex.com/blog/2014/11/11/encrypting-data-in-mysql-with-go/
å
Resources
28
http://golang.org/pkg/database/sql/
http://go-database-sql.org/
https://github.com/go-sql-driver/mysql
http://jmoiron.net/blog/
http://twitter.com/VividCortex/ <--- lots of Go tips
æ
[email protected] @xaprb • linkedin.com/in/xaprb
å
Image Creditshttp://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
30