-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbhandler.go
291 lines (233 loc) · 7.71 KB
/
dbhandler.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
package gopsql
import (
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/Tebro/logger"
// Importing pq to support postgres
_ "github.com/lib/pq"
)
const createTableQueryTemplate = `
CREATE TABLE IF NOT EXISTS %s (
%s
);
`
func getCreateTableQuery(obj interface{}) string {
objType := reflect.TypeOf(obj)
columnName := objType.Name()
var fieldLines []string
for i := 0; i < objType.NumField(); i++ {
objField := objType.Field(i)
if _, ok := objField.Tag.Lookup("column_skip"); !ok {
fieldLines = append(fieldLines, fmt.Sprintf("%s %s", objField.Name, objField.Tag.Get("column_type")))
}
}
return fmt.Sprintf(createTableQueryTemplate, columnName, strings.Join(fieldLines, ","))
}
const selectAllQueryTemplate = "SELECT %s FROM %s;"
func getSelectAllQuery(obj interface{}) (string, bool) {
objType := reflect.TypeOf(obj)
var fieldNames []string
orderBy := false
var orderByField string
for i := 0; i < objType.NumField(); i++ {
if _, ok := objType.Field(i).Tag.Lookup("column_skip"); !ok {
fieldNames = append(fieldNames, objType.Field(i).Name)
}
if _, ok := objType.Field(i).Tag.Lookup("column_order_by"); ok {
orderBy = true
orderByField = objType.Field(i).Name
}
}
var lastPart string
if orderBy {
lastPart = fmt.Sprintf("%s ORDER BY %s", objType.Name(), orderByField)
} else {
lastPart = objType.Name()
}
return fmt.Sprintf(selectAllQueryTemplate, strings.Join(fieldNames, ","), lastPart), orderBy
}
func getSelectFilteredQuery(obj interface{}, filterString string) string {
q, orderBy := getSelectAllQuery(obj)
// Remove the semicolon
var retval string
if orderBy {
parts := strings.Split(q, " ORDER BY ")
retval = fmt.Sprintf("%s WHERE %s ORDER BY %s", parts[0], filterString, parts[1])
} else {
q = strings.TrimRight(q, ";")
retval = fmt.Sprintf("%s WHERE %s;", q, filterString)
}
logger.Debug(fmt.Sprintf("Built filtered SELECT query: '%s'", retval))
return retval
}
func parseFilterString(filters ...string) (string, []string, error) {
if len(filters) < 2 {
return "", nil, errors.New("Not enough parameters provided")
}
var filterValues []string
if len(filters) == 2 {
filterValues = append(filterValues, filters[1])
return fmt.Sprintf("%s=$1", filters[0]), filterValues, nil
}
var parts []string
if (len(filters)+1)%3 == 0 {
valueIndex := 1
for i := 0; i < len(filters); i += 3 {
filterValues = append(filterValues, filters[i+1])
parts = append(parts, fmt.Sprintf("%s=$%d", filters[i], valueIndex))
valueIndex++
if i+2 < len(filters) {
parts = append(parts, filters[i+2])
}
}
}
return strings.Join(parts, " "), filterValues, nil
}
// GetAll gets all the rows for the type
func GetAll(obj interface{}) (*sql.Rows, error) {
q, _ := getSelectAllQuery(obj)
return db.Query(q)
}
// GetFiltered does a query with a WHERE section GetFiltered(sometype, field, value, "AND", field, value)
func GetFiltered(obj interface{}, filter ...string) (*sql.Rows, error) {
parsedFilter, filterValues, err := parseFilterString(filter...)
if err != nil {
return nil, err
}
q := getSelectFilteredQuery(obj, parsedFilter)
filterValueInterfaces := make([]interface{}, len(filterValues))
for i, v := range filterValues {
filterValueInterfaces[i] = v
}
return db.Query(q, filterValueInterfaces...)
}
// Saveable interface is so that Save function always has a way of getting and setting the ID
type Saveable interface {
GetID() int
SetID(int)
}
// UpdateExisting updates a record in the DB based on ID
func UpdateExisting(obj Saveable) error {
q, values := getUpdateQuery(obj)
valueInterfaces := make([]interface{}, len(values))
for i, v := range values {
valueInterfaces[i] = v
}
_, err := db.Exec(q, valueInterfaces...)
return err
}
// InsertNew inserts a new entry and returns the Row for someone with more knowledge to retrieve ID
func InsertNew(obj Saveable) *sql.Row {
q, values := getInsertQuery(obj)
valueInterfaces := make([]interface{}, len(values))
for i, v := range values {
valueInterfaces[i] = v
}
logger.Debug(fmt.Sprintf("Going to insert with: '%v' and %v", q, valueInterfaces))
return db.QueryRow(q, valueInterfaces...)
}
const updateQueryTemplate = "UPDATE %s SET %s WHERE %s;"
func getUpdateQuery(obj Saveable) (string, []string) {
objVal := reflect.ValueOf(obj)
columnName := objVal.Type().Name()
var updateValues []string
var values []string
valueIndex := 1
for i := 0; i < objVal.NumField(); i++ {
field := objVal.Type().Field(i)
_, found1 := field.Tag.Lookup("column_skip")
_, found2 := field.Tag.Lookup("column_skip_insert")
if !found1 && !found2 {
vInter := objVal.Field(i).Interface()
val := reflect.ValueOf(vInter)
theValue := fmt.Sprintf("%v", val)
updateValues = append(updateValues, fmt.Sprintf("%s=$%d", field.Name, valueIndex))
valueIndex++
values = append(values, theValue)
}
}
filter := fmt.Sprintf("ID=$%d", valueIndex)
values = append(values, fmt.Sprintf("%v", obj.GetID())) //Add the ID for the WHERE part
return fmt.Sprintf(updateQueryTemplate, columnName, strings.Join(updateValues, ","), filter), values
}
const insertQueryTemplate = "INSERT INTO %s (%s) VALUES (%s) RETURNING ID;"
func getInsertQuery(obj Saveable) (string, []string) {
objVal := reflect.ValueOf(obj)
columnName := objVal.Type().Name()
// Build Fields and values list
var fields []string
var values []string
var valuePlaceholders []string
valueIndex := 1
for i := 0; i < objVal.NumField(); i++ {
field := objVal.Type().Field(i)
_, found1 := field.Tag.Lookup("column_skip")
_, found2 := field.Tag.Lookup("column_skip_insert")
if !found1 && !found2 {
fields = append(fields, field.Name)
// Getting the actual value
vInter := objVal.Field(i).Interface()
val := reflect.ValueOf(vInter)
theValue := fmt.Sprintf("%v", val)
values = append(values, theValue)
valuePlaceholders = append(valuePlaceholders, fmt.Sprintf("$%d", valueIndex))
valueIndex++
}
}
return fmt.Sprintf(insertQueryTemplate, columnName, strings.Join(fields, ", "), strings.Join(valuePlaceholders, ", ")), values
}
const deleteQueryTemplate = "DELETE FROM %s WHERE ID=$1;"
// Delete removes the provided entry from the database, based on the return value of GetID()
func Delete(obj Saveable) error {
columnName := reflect.ValueOf(obj).Type().Name()
query := fmt.Sprintf(deleteQueryTemplate, columnName)
_, err := db.Exec(query, obj.GetID())
return err
}
// Doc: https://godoc.org/github.com/lib/pq
var db *sql.DB
// Setup connects to the database and makes sure the schema is inserted
func Setup(dbHost string, dbUser string, dbPass string, dbName string, sslMode string, types []Saveable) error {
_sslMode := "disable"
if sslMode != "" {
_sslMode = sslMode
}
dbURL := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s", dbUser, dbPass, dbHost, dbName, _sslMode)
var err error
db, err = sql.Open("postgres", dbURL)
if err != nil {
logger.Error(fmt.Sprintf("Unable to open database connection. Connection URL: %s", dbURL))
return errors.New("unable to connect to the database")
}
dbOK := false
// Check connection, retry 10 times.
for retry := 0; retry < 10; retry++ {
err = db.Ping()
if err != nil {
logger.Error(fmt.Sprintf("Cannot reach database. Waiting 5 seconds before retrying. Try: %d", retry+1))
time.Sleep(5 * time.Second)
continue
}
dbOK = true
break
}
if !dbOK {
logger.Debug(fmt.Sprintf("Connection URL: %s", dbURL))
return errors.New("unable to open database connection")
}
db.SetMaxIdleConns(0)
// Bootstrap the database schema
for _, t := range types {
_, err = db.Exec(getCreateTableQuery(t))
if err != nil {
logger.Error(err)
return fmt.Errorf("failed to create table for: %s", reflect.TypeOf(t).Name())
}
}
logger.Log("Database configured")
return nil
}