-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
executable file
·210 lines (186 loc) · 5.45 KB
/
model.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
package model
import (
"sort"
"strings"
"github.com/Datosystem/go_api_core/message"
"github.com/Datosystem/go_api_core/params"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
type UpdateConditions struct {
Name string `json:"name"`
Default bool `json:"default"`
Conditions map[string]interface{} `json:"conditions"`
}
type UpdateCondition struct {
Field string `json:"field"`
Operator string `json:"operator"`
Value interface{} `json:"value"`
}
type ConditionsModel interface {
DefaultConditions(*gorm.DB, string) (query string, args []interface{})
}
type UpdateConditionsModel interface {
UpdateConditions() []UpdateConditions
}
type JoinsModel interface {
DefaultJoins(*gorm.DB, string) string
}
type OrderedModel interface {
DefaultOrder(*gorm.DB, string) string
}
type ValidationModel interface {
Validate(*gin.Context) message.Message
}
type TableModel interface {
TableName() string
}
type DisplayNameRelationsModel interface {
DisplayNameRelations() []string
}
type DisplayNamePatternModel interface {
DisplayNamePattern() string
}
type tableField struct {
Table string
Field *schema.Field
}
type BaseModel struct {
Delete bool `gorm:"-" json:"$delete,omitempty"`
DISPLAY_NAME string `gorm:"-" query:"" json:",omitempty" label:"Nome di visualizzazione"`
}
func (BaseModel) QueryDISPLAY_NAME(c *gin.Context, model interface{}, modelSchema *schema.Schema, table string, nested bool, query *string, args *[]any, rels map[string]*params.Conditions) message.Message {
if m, ok := model.(DisplayNamePatternModel); ok {
pattern := m.DisplayNamePattern()
sel, relSet := DisplayPatternToSql(pattern, modelSchema, table, nested)
for rel := range relSet {
rels[rel] = ¶ms.Conditions{}
}
*query = "LTRIM(RTRIM(" + sel + "))"
return nil
} else {
if m, ok := model.(DisplayNameRelationsModel); ok {
for _, rel := range m.DisplayNameRelations() {
if nested {
rels[strings.ReplaceAll(table, "__", ".")+"."+rel] = ¶ms.Conditions{}
} else {
rels[rel] = ¶ms.Conditions{}
}
}
}
fields := []tableField{}
for rel := range rels {
relSchema := modelSchema
pieces := strings.Split(rel, ".")
for _, piece := range pieces {
if r, ok := relSchema.Relationships.Relations[piece]; ok {
relSchema = r.FieldSchema
} else {
panic("Invalid relation " + rel)
}
}
alias := strings.ReplaceAll(rel, ".", "__")
relFields := []tableField{}
for _, field := range relSchema.Fields {
if strings.HasPrefix(field.Tag.Get("desc"), "1.") {
relFields = append(relFields, tableField{Table: alias, Field: field})
}
}
sort.SliceStable(relFields, func(i, j int) bool {
return relFields[i].Field.Tag.Get("desc") < relFields[j].Field.Tag.Get("desc")
})
fields = append(fields, relFields...)
}
relFields := []tableField{}
for _, field := range modelSchema.Fields {
if strings.HasPrefix(field.Tag.Get("desc"), "1.") {
relFields = append(relFields, tableField{Table: table, Field: field})
}
}
sort.SliceStable(relFields, func(i, j int) bool {
return relFields[i].Field.Tag.Get("desc") < relFields[j].Field.Tag.Get("desc")
})
fields = append(fields, relFields...)
if len(fields) == 0 {
return message.DisplayNameNotSupported(c)
}
var sel string
if len(fields) > 0 {
t := fields[0].Table
for i := range fields {
if fields[i].Table != t {
sel += "+ ' - ' +"
}
sel += DisplayFieldToSql(fields[i].Table, fields[i].Field, i > 0)
}
}
*query = "LTRIM(RTRIM(" + sel + "))"
return nil
}
}
func DisplayFieldToSql(table string, field *schema.Field, concat bool) string {
sel := "CASE WHEN " + table + "." + field.DBName + " IS NOT NULL"
if field.DataType == schema.String {
sel += " AND LEN(" + table + "." + field.DBName + ") > 0"
}
sel += " THEN"
if concat {
sel = " + " + sel + " ' ' + "
}
if field.DataType == schema.String {
sel += table + "." + field.DBName
} else {
sel += " CAST(" + table + "." + field.DBName + " AS NVARCHAR(MAX))"
}
return sel + " ELSE '' END"
}
func DisplayPatternToSql(pattern string, modelSchema *schema.Schema, table string, nested bool) (string, map[string]*params.Conditions) {
var startIndex int
var sel string
relSet := map[string]*params.Conditions{}
for i, char := range pattern {
if string(char) == "{" {
if i-startIndex > 0 {
if len(sel) > 0 {
sel += "+"
}
sel += "'" + pattern[startIndex:i] + "'"
}
startIndex = i + 1
} else if string(char) == "}" {
pieces := strings.Split(pattern[startIndex:i], ".")
t := table
relSchema := modelSchema
if len(pieces) > 1 {
for _, piece := range pieces[:len(pieces)-1] {
if r, ok := relSchema.Relationships.Relations[piece]; ok {
relSchema = r.FieldSchema
} else {
panic("Invalid field " + pattern[startIndex:i])
}
}
rel := strings.Join(pieces[:len(pieces)-1], ".")
if nested {
relSet[strings.ReplaceAll(table, "__", ".")+"."+rel] = ¶ms.Conditions{}
t = table + "__" + strings.ReplaceAll(rel, ".", "__")
} else {
relSet[rel] = ¶ms.Conditions{}
t = strings.ReplaceAll(rel, ".", "__")
}
}
if len(sel) > 0 {
sel += "+"
}
sel += DisplayFieldToSql(t, relSchema.LookUpField(pieces[len(pieces)-1]), false)
startIndex = i + 1
}
}
if len(pattern)-startIndex > 0 {
if len(sel) > 0 {
sel += "+"
}
sel += "'" + pattern[startIndex:] + "'"
}
return sel, relSet
}