-
Notifications
You must be signed in to change notification settings - Fork 0
/
asterics.go
46 lines (40 loc) · 1.05 KB
/
asterics.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
package store
import (
"fmt"
"reflect"
"strings"
"github.com/l-orlov/slim-fairy/bot/internal/model"
)
// Column names for models
var (
asteriskUsers,
asteriskNutritionists,
asteriskAuthData,
asteriskChatBotDialogs,
asteriskAIAPILogs string
)
func init() {
// Init column names for models
asteriskUsers = asterisk(model.User{})
asteriskNutritionists = asterisk(model.Nutritionist{})
asteriskAuthData = asterisk(model.AuthData{})
asteriskChatBotDialogs = asterisk(model.ChatBotDialog{})
asteriskAIAPILogs = asterisk(model.AIAPILog{})
}
type tableNameGetter interface {
DbTable() string
}
// asterisk replace * in queries select(*) by column names (only for models without nesting)
func asterisk(a tableNameGetter) string {
modelType := reflect.TypeOf(a)
var columns []string
for i := 0; i < modelType.NumField(); i++ {
field := modelType.Field(i)
columnName, ok := field.Tag.Lookup("db")
if !ok || columnName == "-" {
continue
}
columns = append(columns, fmt.Sprintf("%s.%s", a.DbTable(), columnName))
}
return strings.Join(columns, ", ")
}