作者 yangfu

fix: some bug

- calculate item rename
- Formula add function MixTableModel()
@@ -428,7 +428,9 @@ func (querySetService *QuerySetService) CalculateItemPreview(ctx *domain.Context @@ -428,7 +428,9 @@ func (querySetService *QuerySetService) CalculateItemPreview(ctx *domain.Context
428 defer func() { 428 defer func() {
429 transactionContext.RollbackTransaction() 429 transactionContext.RollbackTransaction()
430 }() 430 }()
431 - 431 + if q.Formula.MixTableModel() {
  432 + q.Formula.ExprSql = q.Formula.Complete()
  433 + }
432 _, result := GetItemValues(transactionContext, q) 434 _, result := GetItemValues(transactionContext, q)
433 if err := transactionContext.CommitTransaction(); err != nil { 435 if err := transactionContext.CommitTransaction(); err != nil {
434 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 436 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -483,6 +485,10 @@ func GetItemValues(transactionContext application.TransactionContext, q *query.C @@ -483,6 +485,10 @@ func GetItemValues(transactionContext application.TransactionContext, q *query.C
483 if err != nil { 485 if err != nil {
484 return nil, nil 486 return nil, nil
485 } 487 }
  488 +
  489 + if q.Formula.MixTableModel() {
  490 + q.Formula.ExprSql = q.Formula.Complete()
  491 + }
486 value := starrocks.CalculateItemValue(starrocks.DB, q.Formula) 492 value := starrocks.CalculateItemValue(starrocks.DB, q.Formula)
487 493
488 var result = make([]itemValue, 0) 494 var result = make([]itemValue, 0)
@@ -432,6 +432,9 @@ func (tableService *TableService) ValidExprSql(ctx *domain.Context, cmd *command @@ -432,6 +432,9 @@ func (tableService *TableService) ValidExprSql(ctx *domain.Context, cmd *command
432 for _, f := range cmd.TableFields { 432 for _, f := range cmd.TableFields {
433 set.AddStr(f.TableSqlName) 433 set.AddStr(f.TableSqlName)
434 } 434 }
  435 + if cmd.MixTableModel() {
  436 + cmd.ExprSql = cmd.Complete()
  437 + }
435 selectValue := cmd.ExprSql 438 selectValue := cmd.ExprSql
436 //if _, parseErr := strconv.ParseFloat(cmd.ExprSql, 64); parseErr != nil { 439 //if _, parseErr := strconv.ParseFloat(cmd.ExprSql, 64); parseErr != nil {
437 // selectValue = "'" + selectValue + "'" 440 // selectValue = "'" + selectValue + "'"
1 package domain 1 package domain
2 2
3 -import "fmt" 3 +import (
  4 + "fmt"
  5 + "github.com/zeromicro/go-zero/core/collection"
  6 + "strings"
  7 +)
4 8
5 var ( 9 var (
6 SchemaTable TableType = "Schema" // 方案 10 SchemaTable TableType = "Schema" // 方案
@@ -95,6 +99,50 @@ type FieldExpr struct { @@ -95,6 +99,50 @@ type FieldExpr struct {
95 ExprSql string `json:"exprSql"` 99 ExprSql string `json:"exprSql"`
96 } 100 }
97 101
  102 +func (expr *FieldExpr) Complete() string {
  103 + exprSql := expr.ExprSql
  104 + for _, f := range expr.TableFields {
  105 + sql := fmt.Sprintf("%s.%s", f.TableSqlName, f.FieldSqlName)
  106 + //zhSql := fmt.Sprintf("%s.%s", f.TableName, f.FieldSqlName)
  107 + sub := fmt.Sprintf("max(%s.%s)", f.TableSqlName, f.FieldSqlName)
  108 + //zhSub := fmt.Sprintf("max(%s.%s)", f.TableName, f.FieldSqlName)
  109 + if strings.Contains(expr.ExprSql, fmt.Sprintf("(%s)", sql)) {
  110 + continue
  111 + }
  112 + exprSql = strings.ReplaceAll(exprSql, sql, sub)
  113 + //expr.ExprSql = strings.ReplaceAll(expr.ExprSql, sql, sub)
  114 + //expr.ExprHuman = strings.ReplaceAll(expr.ExprHuman, zhSql, zhSub)
  115 + }
  116 + return exprSql
  117 +}
  118 +
  119 +func (expr *FieldExpr) Tables() []int {
  120 + set := collection.NewSet()
  121 + for _, f := range expr.TableFields {
  122 + if f.TableId == 0 {
  123 + continue
  124 + }
  125 + set.Add(f.TableId)
  126 + }
  127 + return set.KeysInt()
  128 +}
  129 +
  130 +func (expr *FieldExpr) MixTableModel() bool {
  131 + var result = 0
  132 + for _, f := range expr.TableFields {
  133 + if strings.HasPrefix(f.TableSqlName, strings.ToLower(CalculateItem.ToString())) {
  134 + if result&1 == 0 {
  135 + result += 1
  136 + }
  137 + } else {
  138 + if result&2 == 0 {
  139 + result += 2
  140 + }
  141 + }
  142 + }
  143 + return result == 3
  144 +}
  145 +
98 type LabelColumn struct { 146 type LabelColumn struct {
99 Column TableField `json:"column,omitempty"` 147 Column TableField `json:"column,omitempty"`
100 LabelExpr string `json:"labelExpr"` 148 LabelExpr string `json:"labelExpr"`
@@ -141,11 +141,15 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon @@ -141,11 +141,15 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon
141 if queryComponent.Formula != nil { 141 if queryComponent.Formula != nil {
142 formula := queryComponent.Formula 142 formula := queryComponent.Formula
143 res.DatabaseTableName = formula.TableFields[0].TableSqlName 143 res.DatabaseTableName = formula.TableFields[0].TableSqlName
  144 + exprSql := formula.ExprSql
  145 + if queryComponent.Formula.MixTableModel() {
  146 + exprSql = queryComponent.Formula.Complete()
  147 + }
144 for _, f := range queryComponent.Formula.TableFields { 148 for _, f := range queryComponent.Formula.TableFields {
145 res.FormulaCalculateFields = append(res.FormulaCalculateFields, &FormulaCalculateField{ 149 res.FormulaCalculateFields = append(res.FormulaCalculateFields, &FormulaCalculateField{
146 DatabaseTableName: f.TableSqlName, 150 DatabaseTableName: f.TableSqlName,
147 FieldSchema: NewFieldSchema(f), 151 FieldSchema: NewFieldSchema(f),
148 - CalculateExpression: formula.ExprSql, 152 + CalculateExpression: exprSql,
149 CalculateFieldName: table.DataFields[0].SQLName, 153 CalculateFieldName: table.DataFields[0].SQLName,
150 }) 154 })
151 } 155 }
@@ -688,6 +688,10 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateItemTable(ctx *domain.Context @@ -688,6 +688,10 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateItemTable(ctx *domain.Context
688 dependencyTables := querySet.GetDependencyTables(queryComponents) 688 dependencyTables := querySet.GetDependencyTables(queryComponents)
689 tableRepository, _ := repository.NewTableRepository(ptr.transactionContext) 689 tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
690 queryComponent := queryComponents[0] 690 queryComponent := queryComponents[0]
  691 + //!!!warning:每个字段默认需要带函数,没有就补全max()
  692 + //if queryComponent.Formula.MixTableModel() {
  693 + // queryComponent.Formula.Complete()
  694 + //}
691 field := DataField(querySet.Name, domain.String.ToString(), domain.MainTableField, 1) 695 field := DataField(querySet.Name, domain.String.ToString(), domain.MainTableField, 1)
692 if len(queryComponent.Formula.TableFields) > 0 { 696 if len(queryComponent.Formula.TableFields) > 0 {
693 field.SQLType = queryComponent.Formula.TableFields[0].FieldSQLType 697 field.SQLType = queryComponent.Formula.TableFields[0].FieldSQLType
@@ -793,6 +797,9 @@ func (ptr *QuerySetService) Rename(ctx *domain.Context, querySetId int, name str @@ -793,6 +797,9 @@ func (ptr *QuerySetService) Rename(ctx *domain.Context, querySetId int, name str
793 return err 797 return err
794 } 798 }
795 table.Name = name 799 table.Name = name
  800 + if table.TableType == domain.CalculateItem.ToString() && len(table.DataFields) > 0 {
  801 + table.DataFields[0].Name = name
  802 + }
796 if _, err := tableRepository.Save(table); err != nil { 803 if _, err := tableRepository.Save(table); err != nil {
797 return err 804 return err
798 } 805 }