[impr]Update some method from RETURN_GENERATED_KEYS replace to String[]{"id"},Avoid database compatibility issues (#9515)

This commit is contained in:
无处不在 2022-11-10 09:42:38 +08:00 committed by GitHub
parent 3f1b96b15a
commit ec7ba18792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 9 deletions

View File

@ -37,7 +37,6 @@ import javax.annotation.PostConstruct;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -122,14 +121,15 @@ public class GroupCapacityPersistService {
// Note: add "tenant_id = ''" condition. // Note: add "tenant_id = ''" condition.
sql = groupCapacityMapper.insertIntoSelectByWhere(); sql = groupCapacityMapper.insertIntoSelectByWhere();
} }
return insertGroupCapacity(sql, capacity); String[] primaryKeyGeneratedKeys = groupCapacityMapper.getPrimaryKeyGeneratedKeys();
return insertGroupCapacity(sql, capacity, primaryKeyGeneratedKeys);
} }
private boolean insertGroupCapacity(final String sql, final GroupCapacity capacity) { private boolean insertGroupCapacity(final String sql, final GroupCapacity capacity, String[] primaryKeyGeneratedKeys) {
try { try {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder(); GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = connection -> { PreparedStatementCreator preparedStatementCreator = connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); PreparedStatement ps = connection.prepareStatement(sql, primaryKeyGeneratedKeys);
String group = capacity.getGroup(); String group = capacity.getGroup();
ps.setString(1, group); ps.setString(1, group);
ps.setInt(2, capacity.getQuota()); ps.setInt(2, capacity.getQuota());

View File

@ -35,7 +35,6 @@ import javax.annotation.PostConstruct;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -110,10 +109,11 @@ public class TenantCapacityPersistService {
TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.TENANT_CAPACITY); TableConstant.TENANT_CAPACITY);
final String sql = tenantCapacityMapper.insertTenantCapacity(); final String sql = tenantCapacityMapper.insertTenantCapacity();
String[] primaryKeyGeneratedKeys = tenantCapacityMapper.getPrimaryKeyGeneratedKeys();
try { try {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder(); GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = connection -> { PreparedStatementCreator preparedStatementCreator = connection -> {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); PreparedStatement ps = connection.prepareStatement(sql, primaryKeyGeneratedKeys);
String tenant = tenantCapacity.getTenant(); String tenant = tenantCapacity.getTenant();
ps.setString(1, tenant); ps.setString(1, tenant);
ps.setInt(2, tenantCapacity.getQuota()); ps.setInt(2, tenantCapacity.getQuota());

View File

@ -76,7 +76,6 @@ import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -1675,12 +1674,12 @@ public class ExternalStoragePersistServiceImpl implements PersistService {
Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", "src_user", Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", "src_user",
"gmt_create", "gmt_modified", "c_desc", "c_use", "effect", "type", "c_schema", "gmt_create", "gmt_modified", "c_desc", "c_use", "effect", "type", "c_schema",
"encrypted_data_key")); "encrypted_data_key"));
String[] returnGeneratedKeys = configInfoMapper.getPrimaryKeyGeneratedKeys();
try { try {
jt.update(new PreparedStatementCreator() { jt.update(new PreparedStatementCreator() {
@Override @Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); PreparedStatement ps = connection.prepareStatement(sql, returnGeneratedKeys);
ps.setString(1, configInfo.getDataId()); ps.setString(1, configInfo.getDataId());
ps.setString(2, configInfo.getGroup()); ps.setString(2, configInfo.getGroup());
ps.setString(3, tenantTmp); ps.setString(3, tenantTmp);

View File

@ -152,4 +152,9 @@ public abstract class AbstractMapper implements Mapper {
} }
return sql.toString(); return sql.toString();
} }
@Override
public String[] getPrimaryKeyGeneratedKeys() {
return new String[]{"id"};
}
} }

View File

@ -75,4 +75,12 @@ public interface Mapper {
* @return The name of datasource. * @return The name of datasource.
*/ */
String getDataSource(); String getDataSource();
/**
* Get config_info table primary keys name.
* The old default value: Statement.RETURN_GENERATED_KEYS
* The new default value: new String[]{"id"}
* @return an array of column names indicating the columns
*/
String[] getPrimaryKeyGeneratedKeys();
} }