-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
59 lines (50 loc) · 1.44 KB
/
db.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
package main
import (
"database/sql"
"fmt"
"github.com/coopernurse/gorp"
_ "github.com/ziutek/mymysql/godrv"
)
type Room struct {
Id int64
Name string
Topic string
MembershipLimit int `json:"Membership_Limit"`
Full bool
OpenToGuests bool `json:"Open_To_Guests"`
UpdatedAt int64 `json:"-"`
Updated_At string `db:"-"`
CreatedAt int64 `json:"-"`
Created_At string `db:"-"`
}
type User struct {
Id int64
Name string
EmailAddress string `json:"Email_Address"`
Admin bool
CreatedAt string `json:"Created_At"`
Type string
AvatarUrl string `json:"Avatar_Url"`
}
type Message struct {
Id int64
RoomId int64 `json:"Room_Id"`
UserId int64 `json:"User_Id"`
Body string
CreatedAt int64 `json:"-"`
Created_At string `db:"-"`
Type string
Starred bool
}
func InitDb() *gorp.DbMap {
dbConnStr := fmt.Sprintf("tcp:localhost:3306*%s/root/", Config.DbName)
db, err := sql.Open("mymysql", dbConnStr)
CheckFatalError(err, "sql.Open failed")
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
dbmap.AddTableWithName(Room{}, "room").SetKeys(false, "Id")
dbmap.AddTableWithName(Message{}, "message").SetKeys(false, "Id")
dbmap.AddTableWithName(User{}, "user").SetKeys(false, "Id")
err = dbmap.CreateTablesIfNotExists()
CheckFatalError(err, "Create tables failed")
return dbmap
}