-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb.go
More file actions
316 lines (292 loc) · 7.4 KB
/
db.go
File metadata and controls
316 lines (292 loc) · 7.4 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package syncbox
import (
"database/sql"
"os"
"reflect"
"regexp"
"strings"
// imort as driver
_ "github.com/go-sql-driver/mysql"
)
// constants for DB connections
const (
TableNamingSuffix = "Table"
)
// config varaibles and others
var (
camel = regexp.MustCompile("(^[^A-Z]*|[A-Z]*)([A-Z][^A-Z]+|$)")
LocalDBConfg = &DBConfig{
User: "syncbox",
Password: "syncbox",
Host: "localhost",
Port: "3306",
Database: "syncbox",
}
AWSDBConfig = &DBConfig{
User: os.Getenv("SB_DB_USER"),
Password: os.Getenv("SB_DB_PWD"),
Host: os.Getenv("SB_DB_HOST"),
Port: os.Getenv("SB_DB_PORT"),
Database: os.Getenv("SB_DB_DATABASE"),
}
)
// UserTable stores information about users
type UserTable struct {
ID int `mysql:"id,pk,auto_increment"`
Username string `mysql:"username,unique"`
Password string `mysql:"password"`
}
// FileTable stores information about files
type FileTable struct {
ID int `mysql:"id,pk,auto_increment"`
Checksum string `mysql:"checksum,unique"`
UserID int `mysql:"user_id,fk=user.id"`
}
// FileRefTable stores edges that connects from file tree nodes to files
type FileRefTable struct {
ID int `mysql:"id,unique,auto_increment"`
UserID int `mysql:"user_id,pk,fk=user.id"`
FileID int `mysql:"file_id,pk,fk=file.id"`
Path string `mysql:"path,pk"`
Device string `mysql:"device,pk"`
}
// DBConfig is the structure for DB configurations
type DBConfig struct {
User string
Password string
Host string
Port string
Database string
}
// DB is the database
type DB struct {
*DBConfig
*Logger
*sql.DB
Tables []reflect.Value
}
// NewDB instantiates a DB instance
func NewDB(tables ...interface{}) (*DB, error) {
db := &DB{
DBConfig: AWSDBConfig,
Logger: NewDefaultLogger(),
}
conn, err := sql.Open("mysql", db.User+":"+db.Password+"@tcp("+db.Host+":"+db.Port+")/"+db.Database+"?charset=utf8")
if err != nil {
db.Logger.LogDebug("error on new db: %v\n", err)
return nil, err
}
db.DB = conn
for _, table := range tables {
tableType := reflect.ValueOf(table)
db.Tables = append(db.Tables, tableType)
db.Exec(createSchema(table))
if err != nil {
db.Logger.LogDebug("error on execute statement: %v\n", err)
return nil, err
}
}
return db, nil
}
// Exec executes raw SQL commands
func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
stmt, err := db.Prepare(query)
if err != nil {
db.LogDebug("error preparing statment: %v\n", err)
return nil, err
}
var res sql.Result
if len(args) == 0 || (len(args) == 1 && args[0] == nil) {
res, err = stmt.Exec()
} else {
res, err = stmt.Exec(args...)
}
if err != nil {
db.LogDebug("error executing statement: %v\n", err)
return nil, err
}
return res, nil
}
// Query is a structure type that forms a query with builder pattern
type Query struct {
SelectClause string
FromClause string
WhereClause string
db *DB
}
// NewQuery instantiates a query
func NewQuery(db *DB) *Query {
return &Query{
db: db,
SelectClause: "SELECT ",
FromClause: " FROM ",
WhereClause: " WHERE ",
}
}
func (q *Query) copy(q2 *Query) {
q1Val := reflect.ValueOf(q).Elem()
q2Val := reflect.ValueOf(q2).Elem()
for i := 0; i < q1Val.NumField(); i++ {
q1Field := q1Val.Field(i)
q2Field := q2Val.Field(i)
switch q2Field.Kind() {
case reflect.String:
q1Field.SetString(q2Field.String())
case reflect.Int:
q1Field.SetInt(q2Field.Int())
}
}
}
// Select forms the select clause with the constraint
func (q *Query) Select(condition string) *Query {
newQ := NewQuery(q.db)
newQ.copy(q)
newQ.SelectClause += condition
return newQ
}
// From forms the from clause according to the input
func (q *Query) From(condition string) *Query {
newQ := NewQuery(q.db)
newQ.copy(q)
newQ.FromClause += condition
return newQ
}
// Where forms the where clause acoording to the input
func (q *Query) Where(condition string) *Query {
newQ := NewQuery(q.db)
newQ.copy(q)
newQ.WhereClause += condition
return newQ
}
// Exec executes the query
func (q *Query) Exec() (*sql.Rows, error) {
stmt := q.SelectClause + q.FromClause + q.WhereClause
rows, err := q.db.Query(stmt)
if err != nil {
return nil, err
}
return rows, nil
}
// Populate populates data to a slice of reference of tables
// the input should be address of slice of pointers of certain table
func (q *Query) Populate(a interface{}) error {
sliceVal := reflect.ValueOf(a).Elem()
sliceType := reflect.TypeOf(a).Elem()
elemPtr := sliceType.Elem()
elemType := elemPtr.Elem()
rows, err := q.Exec()
if err != nil {
return err
}
records := reflect.MakeSlice(reflect.SliceOf(elemPtr), 0, 0)
for rows.Next() {
recordPtr := reflect.New(elemType)
record := recordPtr.Elem()
fieldNum := elemType.NumField()
fieldList := make([]interface{}, 0, 0)
for i := 0; i < fieldNum; i++ {
field := record.Field(i)
switch field.Kind() {
case reflect.String:
temp := ""
fieldList = append(fieldList, &temp)
case reflect.Int:
temp := 0
fieldList = append(fieldList, &temp)
}
}
rows.Scan(fieldList...)
for i := 0; i < fieldNum; i++ {
field := record.Field(i)
switch field.Kind() {
case reflect.String:
field.Set(reflect.ValueOf(*fieldList[i].(*string)))
case reflect.Int:
field.Set(reflect.ValueOf(*fieldList[i].(*int)))
}
}
records = reflect.Append(records, recordPtr)
}
sliceVal.Set(records)
return nil
}
func createSchema(table interface{}) (string, error) {
stat := "CREATE TABLE IF NOT EXISTS "
structType := reflect.TypeOf(table)
structName := strings.Split(structType.String(), ".")[1]
tableName := underscore(strings.Replace(structName, "Table", "", 1))
stat += tableName + "( "
fieldNum := structType.NumField()
var primaryKeys []string
var constraints []string
for i := 0; i < fieldNum; i++ {
var fieldName string
field := structType.Field(i)
tag := field.Tag.Get("mysql")
opts := strings.Split(tag, ",")
if len(opts) > 0 {
fieldName = opts[0]
opts = opts[1:len(opts)]
} else {
fieldName = field.Name
}
stat += fieldName + " "
fieldType := field.Type
switch fieldType.Kind() {
case reflect.Int:
stat += "INT "
case reflect.String:
stat += "VARCHAR(255) "
}
for _, opt := range opts {
switch {
case opt == "key":
stat += "KEY "
case opt == "pk":
primaryKeys = append(primaryKeys, fieldName)
case opt == "auto_increment":
stat += "AUTO_INCREMENT "
case opt == "unique":
stat += "UNIQUE "
case strings.HasPrefix(opt, "fk"):
fkRef := strings.Replace(opt, "fk=", "", 1)
splitted := strings.Split(fkRef, ".")
table := splitted[0]
column := splitted[1]
constraint := "FOREIGN KEY (" + fieldName + ")" + " REFERENCES " + table + "(" + column + ")"
constraints = append(constraints, constraint)
}
}
stat = strings.TrimRight(stat, " ")
stat += ", "
}
stat += "PRIMARY KEY ("
for _, pk := range primaryKeys {
stat += pk + ", "
}
stat = strings.TrimRight(stat, ", ")
stat += "), "
for _, constraint := range constraints {
stat += constraint + ", "
}
stat = strings.TrimRight(stat, ", ")
stat += ")"
return stat, nil
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func underscore(s string) string {
var a []string
for _, sub := range camel.FindAllStringSubmatch(s, -1) {
if sub[1] != "" {
a = append(a, sub[1])
}
if sub[2] != "" {
a = append(a, sub[2])
}
}
return strings.ToLower(strings.Join(a, "_"))
}