...
|
...
|
@@ -10,9 +10,9 @@ import ( |
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
...
|
...
|
@@ -433,9 +433,9 @@ func (tableService *TableService) ValidExprSql(ctx *domain.Context, cmd *command |
|
|
set.AddStr(f.TableSqlName)
|
|
|
}
|
|
|
selectValue := cmd.ExprSql
|
|
|
if _, parseErr := strconv.ParseFloat(cmd.ExprSql, 64); parseErr != nil {
|
|
|
selectValue = "'" + selectValue + "'"
|
|
|
}
|
|
|
//if _, parseErr := strconv.ParseFloat(cmd.ExprSql, 64); parseErr != nil {
|
|
|
// selectValue = "'" + selectValue + "'"
|
|
|
//}
|
|
|
sql := "select " + selectValue + " as expr"
|
|
|
if len(set.KeysStr()) > 0 {
|
|
|
sql += " from " + strings.Join(set.KeysStr(), ",")
|
...
|
...
|
@@ -551,6 +551,127 @@ func (tableService *TableService) CheckRowDuplicateV2(ctx *domain.Context, cmd * |
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
func (tableService *TableService) CheckRowDuplicateV3(ctx *domain.Context, cmd *command.CheckRowDuplicateCommand) (interface{}, error) {
|
|
|
var defaultResponse = map[string]interface{}{
|
|
|
"rowDuplicateFlag": false,
|
|
|
}
|
|
|
if cmd.TableId == 0 {
|
|
|
return defaultResponse, nil
|
|
|
}
|
|
|
if err := cmd.ValidateCommand(); err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
}
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
var table *domain.Table
|
|
|
_, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
|
|
|
querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
|
|
|
querySet, _ := querySetRepository.FindOne(map[string]interface{}{"BindTableId": table.TableId, "context": ctx})
|
|
|
|
|
|
if querySet == nil {
|
|
|
cache := redis.NewTableQuerySetCacheService()
|
|
|
if item, cacheItemErr := cache.GetTableQuerySetCache(redis.KeyTableQuerySet(cmd.TableId)); cacheItemErr == nil {
|
|
|
querySet = &domain.QuerySet{QuerySetId: item.QuerySetId, QueryComponents: item.QueryComponents}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if querySet == nil {
|
|
|
return defaultResponse, nil
|
|
|
}
|
|
|
|
|
|
if len(querySet.QueryComponents) == 1 {
|
|
|
return defaultResponse, nil
|
|
|
}
|
|
|
var mapQueryComponents = make(map[int][]*domain.QueryComponent)
|
|
|
for _, queryComponent := range querySet.QueryComponents {
|
|
|
if _, ok := mapQueryComponents[queryComponent.MasterTable.TableId]; !ok {
|
|
|
mapQueryComponents[queryComponent.MasterTable.TableId] = make([]*domain.QueryComponent, 0)
|
|
|
}
|
|
|
mapQueryComponents[queryComponent.MasterTable.TableId] = append(mapQueryComponents[queryComponent.MasterTable.TableId], queryComponent)
|
|
|
}
|
|
|
for _, v := range mapQueryComponents {
|
|
|
if len(v) <= 1 {
|
|
|
continue
|
|
|
}
|
|
|
total, duplicateTotal, err := starrocks.WrapQueryHasDuplicateRowBySql(CountQueryComponents(v), starrocks.DB)()
|
|
|
if err != nil {
|
|
|
return nil, factory.FastError(err)
|
|
|
}
|
|
|
if total != duplicateTotal {
|
|
|
return map[string]interface{}{
|
|
|
"tableName": table.Name,
|
|
|
"rowDuplicateFlag": total != duplicateTotal,
|
|
|
"rowTotal": total,
|
|
|
"rowDuplicateTotal": total - duplicateTotal,
|
|
|
}, nil
|
|
|
}
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
return defaultResponse, nil
|
|
|
}
|
|
|
|
|
|
func CountQueryComponents(queryComponents []*domain.QueryComponent) string {
|
|
|
var sql = "select count(id) c1,count(DISTINCT(id)) c2 from ("
|
|
|
for _, q := range queryComponents {
|
|
|
sql += "\n"
|
|
|
sql += ViewQueryComponent(q)
|
|
|
sql += "\n"
|
|
|
sql += "union all"
|
|
|
}
|
|
|
sql = strings.TrimSuffix(sql, "union all")
|
|
|
sql += ") a"
|
|
|
return sql
|
|
|
}
|
|
|
|
|
|
func ViewQueryComponent(queryComponent *domain.QueryComponent) string {
|
|
|
sql := "select * from " + queryComponent.MasterTable.SQLName
|
|
|
for i, c := range queryComponent.Conditions {
|
|
|
if i == 0 {
|
|
|
sql += " where "
|
|
|
}
|
|
|
if len(c.FieldRight.TableFields) > 0 && queryComponent.MasterTable.TableId != c.FieldRight.TableFields[0].TableId {
|
|
|
continue
|
|
|
}
|
|
|
sql += FormatCondition(c)
|
|
|
sql += " and"
|
|
|
}
|
|
|
sql = strings.TrimSuffix(sql, "and")
|
|
|
return sql
|
|
|
}
|
|
|
|
|
|
func FormatCondition(c domain.ConditionExpr) string {
|
|
|
//if len(c.FieldRight.TableFields) > 0 {
|
|
|
// return fmt.Sprintf("%s %s %s", c.FieldRight.ExprSql, FormatOp(c.OperatorSymbol), FormatByOp(c.OperatorSymbol, c.FieldRight.ExprSql))
|
|
|
//}
|
|
|
//if domain.SQLType(c.FieldLeft.TableFields[0].FieldSQLType).IsString() {
|
|
|
// return fmt.Sprintf("%s %s '%s'", c.FieldRight.ExprSql, FormatOp(c.OperatorSymbol), FormatByOp(c.OperatorSymbol, c.FieldRight.ExprSql))
|
|
|
//}
|
|
|
return fmt.Sprintf("%s %s %s", c.FieldLeft.ExprSql, FormatOp(c.OperatorSymbol), FormatByOp(c.OperatorSymbol, c.FieldRight.ExprSql))
|
|
|
}
|
|
|
|
|
|
func FormatOp(op string) string {
|
|
|
return op
|
|
|
}
|
|
|
|
|
|
func FormatByOp(op string, exprSql string) string {
|
|
|
return exprSql
|
|
|
}
|
|
|
|
|
|
func NewTableService(options map[string]interface{}) *TableService {
|
|
|
newTableService := &TableService{}
|
|
|
return newTableService
|
...
|
...
|
|