作者 yangfu

Merge branch 'feat_datatable_query' into test

... ... @@ -38,7 +38,7 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
cacheMiss = true
}
}
response := &dto.TablePreviewDto{HiddenData: cmd.HiddenData}
response := &dto.TablePreviewDto{HiddenData: cmd.HiddenData, TableType: table.TableType, Name: table.Name, TableId: table.TableId}
if dataTable == nil {
switch table.TableType {
case domain.CalculateSet.ToString():
... ... @@ -52,8 +52,9 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
if err != nil {
return nil, factory.FastError(err)
}
data, total := dataTable.FilterByWhere(cmd.Where)
response.Fields = dataTable.Fields
response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, pageData(cmd.Where.PageNumber, cmd.Where.PageSize, dataTable.Data), false), int64(len(dataTable.Data)))
response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, data, false), total)
default:
var options = starrocks.QueryOptions{
Table: table,
... ... @@ -76,8 +77,9 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
} else {
switch table.TableType {
case domain.CalculateSet.ToString():
data, total := dataTable.FilterByWhere(cmd.Where)
response.Fields = dataTable.Fields
response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, pageData(cmd.Where.PageNumber, cmd.Where.PageSize, dataTable.Data), false), int64(len(dataTable.Data)))
response.Data = domain.GripData(domain.ToFieldData(dataTable.Fields, data, false), total)
default:
response.Load(table, dataTable, domain.ObjectMetaTable)
}
... ...
package domain
import "strings"
import (
"strings"
)
type DataTable struct {
Fields []*Field `json:"fields"`
... ... @@ -113,7 +115,81 @@ func (t *DataTable) MatchFields(from []*Field) []*Field {
return from
}
func (t *DataTable) FilterByWhere(where Where) (response [][]string, length int64) {
pageNumber, pageSize := where.PageNumber, where.PageSize
response = make([][]string, 0)
wrapperFilters := func(data []string, next ...func([]string) bool) ([]string, bool) {
ok := true
for i := range next {
if !next[i](data) {
ok = false
}
if !ok {
break
}
}
if !ok {
return nil, false
}
return data, true
}
filterFuncList := make([]func([]string) bool, 0)
for _, c := range where.Conditions {
var index = 0
var filed *Field
for i, f := range t.Fields {
if f.Name == c.Field.Name {
index = i
filed = f
break
}
}
if filed == nil {
continue
}
if len(c.In) > 0 {
filterFuncList = append(filterFuncList, WithInFilter(c.In, index))
}
}
for _, data := range t.Data {
if v, ok := wrapperFilters(data, filterFuncList...); ok {
response = append(response, v)
}
}
length = int64(len(response))
response = pageData(pageNumber, pageSize, response)
return
}
type RangStruct struct {
Op string `json:"op"`
Val interface{} `json:"val"`
}
func pageData(pageNumber, pageSize int, data [][]string) [][]string {
if pageNumber == 0 || pageSize == 0 {
return data
}
offset := (pageNumber - 1) * pageSize
if len(data) < offset {
return [][]string{}
}
if len(data) < offset+pageSize {
pageSize = len(data) - offset
}
return data[offset : offset+pageSize]
}
func WithInFilter(in []interface{}, index int) func(data []string) bool {
return func(data []string) bool {
if len(data) < index {
return false
}
for _, item := range in {
if data[index] == item.(string) {
return true
}
}
return false
}
}
... ...