-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
429f5ed
commit bc7b0d3
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/kaz/isucon9-prep | ||
|
||
require github.com/jmoiron/sqlx v1.2.0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |