From bc7b0d33a79cf49695c01064b6985f9e1aa3d537 Mon Sep 17 00:00:00 2001 From: HuangDavidDongyang Date: Wed, 4 Sep 2019 04:32:02 -0400 Subject: [PATCH] Add code snippets. --- go.mod | 3 + go.sum | 5 ++ samples/sql/go.mod | 8 +++ samples/sql/go.sum | 9 +++ samples/sql/main.go | 149 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 samples/sql/go.mod create mode 100644 samples/sql/go.sum create mode 100644 samples/sql/main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3c66ea0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/kaz/isucon9-prep + +require github.com/jmoiron/sqlx v1.2.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..dd3b2d4 --- /dev/null +++ b/go.sum @@ -0,0 +1,5 @@ +github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= +github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= diff --git a/samples/sql/go.mod b/samples/sql/go.mod new file mode 100644 index 0000000..a9cc1f0 --- /dev/null +++ b/samples/sql/go.mod @@ -0,0 +1,8 @@ +module sql + +require ( + github.com/go-sql-driver/mysql v1.4.1 + github.com/jmoiron/sqlx v1.2.0 + github.com/lib/pq v1.2.0 // indirect + github.com/mattn/go-sqlite3 v1.11.0 // indirect +) diff --git a/samples/sql/go.sum b/samples/sql/go.sum new file mode 100644 index 0000000..911839a --- /dev/null +++ b/samples/sql/go.sum @@ -0,0 +1,9 @@ +github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= +github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= +github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= diff --git a/samples/sql/main.go b/samples/sql/main.go new file mode 100644 index 0000000..2da7a3f --- /dev/null +++ b/samples/sql/main.go @@ -0,0 +1,149 @@ +package main + +import ( + crand "crypto/rand" + "encoding/binary" + "fmt" + "log" + "math/rand" + "time" + + _ "github.com/go-sql-driver/mysql" + "github.com/jmoiron/sqlx" +) + +var ( + db *sqlx.DB +) + +// User ... +type User struct { + Name string `json:"name" db:"name"` + Age int64 `json:"age" db:"age"` +} + +func init() { + seedBuf := make([]byte, 8) + crand.Read(seedBuf) + rand.Seed(int64(binary.LittleEndian.Uint64(seedBuf))) + + dbHost := "127.0.0.1" + + dbPort := "3306" + + dbUser := "root" + + dbPassword := ":" + "114514YJsnpi1919$" + + dsn := fmt.Sprintf("%s%s@tcp(%s:%s)/isubata?parseTime=true&loc=Local&charset=utf8mb4", + dbUser, dbPassword, dbHost, dbPort) + + log.Printf("Connecting to db: %q", dsn) + + tmpdb, err := sqlx.Connect("mysql", dsn) + + if err != nil { + log.Printf("Error: %q", err) + return + } + + db = tmpdb + + for { + err := db.Ping() + if err == nil { + break + } + log.Println(err) + time.Sleep(time.Second * 3) + } + + db.SetMaxOpenConns(20) + db.SetConnMaxLifetime(5 * time.Minute) + log.Printf("Succeeded to connect db.") +} + +// db.Exec +func createUser() { + schema := `CREATE TABLE IF NOT EXISTS user ( + name text, + age integer);` + + result, err := db.Exec(schema) + + if err != nil { + log.Printf("Error: %q", err) + } else { + log.Printf("Result: %q", result) + } +} + +// db.MustExec +func insertUser() { + sentence := `INSERT INTO user (name, age) VALUES ("david", 114514);` + db.MustExec(sentence) +} + +// db.Queryx +func selectUserQueryx() { + sentence := `SELECT * FROM user;` + + rows, err := db.Queryx(sentence) + + if err != nil { + log.Printf("Error: %q", err) + return + } + + for rows.Next() { + var u User + err = rows.StructScan(&u) + + if err != nil { + log.Printf("Error: %q", err) + return + } + + fmt.Println(u) + } +} + +// db.Select +func selectUserSelect() { + sentence := `SELECT * FROM user;` + + us := []User{} + + err := db.Select(&us, sentence) + + if err != nil { + log.Printf("Error: %q", err) + return + } + + for _, u := range us { + fmt.Println(u) + } +} + +func nPlus1() { + +} + +func nPlus1EagerLoading() { + +} + +func nPlus1Join() { + +} + +func main() { + createUser() + + insertUser() + + selectUserQueryx() + + selectUserSelect() +}