/
model.go
308 lines (260 loc) · 6.74 KB
/
model.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"log"
"time"
"math/rand"
"github.com/Pallinder/go-randomdata"
"github.com/Pallinder/sillyname-go"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"github.com/pariz/gountries"
xid "github.com/rs/xid"
)
type League struct {
TableName []byte `sql:"public.league"`
ID string `json:"id"`
Name string `json:"name"`
}
type Club struct {
TableName []byte `sql:"public.club"`
ID string `json:"id"`
Name string `json:"name"`
Stadium string `json:"stadium"`
CoachName string `json:"coach_name"`
}
type ClubLeague struct {
TableName []byte `sql:"public.club_league"`
ClubID string `json:"club_id" sql:",pk"`
LeagueID string `json:"league_id" sql:",pk"`
}
type Cup struct {
TableName []byte `sql:"public.cup"`
ID string `json:"id"`
Name string `json:"name"`
}
type Nation struct {
TableName []byte `sql:"public.nation"`
ID string `json:"id"`
Name string `json:"name"`
Continent string `json:"continent"`
Ranking int `json:"ranking"`
CoachName string `json:"coach_name"`
}
type NationCup struct {
TableName []byte `sql:"public.nation_cup"`
NationID string `json:"nation_id" sql:",pk"`
CupID string `json:"cup_id" sql:",pk"`
}
type Player struct {
TableName []byte `sql:"public.player"`
ID string `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Age int `json:"age"`
Position string `json:"position"`
Height int `json:"height"`
Weight int `json:"weight"`
ClubID string `json:"club_id"`
NationID string `json:"nation_id"`
}
// ConnectDb kết nối CSDL qua cấu hình
func ConnectDb() (db *pg.DB) {
db = pg.Connect(&pg.Options{
User: "postgres",
Password: "123",
Database: "postgres",
Addr: "localhost:5432",
})
return db
}
func InitializeDatabase(db *pg.DB) {
var league League
var club Club
var clubLeague ClubLeague
var cup Cup
var nation Nation
var nationCup NationCup
var player Player
// Tạo bảng
for _, model := range []interface{}{&league, &club, &cup, &nation, &clubLeague, &nationCup, &player} {
err := db.CreateTable(model, &orm.CreateTableOptions{
Temp: false,
FKConstraints: true,
IfNotExists: true,
})
if err != nil {
log.Println(err)
}
}
// Thêm FK constraints cho bảng club_league
_, err := db.Exec(`
ALTER TABLE club_league
ADD CONSTRAINT club_league_club_id_fkey FOREIGN KEY (club_id) REFERENCES club(id)
`)
if err != nil {
log.Println(err)
}
_, err = db.Exec(`
ALTER TABLE club_league
ADD CONSTRAINT club_league_league_id_fkey FOREIGN KEY (league_id) REFERENCES league(id)
`)
if err != nil {
log.Println(err)
}
// Thêm FK constraints cho bảng nation_cup
_, err = db.Exec(`
ALTER TABLE nation_cup
ADD CONSTRAINT nation_cup_nation_id_fkey FOREIGN KEY (nation_id) REFERENCES nation(id)
`)
if err != nil {
log.Println(err)
}
_, err = db.Exec(`
ALTER TABLE nation_cup
ADD CONSTRAINT nation_cup_cup_id_fkey FOREIGN KEY (cup_id) REFERENCES cup(id)
`)
if err != nil {
log.Println(err)
}
// Thêm FK constraints cho bảng player
_, err = db.Exec(`
ALTER TABLE player
ADD CONSTRAINT player_club_id_fkey FOREIGN KEY (club_id) REFERENCES club(id)
`)
if err != nil {
log.Println(err)
}
_, err = db.Exec(`
ALTER TABLE player
ADD CONSTRAINT player_nation_id_fkey FOREIGN KEY (nation_id) REFERENCES nation(id)
`)
if err != nil {
log.Println(err)
}
}
func SaveClubLeagueData(db *pg.DB) {
log.Println("Begin save club data")
var leagueList []League
for i := 1; i <= 1000; i++ {
var league League
league.ID = xid.New().String()
league.Name = sillyname.GenerateStupidName()
leagueList = append(leagueList, league)
}
var clubList []Club
for i := 1; i <= 20000; i++ {
var club Club
club.ID = xid.New().String()
club.Name = sillyname.GenerateStupidName()
club.Stadium = sillyname.GenerateStupidName()
club.CoachName = randomdata.FullName(randomdata.Male)
clubList = append(clubList, club)
}
err := db.Insert(&leagueList)
if err != nil {
log.Println(err)
}
err = db.Insert(&clubList)
if err != nil {
log.Println(err)
}
_, err = db.Exec(`
INSERT INTO club_league (club_id, league_id)
SELECT club.id AS club_id, league.id AS league_id
FROM club, league
LIMIT 1000000
`)
if err != nil {
log.Println(err)
}
log.Println("Finished save club data")
}
func SaveNationCupData(db *pg.DB) {
log.Println("Begin save nation data")
var cupList []Cup
for i := 1; i <= 1000; i++ {
var cup Cup
cup.ID = xid.New().String()
cup.Name = sillyname.GenerateStupidName()
cupList = append(cupList, cup)
}
query := gountries.New()
allCountries := query.FindAllCountries()
min := 1
max := 247
var nationList []Nation
for _, v := range allCountries {
var nation Nation
nation.ID = xid.New().String()
nation.Name = v.Name.Official
nation.Continent = v.Continent
rand.Seed(time.Now().UnixNano())
nation.Ranking = rand.Intn(max) + min
nation.CoachName = randomdata.FullName(randomdata.Male)
nationList = append(nationList, nation)
}
err := db.Insert(&cupList)
if err != nil {
log.Println(err)
}
err = db.Insert(&nationList)
if err != nil {
log.Println(err)
}
_, err = db.Exec(`
INSERT INTO nation_cup (nation_id, cup_id)
SELECT nation.id AS nation_id, cup.id AS cup_id
FROM nation, cup
LIMIT 150000
`)
if err != nil {
log.Println(err)
}
log.Println("Finished save nation data")
}
func SavePlayerData(db *pg.DB) {
log.Println("Begin save player data")
var clubIds []string
_, err := db.Query(&clubIds, `SELECT id AS club_ids FROM club`)
if err != nil {
log.Println(err)
}
minClub := 0
maxClub := len(clubIds) - 1
var nationIds []string
_, err = db.Query(&nationIds, `SELECT id AS nation_ids FROM nation`)
if err != nil {
log.Println(err)
}
minNation := 0
maxNation := len(nationIds) - 1
minAge := 18
maxAge := 22
positions := []string{"Goalkeeper", "Defender", "Midfielder", "Striker"}
minPos := 0
maxPos := len(positions) - 1
minHeight := 160
maxHeight := 200
minWeight := 65
maxWeight := 90
var playerList []Player
for i := 1; i <= 1000000; i++ {
var player Player
rand.Seed(time.Now().UnixNano())
player.ID = xid.New().String()
player.FirstName = randomdata.FirstName(randomdata.Male)
player.LastName = randomdata.LastName()
player.Age = rand.Intn(maxAge) + minAge
player.Position = positions[ rand.Intn(maxPos) + minPos ]
player.Height = rand.Intn(maxHeight) + minHeight
player.Weight = rand.Intn(maxWeight) + minWeight
player.ClubID = clubIds[ rand.Intn(maxClub) + minClub ]
player.NationID = nationIds[ rand.Intn(maxNation) + minNation ]
playerList = append(playerList, player)
}
err = db.Insert(&playerList)
if err != nil {
log.Println(err)
}
log.Println("Finished save player data")
}