-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
105 lines (86 loc) · 2.2 KB
/
database.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
type GameWinHisto struct {
Uid int64 `json:"uid"`
IdPlayer int `json:"idplayer"`
IdGuild int `json:"idguild"`
IdChan int `json:"idchan"`
CreatedAt string `json:"idchan"`
}
type Player struct {
Uid int64 `json:"uid"`
DiscordId int `json:"discordId"`
GuildPlayed string `json:"guildPlayed"`
}
type Guild struct {
Uid int64 `json:"uid"`
DiscordId int `json:"discordId"`
NbGamesFailed int `json:"nbGamesFailed"`
}
func InsertNewWin(db *sql.DB, newWin *GameWinHisto) int64 {
statement, err := db.Prepare(`
INSERT INTO GamesWinHisto (IdPlayer, idGuild, idChan)
Values (?, ?, ?)
`)
if err != nil {
// TODO: Gerer les erreurs
fmt.Printf("err : %+v\n", err)
panic("gere les erreurs")
}
result, err := statement.Exec(
newWin.IdPlayer, newWin.IdGuild, newWin.IdChan)
if err != nil {
fmt.Printf("Err : %+v\n", err)
panic("gere les erreurs")
}
lastInsertedId, err := result.LastInsertId()
if err != nil {
fmt.Printf("Err : %+v\n", err)
panic("gere les erreurs")
}
return lastInsertedId
}
func InsertNewPlayer(db *sql.DB, newPlayer *Player) int64 {
statement, err := db.Prepare(`
INSERT INTO Player (discordId, guildPlayed) VALUES (?, ?)
`)
if err != nil {
fmt.Printf("err : %+v\n", err)
panic("gere les erreurs")
}
result, err := statement.Exec(newPlayer.DiscordId, newPlayer.GuildPlayed)
if err != nil {
fmt.Printf("error : %+v\n\n", err)
panic("gere les errors")
}
lastInsertedId, err := result.LastInsertId()
if err != nil {
fmt.Printf("error : %+v\n\n", err)
panic("gere les errors")
}
return lastInsertedId
}
func InsertNewGuild(db *sql.DB, newGuild *Guild) int64 {
statement, err := db.Prepare(`
INSERT INTO Guild (discordId, ) VALUES (?, ?)
`)
if err != nil {
fmt.Printf("err : %+v\n", err)
panic("gere les erreurs")
}
ret, err := statement.Exec(newGuild.DiscordId, newGuild.NbGamesFailed)
if err != nil {
fmt.Printf("error : %+v\n\n", err)
panic("gere les errors")
}
lastInsertedId, err := ret.LastInsertId()
if err != nil {
fmt.Printf("error : %+v\n\n", err)
panic("gere les errors")
}
return lastInsertedId
}