/
helpers.go
124 lines (107 loc) · 3.31 KB
/
helpers.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
// khan
// https://github.com/topfreegames/khan
//
// Licensed under the MIT license:
// http://www.opensource.org/licenses/mit-license
// Copyright © 2016 Top Free Games <backend@tfgco.com>
package models
import (
"database/sql"
"fmt"
"github.com/go-gorp/gorp"
_ "github.com/lib/pq" //This is required to use postgres with database/sql
egorp "github.com/topfreegames/extensions/gorp"
"github.com/topfreegames/extensions/gorp/interfaces"
"github.com/topfreegames/khan/util"
)
// DB is the contract for all the operations we use from either a connection or transaction
// This is required for automatic transactions
type DB interface {
Get(interface{}, ...interface{}) (interface{}, error)
Select(interface{}, string, ...interface{}) ([]interface{}, error)
SelectOne(interface{}, string, ...interface{}) error
SelectInt(string, ...interface{}) (int64, error)
Insert(...interface{}) error
Update(...interface{}) (int64, error)
Delete(...interface{}) (int64, error)
Exec(string, ...interface{}) (sql.Result, error)
}
var _db interfaces.Database
// GetDefaultDB returns a connection to the default database
func GetDefaultDB() (interfaces.Database, error) {
return GetDB("localhost", "khan", 5433, "disable", "khan", "")
}
// GetPerfDB returns a connection to the perf database
func GetPerfDB() (interfaces.Database, error) {
return GetDB("localhost", "khan_perf", 5433, "disable", "khan_perf", "")
}
// GetDB returns a DbMap connection to the database specified in the arguments
func GetDB(host string, user string, port int, sslmode string, dbName string, password string) (interfaces.Database, error) {
if _db == nil {
var err error
_db, err = InitDb(host, user, port, sslmode, dbName, password)
if err != nil {
_db = nil
return nil, err
}
}
return _db, nil
}
// InitDb initializes a connection to the database
func InitDb(host string, user string, port int, sslmode string, dbName string, password string) (interfaces.Database, error) {
connStr := fmt.Sprintf(
"host=%s user=%s port=%d sslmode=%s dbname=%s",
host, user, port, sslmode, dbName,
)
if password != "" {
connStr += fmt.Sprintf(" password=%s", password)
}
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
db.SetMaxIdleConns(5)
db.SetMaxOpenConns(10)
dbmap := &gorp.DbMap{
Db: db,
Dialect: gorp.PostgresDialect{},
TypeConverter: util.TypeConverter{},
}
dbmap.AddTableWithName(Game{}, "games").SetKeys(true, "ID")
dbmap.AddTableWithName(Player{}, "players").SetKeys(true, "ID")
dbmap.AddTableWithName(Clan{}, "clans").SetKeys(true, "ID")
dbmap.AddTableWithName(Membership{}, "memberships").SetKeys(true, "ID")
dbmap.AddTableWithName(Hook{}, "hooks").SetKeys(true, "ID")
// dbmap.TraceOn("[gorp]", log.New(os.Stdout, "KHAN:", log.Lmicroseconds))
return egorp.New(dbmap, dbName), nil
}
// Returns value or 0
func nullOrInt(value sql.NullInt64) int64 {
if value.Valid {
v, err := value.Value()
if err == nil {
return v.(int64)
}
}
return 0
}
// Returns value or ""
func nullOrString(value sql.NullString) string {
if value.Valid {
v, err := value.Value()
if err == nil {
return v.(string)
}
}
return ""
}
// Returns value or false
func nullOrBool(value sql.NullBool) bool {
if value.Valid {
v, err := value.Value()
if err == nil {
return v.(bool)
}
}
return false
}