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.

base_executor.go 12 kB

the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
the ability to automatically run unit tests after creating a pull request. (#764) * feat: add unit test workflow * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * feat:the ability to automatically run unit tests after creating a pull request. * Optimize/at build lock key performance (#837) * Refer to buildlockkey2 optimization #829 * Time complexity O(NM)-> O(NK) about buildlockkey and buildlockkey2 Increased readability #829 * update import sort #829 * update Encapsulation into util packages #829 * Support Update join (#761) * duplicate image row for update join * update join condition placeholder param error * update join bugfix * Open test annotations * recover update executor * recover update test * recover update test * modified version param --------- Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> --------- Co-authored-by: jimin <slievrly@163.com> Co-authored-by: JayLiu <38887641+luky116@users.noreply.github.com> Co-authored-by: FengZhang <zfcode@qq.com> Co-authored-by: Wiggins <125641755+MinatoWu@users.noreply.github.com> Co-authored-by: lxfeng1997 <33981743+lxfeng1997@users.noreply.github.com>
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 at
  18. import (
  19. "context"
  20. "database/sql"
  21. "database/sql/driver"
  22. "fmt"
  23. "strings"
  24. "github.com/arana-db/parser/ast"
  25. "github.com/arana-db/parser/model"
  26. "github.com/arana-db/parser/test_driver"
  27. gxsort "github.com/dubbogo/gost/sort"
  28. "github.com/pkg/errors"
  29. "seata.apache.org/seata-go/pkg/datasource/sql/exec"
  30. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  31. "seata.apache.org/seata-go/pkg/datasource/sql/undo"
  32. "seata.apache.org/seata-go/pkg/datasource/sql/util"
  33. "seata.apache.org/seata-go/pkg/util/reflectx"
  34. )
  35. type baseExecutor struct {
  36. hooks []exec.SQLHook
  37. }
  38. func (b *baseExecutor) beforeHooks(ctx context.Context, execCtx *types.ExecContext) {
  39. for _, hook := range b.hooks {
  40. hook.Before(ctx, execCtx)
  41. }
  42. }
  43. func (b *baseExecutor) afterHooks(ctx context.Context, execCtx *types.ExecContext) {
  44. for _, hook := range b.hooks {
  45. hook.After(ctx, execCtx)
  46. }
  47. }
  48. // GetScanSlice get the column type for scan
  49. // todo to use ColumnInfo get slice
  50. func (*baseExecutor) GetScanSlice(columnNames []string, tableMeta *types.TableMeta) []interface{} {
  51. scanSlice := make([]interface{}, 0, len(columnNames))
  52. for _, columnName := range columnNames {
  53. var (
  54. // get from metaData from this column
  55. columnMeta = tableMeta.Columns[columnName]
  56. )
  57. switch strings.ToUpper(columnMeta.DatabaseTypeString) {
  58. case "VARCHAR", "NVARCHAR", "VARCHAR2", "CHAR", "TEXT", "JSON", "TINYTEXT":
  59. var scanVal sql.NullString
  60. scanSlice = append(scanSlice, &scanVal)
  61. case "BIT", "INT", "LONGBLOB", "SMALLINT", "TINYINT", "BIGINT", "MEDIUMINT":
  62. if columnMeta.IsNullable == 0 {
  63. scanVal := int64(0)
  64. scanSlice = append(scanSlice, &scanVal)
  65. } else {
  66. scanVal := sql.NullInt64{}
  67. scanSlice = append(scanSlice, &scanVal)
  68. }
  69. case "DATE", "DATETIME", "TIME", "TIMESTAMP", "YEAR":
  70. var scanVal sql.NullTime
  71. scanSlice = append(scanSlice, &scanVal)
  72. case "DECIMAL", "DOUBLE", "FLOAT":
  73. if columnMeta.IsNullable == 0 {
  74. scanVal := float64(0)
  75. scanSlice = append(scanSlice, &scanVal)
  76. } else {
  77. scanVal := sql.NullFloat64{}
  78. scanSlice = append(scanSlice, &scanVal)
  79. }
  80. default:
  81. scanVal := sql.RawBytes{}
  82. scanSlice = append(scanSlice, &scanVal)
  83. }
  84. }
  85. return scanSlice
  86. }
  87. func (b *baseExecutor) buildSelectArgs(stmt *ast.SelectStmt, args []driver.NamedValue) []driver.NamedValue {
  88. var (
  89. selectArgsIndexs = make([]int32, 0)
  90. selectArgs = make([]driver.NamedValue, 0)
  91. )
  92. b.traversalArgs(stmt.From.TableRefs, &selectArgsIndexs)
  93. b.traversalArgs(stmt.Where, &selectArgsIndexs)
  94. if stmt.GroupBy != nil {
  95. for _, item := range stmt.GroupBy.Items {
  96. b.traversalArgs(item, &selectArgsIndexs)
  97. }
  98. }
  99. if stmt.OrderBy != nil {
  100. for _, item := range stmt.OrderBy.Items {
  101. b.traversalArgs(item, &selectArgsIndexs)
  102. }
  103. }
  104. if stmt.Limit != nil {
  105. if stmt.Limit.Offset != nil {
  106. b.traversalArgs(stmt.Limit.Offset, &selectArgsIndexs)
  107. }
  108. if stmt.Limit.Count != nil {
  109. b.traversalArgs(stmt.Limit.Count, &selectArgsIndexs)
  110. }
  111. }
  112. // sort selectArgs index array
  113. gxsort.Int32(selectArgsIndexs)
  114. for _, index := range selectArgsIndexs {
  115. selectArgs = append(selectArgs, args[index])
  116. }
  117. return selectArgs
  118. }
  119. // todo perfect all sql operation
  120. func (b *baseExecutor) traversalArgs(node ast.Node, argsIndex *[]int32) {
  121. if node == nil {
  122. return
  123. }
  124. switch node.(type) {
  125. case *ast.BinaryOperationExpr:
  126. expr := node.(*ast.BinaryOperationExpr)
  127. b.traversalArgs(expr.L, argsIndex)
  128. b.traversalArgs(expr.R, argsIndex)
  129. break
  130. case *ast.BetweenExpr:
  131. expr := node.(*ast.BetweenExpr)
  132. b.traversalArgs(expr.Left, argsIndex)
  133. b.traversalArgs(expr.Right, argsIndex)
  134. break
  135. case *ast.PatternInExpr:
  136. exprs := node.(*ast.PatternInExpr).List
  137. for i := 0; i < len(exprs); i++ {
  138. b.traversalArgs(exprs[i], argsIndex)
  139. }
  140. break
  141. case *ast.Join:
  142. exprs := node.(*ast.Join)
  143. b.traversalArgs(exprs.Left, argsIndex)
  144. if exprs.Right != nil {
  145. b.traversalArgs(exprs.Right, argsIndex)
  146. }
  147. if exprs.On != nil {
  148. b.traversalArgs(exprs.On.Expr, argsIndex)
  149. }
  150. break
  151. case *test_driver.ParamMarkerExpr:
  152. *argsIndex = append(*argsIndex, int32(node.(*test_driver.ParamMarkerExpr).Order))
  153. break
  154. }
  155. }
  156. func (b *baseExecutor) buildRecordImages(rowsi driver.Rows, tableMetaData *types.TableMeta, sqlType types.SQLType) (*types.RecordImage, error) {
  157. // select column names
  158. columnNames := rowsi.Columns()
  159. rowImages := make([]types.RowImage, 0)
  160. sqlRows := util.NewScanRows(rowsi)
  161. for sqlRows.Next() {
  162. ss := b.GetScanSlice(columnNames, tableMetaData)
  163. err := sqlRows.Scan(ss...)
  164. if err != nil {
  165. return nil, err
  166. }
  167. columns := make([]types.ColumnImage, 0)
  168. // build record image
  169. for i, name := range columnNames {
  170. columnMeta := tableMetaData.Columns[name]
  171. keyType := types.IndexTypeNull
  172. if _, ok := tableMetaData.GetPrimaryKeyMap()[name]; ok {
  173. keyType = types.IndexTypePrimaryKey
  174. }
  175. jdbcType := types.MySQLStrToJavaType(columnMeta.DatabaseTypeString)
  176. columns = append(columns, types.ColumnImage{
  177. KeyType: keyType,
  178. ColumnName: name,
  179. ColumnType: jdbcType,
  180. Value: getSqlNullValue(reflectx.GetElemDataValue(ss[i])),
  181. })
  182. }
  183. rowImages = append(rowImages, types.RowImage{Columns: columns})
  184. }
  185. return &types.RecordImage{TableName: tableMetaData.TableName, Rows: rowImages, SQLType: sqlType}, nil
  186. }
  187. func (b *baseExecutor) getNeedColumns(meta *types.TableMeta, columns []string, dbType types.DBType) []string {
  188. var needUpdateColumns []string
  189. if undo.UndoConfig.OnlyCareUpdateColumns && columns != nil && len(columns) > 0 {
  190. needUpdateColumns = columns
  191. if !b.containsPKByName(meta, columns) {
  192. pkNames := meta.GetPrimaryKeyOnlyName()
  193. if pkNames != nil && len(pkNames) > 0 {
  194. for _, name := range pkNames {
  195. needUpdateColumns = append(needUpdateColumns, name)
  196. }
  197. }
  198. }
  199. // todo If it contains onUpdate columns, add onUpdate columns
  200. } else {
  201. needUpdateColumns = meta.ColumnNames
  202. }
  203. for i := range needUpdateColumns {
  204. needUpdateColumns[i] = AddEscape(needUpdateColumns[i], dbType)
  205. }
  206. return needUpdateColumns
  207. }
  208. func (b *baseExecutor) containsPKByName(meta *types.TableMeta, columns []string) bool {
  209. pkColumnNameList := meta.GetPrimaryKeyOnlyName()
  210. if len(pkColumnNameList) == 0 {
  211. return false
  212. }
  213. matchCounter := 0
  214. for _, column := range columns {
  215. for _, pkName := range pkColumnNameList {
  216. if strings.EqualFold(pkName, column) ||
  217. strings.EqualFold(pkName, strings.ToLower(column)) {
  218. matchCounter++
  219. }
  220. }
  221. }
  222. return matchCounter == len(pkColumnNameList)
  223. }
  224. func (u *baseExecutor) buildSelectFields(ctx context.Context, tableMeta *types.TableMeta, tableAliases string, inUseFields []*ast.Assignment) ([]*ast.SelectField, error) {
  225. fields := make([]*ast.SelectField, 0, len(inUseFields))
  226. tableName := tableAliases
  227. if tableAliases == "" {
  228. tableName = tableMeta.TableName
  229. }
  230. if undo.UndoConfig.OnlyCareUpdateColumns {
  231. for _, column := range inUseFields {
  232. tn := column.Column.Table.O
  233. if tn != "" && tn != tableName {
  234. continue
  235. }
  236. fields = append(fields, &ast.SelectField{
  237. Expr: &ast.ColumnNameExpr{
  238. Name: column.Column,
  239. },
  240. })
  241. }
  242. if len(fields) == 0 {
  243. return fields, nil
  244. }
  245. // select indexes columns
  246. for _, columnName := range tableMeta.GetPrimaryKeyOnlyName() {
  247. fields = append(fields, &ast.SelectField{
  248. Expr: &ast.ColumnNameExpr{
  249. Name: &ast.ColumnName{
  250. Table: model.CIStr{
  251. O: tableName,
  252. L: tableName,
  253. },
  254. Name: model.CIStr{
  255. O: columnName,
  256. L: columnName,
  257. },
  258. },
  259. },
  260. })
  261. }
  262. } else {
  263. fields = append(fields, &ast.SelectField{
  264. Expr: &ast.ColumnNameExpr{
  265. Name: &ast.ColumnName{
  266. Name: model.CIStr{
  267. O: "*",
  268. L: "*",
  269. },
  270. },
  271. },
  272. })
  273. }
  274. return fields, nil
  275. }
  276. func getSqlNullValue(value interface{}) interface{} {
  277. if value == nil {
  278. return nil
  279. }
  280. if v, ok := value.(sql.NullString); ok {
  281. if v.Valid {
  282. return v.String
  283. }
  284. return nil
  285. }
  286. if v, ok := value.(sql.NullFloat64); ok {
  287. if v.Valid {
  288. return v.Float64
  289. }
  290. return nil
  291. }
  292. if v, ok := value.(sql.NullBool); ok {
  293. if v.Valid {
  294. return v.Bool
  295. }
  296. return nil
  297. }
  298. if v, ok := value.(sql.NullTime); ok {
  299. if v.Valid {
  300. return v.Time
  301. }
  302. return nil
  303. }
  304. if v, ok := value.(sql.NullByte); ok {
  305. if v.Valid {
  306. return v.Byte
  307. }
  308. return nil
  309. }
  310. if v, ok := value.(sql.NullInt16); ok {
  311. if v.Valid {
  312. return v.Int16
  313. }
  314. return nil
  315. }
  316. if v, ok := value.(sql.NullInt32); ok {
  317. if v.Valid {
  318. return v.Int32
  319. }
  320. return nil
  321. }
  322. if v, ok := value.(sql.NullInt64); ok {
  323. if v.Valid {
  324. return v.Int64
  325. }
  326. return nil
  327. }
  328. return value
  329. }
  330. // buildWhereConditionByPKs build where condition by primary keys
  331. // each pk is a condition.the result will like :" (id,userCode) in ((?,?),(?,?)) or (id,userCode) in ((?,?),(?,?) ) or (id,userCode) in ((?,?))"
  332. func (b *baseExecutor) buildWhereConditionByPKs(pkNameList []string, rowSize int, dbType string, maxInSize int) string {
  333. var (
  334. whereStr = &strings.Builder{}
  335. batchSize = rowSize/maxInSize + 1
  336. )
  337. if rowSize%maxInSize == 0 {
  338. batchSize = rowSize / maxInSize
  339. }
  340. for batch := 0; batch < batchSize; batch++ {
  341. if batch > 0 {
  342. whereStr.WriteString(" OR ")
  343. }
  344. whereStr.WriteString("(")
  345. for i := 0; i < len(pkNameList); i++ {
  346. if i > 0 {
  347. whereStr.WriteString(",")
  348. }
  349. // todo add escape
  350. whereStr.WriteString(fmt.Sprintf("`%s`", pkNameList[i]))
  351. }
  352. whereStr.WriteString(") IN (")
  353. var eachSize int
  354. if batch == batchSize-1 {
  355. if rowSize%maxInSize == 0 {
  356. eachSize = maxInSize
  357. } else {
  358. eachSize = rowSize % maxInSize
  359. }
  360. } else {
  361. eachSize = maxInSize
  362. }
  363. for i := 0; i < eachSize; i++ {
  364. if i > 0 {
  365. whereStr.WriteString(",")
  366. }
  367. whereStr.WriteString("(")
  368. for j := 0; j < len(pkNameList); j++ {
  369. if j > 0 {
  370. whereStr.WriteString(",")
  371. }
  372. whereStr.WriteString("?")
  373. }
  374. whereStr.WriteString(")")
  375. }
  376. whereStr.WriteString(")")
  377. }
  378. return whereStr.String()
  379. }
  380. func (b *baseExecutor) buildPKParams(rows []types.RowImage, pkNameList []string) []driver.NamedValue {
  381. params := make([]driver.NamedValue, 0)
  382. for _, row := range rows {
  383. coumnMap := row.GetColumnMap()
  384. for i, pk := range pkNameList {
  385. if col, ok := coumnMap[pk]; ok {
  386. params = append(params, driver.NamedValue{
  387. Ordinal: i, Value: col.Value,
  388. })
  389. }
  390. }
  391. }
  392. return params
  393. }
  394. // the string as local key. the local key example(multi pk): "t_user:1_a,2_b"
  395. func (b *baseExecutor) buildLockKey(records *types.RecordImage, meta types.TableMeta) string {
  396. return util.BuildLockKey(records, meta)
  397. }
  398. func (b *baseExecutor) rowsPrepare(ctx context.Context, conn driver.Conn, selectSQL string, selectArgs []driver.NamedValue) (driver.Rows, error) {
  399. var queryer driver.Queryer
  400. queryerContext, ok := conn.(driver.QueryerContext)
  401. if !ok {
  402. queryer, ok = conn.(driver.Queryer)
  403. }
  404. if ok {
  405. var err error
  406. rows, err = util.CtxDriverQuery(ctx, queryerContext, queryer, selectSQL, selectArgs)
  407. if err != nil {
  408. return nil, err
  409. }
  410. } else {
  411. return nil, errors.New("target conn should been driver.QueryerContext or driver.Queryer")
  412. }
  413. return rows, nil
  414. }