-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqldriver.go
134 lines (113 loc) · 3.46 KB
/
sqldriver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"database/sql"
"fmt"
"os"
_ "github.com/go-sql-driver/mysql"
_ "github.com/joho/godotenv/autoload"
)
func main() {
// Configure the example database connection.
openDB("mysql", func(db *sql.DB) {
// 1. recreate the table.
recreateTable(db)
// 2. Run some simple examples.
simpleExample(db)
// 3. Getting further.
tradeExample(db)
})
}
func simpleExample(db *sql.DB) {
// Create a player, who has a coin and a goods.
err := createPlayer(db, Player{ID: "test", Coins: 1, Goods: 1})
if err != nil {
panic(err)
}
// Get a player.
testPlayer, err := getPlayer(db, "test")
if err != nil {
panic(err)
}
fmt.Printf("getPlayer: %+v\n", testPlayer)
// Create players with bulk inserts. Insert 1919 players totally, with 114 players per batch.
err = bulkInsertPlayers(db, randomPlayers(1919), 114)
if err != nil {
panic(err)
}
// Count players amount.
playersCount, err := getCount(db)
if err != nil {
panic(err)
}
fmt.Printf("countPlayers: %d\n", playersCount)
// Print 3 players.
threePlayers, err := getPlayerByLimit(db, 3)
if err != nil {
panic(err)
}
for index, player := range threePlayers {
fmt.Printf("print %d player: %+v\n", index+1, player)
}
}
func tradeExample(db *sql.DB) {
// Player 1: id is "1", has only 100 coins.
// Player 2: id is "2", has 114514 coins, and 20 goods.
player1 := Player{ID: "1", Coins: 100}
player2 := Player{ID: "2", Coins: 114514, Goods: 20}
// Create two players "by hand", using the INSERT statement on the backend.
if err := createPlayer(db, player1); err != nil {
panic(err)
}
if err := createPlayer(db, player2); err != nil {
panic(err)
}
// Player 1 wants to buy 10 goods from player 2.
// It will cost 500 coins, but player 1 cannot afford it.
fmt.Println("\nbuyGoods:\n => this trade will fail")
if err := buyGoods(db, player2.ID, player1.ID, 10, 500); err == nil {
panic("there shouldn't be success")
}
// So player 1 has to reduce the incoming quantity to two.
fmt.Println("\nbuyGoods:\n => this trade will success")
if err := buyGoods(db, player2.ID, player1.ID, 2, 100); err != nil {
panic(err)
}
}
func openDB(driverName string, runnable func(db *sql.DB)) {
db, err := sql.Open(driverName, getDSN())
if err != nil {
panic(err)
}
defer db.Close()
runnable(db)
}
func getDSN() string {
tidbHost := getEnvWithDefault("TIDB_HOST", "127.0.0.1")
tidbPort := getEnvWithDefault("TIDB_PORT", "4000")
tidbUser := getEnvWithDefault("TIDB_USER", "root")
tidbPassword := getEnvWithDefault("TIDB_PASSWORD", "")
tidbDBName := getEnvWithDefault("TIDB_DB_NAME", "test")
useSSL := getEnvWithDefault("USE_SSL", "false")
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&tls=%s",
tidbUser, tidbPassword, tidbHost, tidbPort, tidbDBName, useSSL)
}
func getEnvWithDefault(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}