作者 yangfu

fix: some bug

- calculate item rename
- Formula add function MixTableModel()
... ... @@ -428,7 +428,9 @@ func (querySetService *QuerySetService) CalculateItemPreview(ctx *domain.Context
defer func() {
transactionContext.RollbackTransaction()
}()
if q.Formula.MixTableModel() {
q.Formula.ExprSql = q.Formula.Complete()
}
_, result := GetItemValues(transactionContext, q)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ... @@ -483,6 +485,10 @@ func GetItemValues(transactionContext application.TransactionContext, q *query.C
if err != nil {
return nil, nil
}
if q.Formula.MixTableModel() {
q.Formula.ExprSql = q.Formula.Complete()
}
value := starrocks.CalculateItemValue(starrocks.DB, q.Formula)
var result = make([]itemValue, 0)
... ...
... ... @@ -432,6 +432,9 @@ func (tableService *TableService) ValidExprSql(ctx *domain.Context, cmd *command
for _, f := range cmd.TableFields {
set.AddStr(f.TableSqlName)
}
if cmd.MixTableModel() {
cmd.ExprSql = cmd.Complete()
}
selectValue := cmd.ExprSql
//if _, parseErr := strconv.ParseFloat(cmd.ExprSql, 64); parseErr != nil {
// selectValue = "'" + selectValue + "'"
... ...
package domain
import "fmt"
import (
"fmt"
"github.com/zeromicro/go-zero/core/collection"
"strings"
)
var (
SchemaTable TableType = "Schema" // 方案
... ... @@ -95,6 +99,50 @@ type FieldExpr struct {
ExprSql string `json:"exprSql"`
}
func (expr *FieldExpr) Complete() string {
exprSql := expr.ExprSql
for _, f := range expr.TableFields {
sql := fmt.Sprintf("%s.%s", f.TableSqlName, f.FieldSqlName)
//zhSql := fmt.Sprintf("%s.%s", f.TableName, f.FieldSqlName)
sub := fmt.Sprintf("max(%s.%s)", f.TableSqlName, f.FieldSqlName)
//zhSub := fmt.Sprintf("max(%s.%s)", f.TableName, f.FieldSqlName)
if strings.Contains(expr.ExprSql, fmt.Sprintf("(%s)", sql)) {
continue
}
exprSql = strings.ReplaceAll(exprSql, sql, sub)
//expr.ExprSql = strings.ReplaceAll(expr.ExprSql, sql, sub)
//expr.ExprHuman = strings.ReplaceAll(expr.ExprHuman, zhSql, zhSub)
}
return exprSql
}
func (expr *FieldExpr) Tables() []int {
set := collection.NewSet()
for _, f := range expr.TableFields {
if f.TableId == 0 {
continue
}
set.Add(f.TableId)
}
return set.KeysInt()
}
func (expr *FieldExpr) MixTableModel() bool {
var result = 0
for _, f := range expr.TableFields {
if strings.HasPrefix(f.TableSqlName, strings.ToLower(CalculateItem.ToString())) {
if result&1 == 0 {
result += 1
}
} else {
if result&2 == 0 {
result += 2
}
}
}
return result == 3
}
type LabelColumn struct {
Column TableField `json:"column,omitempty"`
LabelExpr string `json:"labelExpr"`
... ...
... ... @@ -141,11 +141,15 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon
if queryComponent.Formula != nil {
formula := queryComponent.Formula
res.DatabaseTableName = formula.TableFields[0].TableSqlName
exprSql := formula.ExprSql
if queryComponent.Formula.MixTableModel() {
exprSql = queryComponent.Formula.Complete()
}
for _, f := range queryComponent.Formula.TableFields {
res.FormulaCalculateFields = append(res.FormulaCalculateFields, &FormulaCalculateField{
DatabaseTableName: f.TableSqlName,
FieldSchema: NewFieldSchema(f),
CalculateExpression: formula.ExprSql,
CalculateExpression: exprSql,
CalculateFieldName: table.DataFields[0].SQLName,
})
}
... ...
... ... @@ -688,6 +688,10 @@ func (ptr *QuerySetService) CreateOrUpdateCalculateItemTable(ctx *domain.Context
dependencyTables := querySet.GetDependencyTables(queryComponents)
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
queryComponent := queryComponents[0]
//!!!warning:每个字段默认需要带函数,没有就补全max()
//if queryComponent.Formula.MixTableModel() {
// queryComponent.Formula.Complete()
//}
field := DataField(querySet.Name, domain.String.ToString(), domain.MainTableField, 1)
if len(queryComponent.Formula.TableFields) > 0 {
field.SQLType = queryComponent.Formula.TableFields[0].FieldSQLType
... ... @@ -793,6 +797,9 @@ func (ptr *QuerySetService) Rename(ctx *domain.Context, querySetId int, name str
return err
}
table.Name = name
if table.TableType == domain.CalculateItem.ToString() && len(table.DataFields) > 0 {
table.DataFields[0].Name = name
}
if _, err := tableRepository.Save(table); err != nil {
return err
}
... ...