作者 yangfu

fix: mata table query

@@ -19,7 +19,10 @@ type FieldOptionalValuesCommand struct { @@ -19,7 +19,10 @@ type FieldOptionalValuesCommand struct {
19 } 19 }
20 20
21 func (cmd *FieldOptionalValuesCommand) Valid(validation *validation.Validation) { 21 func (cmd *FieldOptionalValuesCommand) Valid(validation *validation.Validation) {
22 - 22 + if cmd.PageSize == 0 {
  23 + cmd.PageNumber = 1
  24 + cmd.PageSize = 2000
  25 + }
23 } 26 }
24 27
25 func (cmd *FieldOptionalValuesCommand) ValidateCommand() error { 28 func (cmd *FieldOptionalValuesCommand) ValidateCommand() error {
@@ -26,6 +26,10 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -26,6 +26,10 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
26 transactionContext.RollbackTransaction() 26 transactionContext.RollbackTransaction()
27 }() 27 }()
28 var dataTable *domain.DataTable 28 var dataTable *domain.DataTable
  29 + var empty = map[string]interface{}{
  30 + "values": []string{},
  31 + "total": 0,
  32 + }
29 tableRepository, _, _ := factory.FastPgTable(transactionContext, 0) 33 tableRepository, _, _ := factory.FastPgTable(transactionContext, 0)
30 var table *domain.Table 34 var table *domain.Table
31 var db *gorm.DB 35 var db *gorm.DB
@@ -63,6 +67,9 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd * @@ -63,6 +67,9 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
63 if !ok { 67 if !ok {
64 return nil, factory.FastError(fmt.Errorf("列:%v 不存在", cmd.Field.Name)) 68 return nil, factory.FastError(fmt.Errorf("列:%v 不存在", cmd.Field.Name))
65 } 69 }
  70 + if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
  71 + return empty, nil
  72 + }
66 73
67 options := &starrocks.QueryOptions{ 74 options := &starrocks.QueryOptions{
68 TableName: table.SQLName, 75 TableName: table.SQLName,
@@ -24,7 +24,7 @@ type Condition struct { @@ -24,7 +24,7 @@ type Condition struct {
24 Like string `json:"like"` 24 Like string `json:"like"`
25 In []interface{} `json:"in"` 25 In []interface{} `json:"in"`
26 Ex []interface{} `json:"ex"` 26 Ex []interface{} `json:"ex"`
27 - Range []interface{} `json:"range"` 27 + Range []RangStruct `json:"range"`
28 Order string `json:"order"` 28 Order string `json:"order"`
29 } 29 }
30 30
@@ -44,3 +44,8 @@ func (t *DataTable) OptionalValue() []string { @@ -44,3 +44,8 @@ func (t *DataTable) OptionalValue() []string {
44 func (t *DataTable) MatchFields(from []*Field) []*Field { 44 func (t *DataTable) MatchFields(from []*Field) []*Field {
45 return from 45 return from
46 } 46 }
  47 +
  48 +type RangStruct struct {
  49 + Op string `json:"op"`
  50 + Val interface{} `json:"val"`
  51 +}
@@ -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 }
@@ -27,7 +27,7 @@ func Init() error { @@ -27,7 +27,7 @@ func Init() error {
27 Colorful: false, // Disable color 27 Colorful: false, // Disable color
28 }, 28 },
29 ) 29 )
30 - DB, err = gorm.Open(mysql.Open(constant.STARROCKS_MYSQL_DATA_SOURCE), &gorm.Config{Logger: newLogger}) 30 + DB, err = gorm.Open(mysql.Open(constant.STARROCKS_MYSQL_DATA_SOURCE), &gorm.Config{Logger: newLogger, PrepareStmt: false})
31 31
32 //Test1() 32 //Test1()
33 return err 33 return err