Skip to content

Commit

Permalink
bugfix: convert to utf8mb4 if mysql column is json type (#6232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bughue authored Jan 16, 2024
1 parent c746361 commit 82b036f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6143](https://github.com/apache/incubator-seata/pull/6143)] gracefully shut down the server
- [[#6204](https://github.com/apache/incubator-seata/pull/6204)] fix the problem that The incorrect configuration needs to be fixed
- [[#6248](https://github.com/apache/incubator-seata/pull/6248)] fix JDBC resultSet, statement, connection closing order
- [[#6248](https://github.com/apache/incubator-seata/pull/6256)] fix raft-discovery cannot read registry configuration for seata-all sdk
- [[#6256](https://github.com/apache/incubator-seata/pull/6256)] fix raft-discovery cannot read registry configuration for seata-all sdk
- [[#6232](https://github.com/apache/incubator-seata/pull/6232)] convert to utf8mb4 if mysql column is json type


### optimize:
Expand Down
3 changes: 2 additions & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
- [[#6143](https://github.com/apache/incubator-seata/pull/6143)] 修复优雅停机
- [[#6204](https://github.com/apache/incubator-seata/pull/6204)] 修复错误配置问题
- [[#6248](https://github.com/apache/incubator-seata/pull/6248)] 修复JDBC resultSet, statement, connection关闭顺序
- [[#6248](https://github.com/apache/incubator-seata/pull/6256)] 修复在seata-all sdk下,raft-discovery模块不能读取registry.conf的配置的问题
- [[#6256](https://github.com/apache/incubator-seata/pull/6256)] 修复在seata-all sdk下,raft-discovery模块不能读取registry.conf的配置的问题
- [[#6232](https://github.com/apache/incubator-seata/pull/6232)] 修复在mysql的json类型下出现Cannot create a JSON value from a string with CHARACTER SET 'binary'问题

### optimize:
- [[#6031](https://github.com/apache/incubator-seata/pull/6031)] 添加undo_log表的存在性校验
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.rm.datasource.undo.mysql;

import io.seata.rm.datasource.sql.struct.Field;
import io.seata.sqlparser.struct.ColumnMeta;
import io.seata.sqlparser.struct.TableMeta;

import java.sql.Types;

/**
* the type MySQLJsonHelper
**/
public class MySQLJsonHelper {
public static String convertIfJson(Field field, TableMeta tableMeta) {
ColumnMeta columnMeta = tableMeta.getColumnMeta(field.getName());
if (columnMeta != null && columnMeta.getDataType() == Types.OTHER
&& "JSON".equals(columnMeta.getDataTypeName())) {
return "CONVERT(? USING utf8mb4)";
}
return "?";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected String buildUndoSQL() {
String insertColumns = fields.stream()
.map(field -> ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL))
.collect(Collectors.joining(", "));
String insertValues = fields.stream().map(field -> "?")
String insertValues = fields.stream().map(field -> MySQLJsonHelper.convertIfJson(field, beforeImage.getTableMeta()))
.collect(Collectors.joining(", "));

return String.format(INSERT_SQL_TEMPLATE, sqlUndoLog.getTableName(), insertColumns, insertValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ protected String buildUndoSQL() {
// update sql undo log before image all field come from table meta. need add escape.
// see BaseTransactionalExecutor#buildTableRecords
String updateColumns = nonPkFields.stream().map(
field -> ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL) + " = ?").collect(
Collectors.joining(", "));
field -> {
String addEscape = ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL);
return addEscape + " = " + MySQLJsonHelper.convertIfJson(field, beforeImage.getTableMeta());
})
.collect(Collectors.joining(", "));

List<String> pkNameList = getOrderedPkList(beforeImage, row, JdbcConstants.MYSQL).stream().map(e -> e.getName())
.collect(Collectors.toList());
Expand Down

0 comments on commit 82b036f

Please sign in to comment.