|
@@ -10,9 +10,9 @@ import ( |
|
@@ -10,9 +10,9 @@ import ( |
|
10
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
|
10
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/dto"
|
|
11
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
|
11
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/table/query"
|
|
12
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
12
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
|
|
|
|
13
|
+ "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
|
|
13
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
|
14
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks"
|
|
14
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
|
15
|
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
|
|
15
|
- "strconv"
|
|
|
|
16
|
"strings"
|
16
|
"strings"
|
|
17
|
)
|
17
|
)
|
|
18
|
|
18
|
|
|
@@ -433,9 +433,9 @@ func (tableService *TableService) ValidExprSql(ctx *domain.Context, cmd *command |
|
@@ -433,9 +433,9 @@ func (tableService *TableService) ValidExprSql(ctx *domain.Context, cmd *command |
|
433
|
set.AddStr(f.TableSqlName)
|
433
|
set.AddStr(f.TableSqlName)
|
|
434
|
}
|
434
|
}
|
|
435
|
selectValue := cmd.ExprSql
|
435
|
selectValue := cmd.ExprSql
|
|
436
|
- if _, parseErr := strconv.ParseFloat(cmd.ExprSql, 64); parseErr != nil {
|
|
|
|
437
|
- selectValue = "'" + selectValue + "'"
|
|
|
|
438
|
- }
|
436
|
+ //if _, parseErr := strconv.ParseFloat(cmd.ExprSql, 64); parseErr != nil {
|
|
|
|
437
|
+ // selectValue = "'" + selectValue + "'"
|
|
|
|
438
|
+ //}
|
|
439
|
sql := "select " + selectValue + " as expr"
|
439
|
sql := "select " + selectValue + " as expr"
|
|
440
|
if len(set.KeysStr()) > 0 {
|
440
|
if len(set.KeysStr()) > 0 {
|
|
441
|
sql += " from " + strings.Join(set.KeysStr(), ",")
|
441
|
sql += " from " + strings.Join(set.KeysStr(), ",")
|
|
@@ -551,6 +551,127 @@ func (tableService *TableService) CheckRowDuplicateV2(ctx *domain.Context, cmd * |
|
@@ -551,6 +551,127 @@ func (tableService *TableService) CheckRowDuplicateV2(ctx *domain.Context, cmd * |
|
551
|
}, nil
|
551
|
}, nil
|
|
552
|
}
|
552
|
}
|
|
553
|
|
553
|
|
|
|
|
554
|
+func (tableService *TableService) CheckRowDuplicateV3(ctx *domain.Context, cmd *command.CheckRowDuplicateCommand) (interface{}, error) {
|
|
|
|
555
|
+ var defaultResponse = map[string]interface{}{
|
|
|
|
556
|
+ "rowDuplicateFlag": false,
|
|
|
|
557
|
+ }
|
|
|
|
558
|
+ if cmd.TableId == 0 {
|
|
|
|
559
|
+ return defaultResponse, nil
|
|
|
|
560
|
+ }
|
|
|
|
561
|
+ if err := cmd.ValidateCommand(); err != nil {
|
|
|
|
562
|
+ return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
|
|
|
563
|
+ }
|
|
|
|
564
|
+ transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
|
565
|
+ if err != nil {
|
|
|
|
566
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
|
567
|
+ }
|
|
|
|
568
|
+ if err := transactionContext.StartTransaction(); err != nil {
|
|
|
|
569
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
|
570
|
+ }
|
|
|
|
571
|
+ defer func() {
|
|
|
|
572
|
+ transactionContext.RollbackTransaction()
|
|
|
|
573
|
+ }()
|
|
|
|
574
|
+
|
|
|
|
575
|
+ var table *domain.Table
|
|
|
|
576
|
+ _, table, err = factory.FastPgTable(transactionContext, cmd.TableId)
|
|
|
|
577
|
+ if err != nil {
|
|
|
|
578
|
+ return nil, factory.FastError(err)
|
|
|
|
579
|
+ }
|
|
|
|
580
|
+
|
|
|
|
581
|
+ querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0)
|
|
|
|
582
|
+ querySet, _ := querySetRepository.FindOne(map[string]interface{}{"BindTableId": table.TableId, "context": ctx})
|
|
|
|
583
|
+
|
|
|
|
584
|
+ if querySet == nil {
|
|
|
|
585
|
+ cache := redis.NewTableQuerySetCacheService()
|
|
|
|
586
|
+ if item, cacheItemErr := cache.GetTableQuerySetCache(redis.KeyTableQuerySet(cmd.TableId)); cacheItemErr == nil {
|
|
|
|
587
|
+ querySet = &domain.QuerySet{QuerySetId: item.QuerySetId, QueryComponents: item.QueryComponents}
|
|
|
|
588
|
+ }
|
|
|
|
589
|
+ }
|
|
|
|
590
|
+
|
|
|
|
591
|
+ if querySet == nil {
|
|
|
|
592
|
+ return defaultResponse, nil
|
|
|
|
593
|
+ }
|
|
|
|
594
|
+
|
|
|
|
595
|
+ if len(querySet.QueryComponents) == 1 {
|
|
|
|
596
|
+ return defaultResponse, nil
|
|
|
|
597
|
+ }
|
|
|
|
598
|
+ var mapQueryComponents = make(map[int][]*domain.QueryComponent)
|
|
|
|
599
|
+ for _, queryComponent := range querySet.QueryComponents {
|
|
|
|
600
|
+ if _, ok := mapQueryComponents[queryComponent.MasterTable.TableId]; !ok {
|
|
|
|
601
|
+ mapQueryComponents[queryComponent.MasterTable.TableId] = make([]*domain.QueryComponent, 0)
|
|
|
|
602
|
+ }
|
|
|
|
603
|
+ mapQueryComponents[queryComponent.MasterTable.TableId] = append(mapQueryComponents[queryComponent.MasterTable.TableId], queryComponent)
|
|
|
|
604
|
+ }
|
|
|
|
605
|
+ for _, v := range mapQueryComponents {
|
|
|
|
606
|
+ if len(v) <= 1 {
|
|
|
|
607
|
+ continue
|
|
|
|
608
|
+ }
|
|
|
|
609
|
+ total, duplicateTotal, err := starrocks.WrapQueryHasDuplicateRowBySql(CountQueryComponents(v), starrocks.DB)()
|
|
|
|
610
|
+ if err != nil {
|
|
|
|
611
|
+ return nil, factory.FastError(err)
|
|
|
|
612
|
+ }
|
|
|
|
613
|
+ if total != duplicateTotal {
|
|
|
|
614
|
+ return map[string]interface{}{
|
|
|
|
615
|
+ "tableName": table.Name,
|
|
|
|
616
|
+ "rowDuplicateFlag": total != duplicateTotal,
|
|
|
|
617
|
+ "rowTotal": total,
|
|
|
|
618
|
+ "rowDuplicateTotal": total - duplicateTotal,
|
|
|
|
619
|
+ }, nil
|
|
|
|
620
|
+ }
|
|
|
|
621
|
+ }
|
|
|
|
622
|
+ if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
|
623
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
|
624
|
+ }
|
|
|
|
625
|
+ return defaultResponse, nil
|
|
|
|
626
|
+}
|
|
|
|
627
|
+
|
|
|
|
628
|
+func CountQueryComponents(queryComponents []*domain.QueryComponent) string {
|
|
|
|
629
|
+ var sql = "select count(id) c1,count(DISTINCT(id)) c2 from ("
|
|
|
|
630
|
+ for _, q := range queryComponents {
|
|
|
|
631
|
+ sql += "\n"
|
|
|
|
632
|
+ sql += ViewQueryComponent(q)
|
|
|
|
633
|
+ sql += "\n"
|
|
|
|
634
|
+ sql += "union all"
|
|
|
|
635
|
+ }
|
|
|
|
636
|
+ sql = strings.TrimSuffix(sql, "union all")
|
|
|
|
637
|
+ sql += ") a"
|
|
|
|
638
|
+ return sql
|
|
|
|
639
|
+}
|
|
|
|
640
|
+
|
|
|
|
641
|
+func ViewQueryComponent(queryComponent *domain.QueryComponent) string {
|
|
|
|
642
|
+ sql := "select * from " + queryComponent.MasterTable.SQLName
|
|
|
|
643
|
+ for i, c := range queryComponent.Conditions {
|
|
|
|
644
|
+ if i == 0 {
|
|
|
|
645
|
+ sql += " where "
|
|
|
|
646
|
+ }
|
|
|
|
647
|
+ if len(c.FieldRight.TableFields) > 0 && queryComponent.MasterTable.TableId != c.FieldRight.TableFields[0].TableId {
|
|
|
|
648
|
+ continue
|
|
|
|
649
|
+ }
|
|
|
|
650
|
+ sql += FormatCondition(c)
|
|
|
|
651
|
+ sql += " and"
|
|
|
|
652
|
+ }
|
|
|
|
653
|
+ sql = strings.TrimSuffix(sql, "and")
|
|
|
|
654
|
+ return sql
|
|
|
|
655
|
+}
|
|
|
|
656
|
+
|
|
|
|
657
|
+func FormatCondition(c domain.ConditionExpr) string {
|
|
|
|
658
|
+ //if len(c.FieldRight.TableFields) > 0 {
|
|
|
|
659
|
+ // return fmt.Sprintf("%s %s %s", c.FieldRight.ExprSql, FormatOp(c.OperatorSymbol), FormatByOp(c.OperatorSymbol, c.FieldRight.ExprSql))
|
|
|
|
660
|
+ //}
|
|
|
|
661
|
+ //if domain.SQLType(c.FieldLeft.TableFields[0].FieldSQLType).IsString() {
|
|
|
|
662
|
+ // return fmt.Sprintf("%s %s '%s'", c.FieldRight.ExprSql, FormatOp(c.OperatorSymbol), FormatByOp(c.OperatorSymbol, c.FieldRight.ExprSql))
|
|
|
|
663
|
+ //}
|
|
|
|
664
|
+ return fmt.Sprintf("%s %s %s", c.FieldLeft.ExprSql, FormatOp(c.OperatorSymbol), FormatByOp(c.OperatorSymbol, c.FieldRight.ExprSql))
|
|
|
|
665
|
+}
|
|
|
|
666
|
+
|
|
|
|
667
|
+func FormatOp(op string) string {
|
|
|
|
668
|
+ return op
|
|
|
|
669
|
+}
|
|
|
|
670
|
+
|
|
|
|
671
|
+func FormatByOp(op string, exprSql string) string {
|
|
|
|
672
|
+ return exprSql
|
|
|
|
673
|
+}
|
|
|
|
674
|
+
|
|
554
|
func NewTableService(options map[string]interface{}) *TableService {
|
675
|
func NewTableService(options map[string]interface{}) *TableService {
|
|
555
|
newTableService := &TableService{}
|
676
|
newTableService := &TableService{}
|
|
556
|
return newTableService
|
677
|
return newTableService
|