You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package executor
  18. import (
  19. "fmt"
  20. "strings"
  21. "seata.apache.org/seata-go/pkg/datasource/sql/datasource"
  22. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  23. "seata.apache.org/seata-go/pkg/util/log"
  24. )
  25. // IsRecordsEquals check before record and after record if equal
  26. func IsRecordsEquals(beforeImage *types.RecordImage, afterImage *types.RecordImage) (bool, error) {
  27. if beforeImage == nil && afterImage == nil {
  28. return true, nil
  29. }
  30. if beforeImage == nil || afterImage == nil {
  31. return false, nil
  32. }
  33. if !strings.EqualFold(beforeImage.TableName, afterImage.TableName) || len(beforeImage.Rows) != len(afterImage.Rows) {
  34. return false, nil
  35. }
  36. if len(beforeImage.Rows) == 0 {
  37. return true, nil
  38. }
  39. return compareRows(*beforeImage.TableMeta, beforeImage.Rows, afterImage.Rows)
  40. }
  41. func compareRows(tableMeta types.TableMeta, oldRows []types.RowImage, newRows []types.RowImage) (bool, error) {
  42. oldRowMap := rowListToMap(oldRows, tableMeta.GetPrimaryKeyOnlyName())
  43. newRowMap := rowListToMap(newRows, tableMeta.GetPrimaryKeyOnlyName())
  44. for key, oldRow := range oldRowMap {
  45. newRow := newRowMap[key]
  46. if newRow == nil {
  47. log.Infof("compare row failed, rowKey %s, reason new field is null", key)
  48. return false, nil
  49. }
  50. for fieldName, oldValue := range oldRow {
  51. newValue := newRow[fieldName]
  52. if !datasource.DeepEqual(newValue, oldValue) {
  53. log.Infof("compare row failed, rowKey %s, fieldName %s, oldValue %v, newValud %v", key, fieldName, oldValue, newValue)
  54. return false, nil
  55. }
  56. }
  57. }
  58. return true, nil
  59. }
  60. func rowListToMap(rows []types.RowImage, primaryKeyList []string) map[string]map[string]interface{} {
  61. rowMap := make(map[string]map[string]interface{}, 0)
  62. for _, row := range rows {
  63. fieldMap := make(map[string]interface{}, 0)
  64. var rowKey string
  65. var firstUnderline bool
  66. for _, column := range row.Columns {
  67. for i, key := range primaryKeyList {
  68. if column.ColumnName == key {
  69. if firstUnderline && i > 0 {
  70. rowKey += "_##$$_"
  71. }
  72. // todo make value more accurate
  73. rowKey = fmt.Sprintf("%v%v", rowKey, column.GetActualValue())
  74. firstUnderline = true
  75. }
  76. }
  77. fieldMap[strings.ToUpper(column.ColumnName)] = column.Value
  78. }
  79. rowMap[rowKey] = fieldMap
  80. }
  81. return rowMap
  82. }
  83. // buildWhereConditionByPKs build where condition by primary keys
  84. // each pk is a condition.the result will like :" (id,userCode) in ((?,?),(?,?)) or (id,userCode) in ((?,?),(?,?) ) or (id,userCode) in ((?,?))"
  85. func buildWhereConditionByPKs(pkNameList []string, rowSize int, maxInSize int) string {
  86. var (
  87. whereStr = &strings.Builder{}
  88. batchSize = rowSize/maxInSize + 1
  89. )
  90. if rowSize%maxInSize == 0 {
  91. batchSize = rowSize / maxInSize
  92. }
  93. for batch := 0; batch < batchSize; batch++ {
  94. if batch > 0 {
  95. whereStr.WriteString(" OR ")
  96. }
  97. whereStr.WriteString("(")
  98. for i := 0; i < len(pkNameList); i++ {
  99. if i > 0 {
  100. whereStr.WriteString(",")
  101. }
  102. // todo add escape
  103. whereStr.WriteString(fmt.Sprintf("`%s`", pkNameList[i]))
  104. }
  105. whereStr.WriteString(") IN (")
  106. var eachSize int
  107. if batch == batchSize-1 {
  108. if rowSize%maxInSize == 0 {
  109. eachSize = maxInSize
  110. } else {
  111. eachSize = rowSize % maxInSize
  112. }
  113. } else {
  114. eachSize = maxInSize
  115. }
  116. for i := 0; i < eachSize; i++ {
  117. if i > 0 {
  118. whereStr.WriteString(",")
  119. }
  120. whereStr.WriteString("(")
  121. for j := 0; j < len(pkNameList); j++ {
  122. if j > 0 {
  123. whereStr.WriteString(",")
  124. }
  125. whereStr.WriteString("?")
  126. }
  127. whereStr.WriteString(")")
  128. }
  129. whereStr.WriteString(")")
  130. }
  131. return whereStr.String()
  132. }
  133. func buildPKParams(rows []types.RowImage, pkNameList []string) []interface{} {
  134. params := make([]interface{}, 0)
  135. for _, row := range rows {
  136. coumnMap := row.GetColumnMap()
  137. for _, pk := range pkNameList {
  138. col := coumnMap[pk]
  139. if col != nil {
  140. params = append(params, col.Value)
  141. }
  142. }
  143. }
  144. return params
  145. }