From 9a78d8a46616ff7cf57f36e336db00abb6197056 Mon Sep 17 00:00:00 2001 From: lxfeng1997 <824141436@qq.com> Date: Fri, 29 Nov 2024 16:16:08 +0800 Subject: [PATCH] only inserted fields --- pkg/datasource/sql/exec/at/insert_executor.go | 7 ++++-- .../builder/mysql_insert_undo_log_builder.go | 7 ++++-- .../mysql_multi_update_undo_log_builder.go | 22 +++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/pkg/datasource/sql/exec/at/insert_executor.go b/pkg/datasource/sql/exec/at/insert_executor.go index a05da5062..d1c7b4cee 100644 --- a/pkg/datasource/sql/exec/at/insert_executor.go +++ b/pkg/datasource/sql/exec/at/insert_executor.go @@ -184,9 +184,12 @@ func (i *insertExecutor) buildAfterImageSQL(ctx context.Context) (string, []driv } // build check sql sb := strings.Builder{} - sb.WriteString("SELECT * FROM " + tableName) + suffix := strings.Builder{} + sb.WriteString("SELECT " + strings.Join(pkColumnNameList, ", ")) + suffix.WriteString(" FROM " + tableName) whereSQL := i.buildWhereConditionByPKs(pkColumnNameList, len(pkValuesMap[pkColumnNameList[0]]), "mysql", maxInSize) - sb.WriteString(" WHERE " + whereSQL + " ") + suffix.WriteString(" WHERE " + whereSQL + " ") + sb.WriteString(suffix.String()) return sb.String(), i.buildPKParams(pkRowImages, pkColumnNameList), nil } diff --git a/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go b/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go index c837579bd..6144e66b6 100644 --- a/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go +++ b/pkg/datasource/sql/undo/builder/mysql_insert_undo_log_builder.go @@ -132,9 +132,12 @@ func (u *MySQLInsertUndoLogBuilder) buildAfterImageSQL(ctx context.Context, exec } // build check sql sb := strings.Builder{} - sb.WriteString("SELECT * FROM " + tableName) + suffix := strings.Builder{} + sb.WriteString("SELECT " + strings.Join(pkColumnNameList, ", ")) + suffix.WriteString(" FROM " + tableName) whereSQL := u.buildWhereConditionByPKs(pkColumnNameList, len(pkValuesMap[pkColumnNameList[0]]), "mysql", maxInSize) - sb.WriteString(" WHERE " + whereSQL + " ") + suffix.WriteString(" WHERE " + whereSQL + " ") + sb.WriteString(suffix.String()) return sb.String(), u.buildPKParams(pkRowImages, pkColumnNameList), nil } diff --git a/pkg/datasource/sql/undo/builder/mysql_multi_update_undo_log_builder.go b/pkg/datasource/sql/undo/builder/mysql_multi_update_undo_log_builder.go index 57b9f6ca5..0c15af393 100644 --- a/pkg/datasource/sql/undo/builder/mysql_multi_update_undo_log_builder.go +++ b/pkg/datasource/sql/undo/builder/mysql_multi_update_undo_log_builder.go @@ -138,8 +138,26 @@ func (u *MySQLMultiUpdateUndoLogBuilder) AfterImage(ctx context.Context, execCtx func (u *MySQLMultiUpdateUndoLogBuilder) buildAfterImageSQL(beforeImage *types.RecordImage, meta types.TableMeta) (string, []driver.Value) { sb := strings.Builder{} - // todo use ONLY_CARE_UPDATE_COLUMNS to judge select all columns or not - sb.WriteString("SELECT * FROM " + meta.TableName + " ") + var selectFieldsStr string + selectFields := make([]string, 0, len(meta.ColumnNames)) + var fieldsExits = make(map[string]struct{}) + if undo.UndoConfig.OnlyCareUpdateColumns { + for _, row := range beforeImage.Rows { + for _, column := range row.Columns { + if _, exist := fieldsExits[column.ColumnName]; exist { + continue + } + + fieldsExits[column.ColumnName] = struct{}{} + selectFields = append(selectFields, column.ColumnName) + } + } + selectFieldsStr = strings.Join(selectFields, ",") + } else { + selectFieldsStr = strings.Join(meta.ColumnNames, ",") + } + + sb.WriteString("SELECT " + selectFieldsStr + " FROM " + meta.TableName + " ") whereSQL := u.buildWhereConditionByPKs(meta.GetPrimaryKeyOnlyName(), len(beforeImage.Rows), "mysql", maxInSize) sb.WriteString(" " + whereSQL + " ") return sb.String(), u.buildPKParams(beforeImage.Rows, meta.GetPrimaryKeyOnlyName())