This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
persist-utils.go
183 lines (149 loc) · 4.07 KB
/
persist-utils.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
package scaler_utils
import (
"fmt"
"log"
"time"
"os"
"encoding/json"
"database/sql"
"github.com/coopernurse/gorp"
_ "github.com/go-sql-driver/mysql"
)
// Code referenced from
// http://nathanleclaire.com/blog/2013/11/04/want-to-work-with-databases-in-golang-lets-try-some-gorp/
var (
dbEnabled bool = true
TABLE_NAME = "AppDetail"
dbmap *gorp.DbMap
vcap_services string
)
func init() {
vcap_services = os.Getenv("VCAP_SERVICES")
if vcap_services == "" {
dbEnabled = false
return
}
var jsonBlob = []byte(vcap_services)
var vcapServices VcapServices
err := json.Unmarshal(jsonBlob, &vcapServices)
if (err != nil) {
fmt.Println("Error: ", err)
dbEnabled = false
return
}
fmt.Printf("Persistence: Vcap Services: %v\n", vcapServices.MySqlServiceDefn)
if (len(vcapServices.MySqlServiceDefn) == 0 ) {
dbEnabled = false
return
}
mysqlServiceDefn := vcapServices.MySqlServiceDefn[0]
dbString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
mysqlServiceDefn.Credentials.Username,
mysqlServiceDefn.Credentials.Password,
mysqlServiceDefn.Credentials.Hostname,
mysqlServiceDefn.Credentials.Port,
mysqlServiceDefn.Credentials.Name)
// if (dbType == "MongoDb") {
// dbString = vcapServices.MongoDbServiceDefn[0].Credentials.Uri
// }
InitDB(dbString)
}
func IsDBEnabled() bool {
return dbEnabled
}
func enableDB() {
dbEnabled = true
fmt.Printf("Persistence: MySQL Persistence Enabled\n")
}
func InitDB(dbConnectionInfo string) {
fmt.Printf("Persistence: Connecting to DB using parameters: %s", dbConnectionInfo)
db, err := sql.Open("mysql", dbConnectionInfo)
if err != nil {
log.Fatalln("\nFatal!! Error connecting to DB provided: " + dbConnectionInfo, err)
}
// construct a gorp DbMap
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
// JSON struct seems to allow null and that has problems with setKeys..
//table := dbmap.AddTableWithName(AppDetail{}, "appdetails").SetKeys(true, "Target")
dbmap.AddTable(AppDetail{})
err = dbmap.CreateTablesIfNotExists()
checkErr(err, "Create tables failed")
enableDB()
}
func CloseDB() {
if (dbmap != nil ) {
dbmap.Db.Close()
}
}
func checkErr(err error, msg string) {
if err != nil {
log.Fatalln(msg, err)
}
}
func Get(target string) AppDetail {
var nilApp AppDetail
if dbmap == nil {
log.Fatalln("Persistence: Call InitDb() first!")
return nilApp
}
//appDetail, err := dbmap.Get(AppDetail{}, target)
var appDetail AppDetail
_, err := dbmap.Select(&appDetail, "select * from " + TABLE_NAME + " where trader = ?", target)
if err != nil {
log.Fatalln("Persistence: Error selecting AppDetail with target name: %s from DB, %s", target, err)
return nilApp
}
return appDetail
}
func Load() []AppDetail {
var nilApps []AppDetail
if dbmap == nil {
log.Fatalln("Persistence: Call InitDb() first!")
return nilApps
}
var appDetails []AppDetail
_, err := dbmap.Select(&appDetails, "select * from " + TABLE_NAME + " LIMIT 50")
if err != nil {
log.Fatalln("Persistence: Error loading AppDetail from DB: %s", err)
return nilApps
}
return appDetails
}
func Insert(appDetail AppDetail) {
if dbmap == nil {
log.Fatalln("Persistence: Call InitDb() first!")
return
}
appDetail.Updated = time.Now().Unix()
err := dbmap.Insert(&appDetail)
if err != nil {
log.Fatalln("Persistence: Error persisting AppDetail: %v into DB, %s", appDetail, err)
}
}
func Update(appDetail AppDetail) {
if dbmap == nil {
log.Fatalln("Persistence: Call InitDb() first!")
return
}
appDetail.Updated = time.Now().Unix()
_, err := dbmap.Update(appDetail)
if err != nil {
log.Fatalln("Persistence: Error updating AppDetail: %v into DB, %s", appDetail, err)
}
}
func Delete(appDetail AppDetail) {
if dbmap == nil {
log.Fatalln("\nCall InitDb() first!")
return
}
_, err := dbmap.Delete(appDetail)
if err != nil {
log.Fatalln("Persistence: Error deleting AppDetail: %v from DB, %s", appDetail, err)
}
}
func DeleteAll() error {
if dbmap == nil {
log.Fatalln("Persistence: Call InitDb() first!")
}
return dbmap.TruncateTables()
}