作者 yangfu

Merge branch 'feat_datatable_query' into test

@@ -38,7 +38,7 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command @@ -38,7 +38,7 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
38 cacheMiss = true 38 cacheMiss = true
39 } 39 }
40 } 40 }
41 - response := &dto.TablePreviewDto{HiddenData: cmd.HiddenData} 41 + response := &dto.TablePreviewDto{HiddenData: cmd.HiddenData, TableType: table.TableType, Name: table.Name, TableId: table.TableId}
42 if dataTable == nil { 42 if dataTable == nil {
43 switch table.TableType { 43 switch table.TableType {
44 case domain.CalculateSet.ToString(): 44 case domain.CalculateSet.ToString():
@@ -52,8 +52,9 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command @@ -52,8 +52,9 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
52 if err != nil { 52 if err != nil {
53 return nil, factory.FastError(err) 53 return nil, factory.FastError(err)
54 } 54 }
  55 + data, total := dataTable.FilterByWhere(cmd.Where)
55 response.Fields = dataTable.Fields 56 response.Fields = dataTable.Fields
56 - response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, pageData(cmd.Where.PageNumber, cmd.Where.PageSize, dataTable.Data), false), int64(len(dataTable.Data))) 57 + response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, data, false), total)
57 default: 58 default:
58 var options = starrocks.QueryOptions{ 59 var options = starrocks.QueryOptions{
59 Table: table, 60 Table: table,
@@ -76,8 +77,9 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command @@ -76,8 +77,9 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
76 } else { 77 } else {
77 switch table.TableType { 78 switch table.TableType {
78 case domain.CalculateSet.ToString(): 79 case domain.CalculateSet.ToString():
  80 + data, total := dataTable.FilterByWhere(cmd.Where)
79 response.Fields = dataTable.Fields 81 response.Fields = dataTable.Fields
80 - response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, pageData(cmd.Where.PageNumber, cmd.Where.PageSize, dataTable.Data), false), int64(len(dataTable.Data))) 82 + response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, data, false), total)
81 default: 83 default:
82 response.Load(table, dataTable, domain.ObjectMetaTable) 84 response.Load(table, dataTable, domain.ObjectMetaTable)
83 } 85 }
1 package domain 1 package domain
2 2
3 -import "strings" 3 +import (
  4 + "strings"
  5 +)
4 6
5 type DataTable struct { 7 type DataTable struct {
6 Fields []*Field `json:"fields"` 8 Fields []*Field `json:"fields"`
@@ -113,7 +115,81 @@ func (t *DataTable) MatchFields(from []*Field) []*Field { @@ -113,7 +115,81 @@ func (t *DataTable) MatchFields(from []*Field) []*Field {
113 return from 115 return from
114 } 116 }
115 117
  118 +func (t *DataTable) FilterByWhere(where Where) (response [][]string, length int64) {
  119 + pageNumber, pageSize := where.PageNumber, where.PageSize
  120 + response = make([][]string, 0)
  121 + wrapperFilters := func(data []string, next ...func([]string) bool) ([]string, bool) {
  122 + ok := true
  123 + for i := range next {
  124 + if !next[i](data) {
  125 + ok = false
  126 + }
  127 + if !ok {
  128 + break
  129 + }
  130 + }
  131 + if !ok {
  132 + return nil, false
  133 + }
  134 + return data, true
  135 + }
  136 + filterFuncList := make([]func([]string) bool, 0)
  137 + for _, c := range where.Conditions {
  138 + var index = 0
  139 + var filed *Field
  140 + for i, f := range t.Fields {
  141 + if f.Name == c.Field.Name {
  142 + index = i
  143 + filed = f
  144 + break
  145 + }
  146 + }
  147 + if filed == nil {
  148 + continue
  149 + }
  150 + if len(c.In) > 0 {
  151 + filterFuncList = append(filterFuncList, WithInFilter(c.In, index))
  152 + }
  153 + }
  154 + for _, data := range t.Data {
  155 + if v, ok := wrapperFilters(data, filterFuncList...); ok {
  156 + response = append(response, v)
  157 + }
  158 + }
  159 + length = int64(len(response))
  160 + response = pageData(pageNumber, pageSize, response)
  161 + return
  162 +}
  163 +
116 type RangStruct struct { 164 type RangStruct struct {
117 Op string `json:"op"` 165 Op string `json:"op"`
118 Val interface{} `json:"val"` 166 Val interface{} `json:"val"`
119 } 167 }
  168 +
  169 +func pageData(pageNumber, pageSize int, data [][]string) [][]string {
  170 + if pageNumber == 0 || pageSize == 0 {
  171 + return data
  172 + }
  173 + offset := (pageNumber - 1) * pageSize
  174 + if len(data) < offset {
  175 + return [][]string{}
  176 + }
  177 + if len(data) < offset+pageSize {
  178 + pageSize = len(data) - offset
  179 + }
  180 + return data[offset : offset+pageSize]
  181 +}
  182 +
  183 +func WithInFilter(in []interface{}, index int) func(data []string) bool {
  184 + return func(data []string) bool {
  185 + if len(data) < index {
  186 + return false
  187 + }
  188 + for _, item := range in {
  189 + if data[index] == item.(string) {
  190 + return true
  191 + }
  192 + }
  193 + return false
  194 + }
  195 +}