|
@@ -5,11 +5,14 @@ import ( |
|
@@ -5,11 +5,14 @@ import ( |
|
5
|
"fmt"
|
5
|
"fmt"
|
|
6
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
6
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
|
7
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
|
7
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
|
|
|
|
8
|
+ "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
|
|
8
|
"gorm.io/gorm"
|
9
|
"gorm.io/gorm"
|
|
9
|
"reflect"
|
10
|
"reflect"
|
|
10
|
"strings"
|
11
|
"strings"
|
|
11
|
)
|
12
|
)
|
|
12
|
|
13
|
|
|
|
|
14
|
+var AssertString = utils.AssertString
|
|
|
|
15
|
+
|
|
13
|
func Query(params QueryOptions, queryFunc func(params QueryOptions) (*sql.Rows, error)) (*domain.DataTable, error) {
|
16
|
func Query(params QueryOptions, queryFunc func(params QueryOptions) (*sql.Rows, error)) (*domain.DataTable, error) {
|
|
14
|
rows, err := queryFunc(params)
|
17
|
rows, err := queryFunc(params)
|
|
15
|
if err != nil {
|
18
|
if err != nil {
|
|
@@ -97,6 +100,27 @@ func (c Condition) SetWhere(q *gorm.DB) { |
|
@@ -97,6 +100,27 @@ func (c Condition) SetWhere(q *gorm.DB) { |
|
97
|
in := c.InArgs(c.Ex)
|
100
|
in := c.InArgs(c.Ex)
|
|
98
|
q.Where(fmt.Sprintf("%v not in %v", c.Field.SQLName, in))
|
101
|
q.Where(fmt.Sprintf("%v not in %v", c.Field.SQLName, in))
|
|
99
|
}
|
102
|
}
|
|
|
|
103
|
+ if len(c.Range) > 0 {
|
|
|
|
104
|
+ for _, item := range c.Range {
|
|
|
|
105
|
+ if item.Op == "" {
|
|
|
|
106
|
+ continue
|
|
|
|
107
|
+ }
|
|
|
|
108
|
+ opVal, ok := opMap[item.Op]
|
|
|
|
109
|
+ if !ok {
|
|
|
|
110
|
+ continue
|
|
|
|
111
|
+ }
|
|
|
|
112
|
+ val, err := domain.ValueToType(AssertString(item.Val), c.Field.SQLType)
|
|
|
|
113
|
+ if err != nil {
|
|
|
|
114
|
+ log.Logger.Error(err.Error())
|
|
|
|
115
|
+ continue
|
|
|
|
116
|
+ }
|
|
|
|
117
|
+ q.Where(fmt.Sprintf("%s %s %s",
|
|
|
|
118
|
+ c.Field.SQLName,
|
|
|
|
119
|
+ opVal,
|
|
|
|
120
|
+ c.formatByOp(item.Op, val),
|
|
|
|
121
|
+ ))
|
|
|
|
122
|
+ }
|
|
|
|
123
|
+ }
|
|
100
|
if c.Distinct {
|
124
|
if c.Distinct {
|
|
101
|
q.Distinct(c.Field.SQLName)
|
125
|
q.Distinct(c.Field.SQLName)
|
|
102
|
}
|
126
|
}
|
|
@@ -104,12 +128,37 @@ func (c Condition) SetWhere(q *gorm.DB) { |
|
@@ -104,12 +128,37 @@ func (c Condition) SetWhere(q *gorm.DB) { |
|
104
|
q.Order(fmt.Sprintf("%v %v", c.Field.SQLName, c.Order))
|
128
|
q.Order(fmt.Sprintf("%v %v", c.Field.SQLName, c.Order))
|
|
105
|
}
|
129
|
}
|
|
106
|
}
|
130
|
}
|
|
|
|
131
|
+
|
|
|
|
132
|
+var opMap = map[string]string{
|
|
|
|
133
|
+ "=": "=",
|
|
|
|
134
|
+ ">": ">",
|
|
|
|
135
|
+ "<": "<",
|
|
|
|
136
|
+ ">=": ">=",
|
|
|
|
137
|
+ "<=": "<=",
|
|
|
|
138
|
+ "<>": "<>",
|
|
|
|
139
|
+ "like": "like",
|
|
|
|
140
|
+ "not like": "not like",
|
|
|
|
141
|
+}
|
|
|
|
142
|
+
|
|
|
|
143
|
+func (c Condition) formatByOp(op string, val interface{}) string {
|
|
|
|
144
|
+ if op == "like" || op == "not like" {
|
|
|
|
145
|
+ return fmt.Sprintf("'%%%v%%'", AssertString(val))
|
|
|
|
146
|
+ }
|
|
|
|
147
|
+ return c.Arg(val)
|
|
|
|
148
|
+}
|
|
|
|
149
|
+
|
|
107
|
func (c Condition) InArgs(args interface{}) string {
|
150
|
func (c Condition) InArgs(args interface{}) string {
|
|
108
|
bytes := make([]byte, 0)
|
151
|
bytes := make([]byte, 0)
|
|
109
|
bytes = appendIn(bytes, reflect.ValueOf(args))
|
152
|
bytes = appendIn(bytes, reflect.ValueOf(args))
|
|
110
|
return string(bytes)
|
153
|
return string(bytes)
|
|
111
|
}
|
154
|
}
|
|
112
|
|
155
|
|
|
|
|
156
|
+func (c Condition) Arg(args interface{}) string {
|
|
|
|
157
|
+ bytes := make([]byte, 0)
|
|
|
|
158
|
+ bytes = appendValue(bytes, reflect.ValueOf(args))
|
|
|
|
159
|
+ return string(bytes)
|
|
|
|
160
|
+}
|
|
|
|
161
|
+
|
|
113
|
func appendIn(b []byte, slice reflect.Value) []byte {
|
162
|
func appendIn(b []byte, slice reflect.Value) []byte {
|
|
114
|
sliceLen := slice.Len()
|
163
|
sliceLen := slice.Len()
|
|
115
|
b = append(b, '(')
|
164
|
b = append(b, '(')
|
|
@@ -186,7 +235,7 @@ func queryWithoutLimitOffset(query *gorm.DB, params QueryOptions) { |
|
@@ -186,7 +235,7 @@ func queryWithoutLimitOffset(query *gorm.DB, params QueryOptions) { |
|
186
|
if len(params.Select) > 0 {
|
235
|
if len(params.Select) > 0 {
|
|
187
|
fields := make([]string, 0)
|
236
|
fields := make([]string, 0)
|
|
188
|
for _, f := range params.Select {
|
237
|
for _, f := range params.Select {
|
|
189
|
- if f.Flag==domain.ManualField{
|
238
|
+ if f.Flag == domain.ManualField {
|
|
190
|
fields = append(fields, "'' "+f.SQLName)
|
239
|
fields = append(fields, "'' "+f.SQLName)
|
|
191
|
continue
|
240
|
continue
|
|
192
|
}
|
241
|
}
|
|
@@ -226,7 +275,7 @@ func WrapQueryCountWithDB(params QueryOptions, db *gorm.DB) func() (int64, error |
|
@@ -226,7 +275,7 @@ func WrapQueryCountWithDB(params QueryOptions, db *gorm.DB) func() (int64, error |
|
226
|
func ArrayInterfaceToString(args []interface{}) []string {
|
275
|
func ArrayInterfaceToString(args []interface{}) []string {
|
|
227
|
result := make([]string, 0)
|
276
|
result := make([]string, 0)
|
|
228
|
for _, arg := range args {
|
277
|
for _, arg := range args {
|
|
229
|
- result = append(result, utils.AssertString(arg))
|
278
|
+ result = append(result, AssertString(arg))
|
|
230
|
}
|
279
|
}
|
|
231
|
return result
|
280
|
return result
|
|
232
|
} |
281
|
} |