http://jira.mfd.msk:8088/browse/CLS-754 мелкий рефакторинг

This commit is contained in:
AKurakin 2024-09-12 19:09:54 +03:00
parent 0ae83b1c52
commit 20aef32798
2 changed files with 23 additions and 25 deletions

View file

@ -7,7 +7,6 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.sql.Types; import java.sql.Types;
import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
@ -39,7 +38,6 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
private final static int BATCH_SIZE = 1000; private final static int BATCH_SIZE = 1000;
protected final Logger log = LoggerFactory.getLogger(this.getClass()); protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected final JdbcTemplate jdbcTemplate; protected final JdbcTemplate jdbcTemplate;
protected SimpleDateFormat DB_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
protected Map<String, ColumnMetaData> columnMetaDataMap; protected Map<String, ColumnMetaData> columnMetaDataMap;
protected SimpleObjectMapStore(JdbcTemplate jdbcTemplate) { protected SimpleObjectMapStore(JdbcTemplate jdbcTemplate) {
@ -160,7 +158,7 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
} }
protected void batchInsertUpdate(@NonNull String insertStatement, @NonNull List<Object[]> args) { protected void batchInsertUpdate(@NonNull String insertStatement, @NonNull List<Object[]> args) {
updateMetaData(); readMetaData();
try { try {
int[][] ret = jdbcTemplate.batchUpdate(insertStatement, args, BATCH_SIZE, (ps, params) -> { int[][] ret = jdbcTemplate.batchUpdate(insertStatement, args, BATCH_SIZE, (ps, params) -> {
for (int i = 0; i < params.length; i++) { for (int i = 0; i < params.length; i++) {
@ -181,10 +179,11 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
} }
} }
Object normalizeValue(Object value, ParameterMetaData metaData, Map<String, ColumnMetaData> columnMetaDataMap, int i, String insertStatement, Object[] params) throws SQLException { Object normalizeValue(Object value, ParameterMetaData metaData, Map<String, ColumnMetaData> columnMetaDataMap, int i,
String insertStatement, Object[] params) throws SQLException {
if (value == null) if (value == null)
return null; return null;
i++; // нумерация в ParameterMetaData с 1 i++; // нумерация в ParameterMetaData начинается с 1
final int type = metaData.getParameterType(i); final int type = metaData.getParameterType(i);
final String fieldName = getFields()[i - 1].toLowerCase(); final String fieldName = getFields()[i - 1].toLowerCase();
@ -196,24 +195,24 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
if (columnMetaDataMap == null) { if (columnMetaDataMap == null) {
length = metaData.getPrecision(i + 1); length = metaData.getPrecision(i + 1);
} else { } else {
// в postgresql для String ParameterMetaData.getPrecision возвращает 0, по этому используется другой способ через columnMetaDataMap. // в postgresql для String ParameterMetaData.getPrecision возвращает 0, используется columnMetaDataMap
length = columnMetaDataMap.get(fieldName).getPrecision(); length = columnMetaDataMap.get(fieldName).getPrecision();
} }
if (length > 0 && stringValue.length() > length) { if (length > 0 && stringValue.length() > length) {
value = stringValue.substring(0, length); value = stringValue.substring(0, length);
log.error("String value for insert is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]", log.error("String value is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]",
insertStatement, Arrays.toString(params), i, stringValue, value); insertStatement, Arrays.toString(params), i, stringValue, value);
} }
} else if ((type == Types.DECIMAL || type == Types.NUMERIC || type == Types.BIGINT || type == Types.INTEGER) && } else if ((type == Types.DECIMAL || type == Types.NUMERIC || type == Types.BIGINT || type == Types.INTEGER) &&
(value instanceof BigDecimal || value instanceof Long || value instanceof Integer)) { (value instanceof BigDecimal || value instanceof Long || value instanceof Integer)) {
if (value instanceof Integer && (type == Types.INTEGER || type == Types.BIGINT)) { if (value instanceof Integer && (type == Types.INTEGER || type == Types.BIGINT)) {
return value; // value ok return value; // ok
} }
if (value instanceof Long vl) { if (value instanceof Long vl) {
if (type == Types.BIGINT) { if (type == Types.BIGINT) {
return value; // value ok return value; // ok
} }
if (type == Types.INTEGER) { if (type == Types.INTEGER) {
if (vl > Integer.MAX_VALUE) { if (vl > Integer.MAX_VALUE) {
@ -242,12 +241,12 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
if (bigDecimalValue.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) { if (bigDecimalValue.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
Object oldValue = value; Object oldValue = value;
value = Long.MAX_VALUE; value = Long.MAX_VALUE;
log.error("Double value for insert as BIGINT is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]", log.error("Double value as BIGINT is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]",
insertStatement, Arrays.toString(params), i, oldValue, value); insertStatement, Arrays.toString(params), i, oldValue, value);
} else if (bigDecimalValue.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) { } else if (bigDecimalValue.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) {
Object oldValue = value; Object oldValue = value;
value = Long.MIN_VALUE; value = Long.MIN_VALUE;
log.error("Double value for insert as BIGINT is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]", log.error("Double value as BIGINT is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]",
insertStatement, Arrays.toString(params), i, oldValue, value); insertStatement, Arrays.toString(params), i, oldValue, value);
} }
return value; // value ok return value; // value ok
@ -256,12 +255,12 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
if (bigDecimalValue.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) { if (bigDecimalValue.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) {
Object oldValue = value; Object oldValue = value;
value = Integer.MAX_VALUE; value = Integer.MAX_VALUE;
log.error("Double value for insert as INTEGER is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]", log.error("Double value as INTEGER is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]",
insertStatement, Arrays.toString(params), i, oldValue, value); insertStatement, Arrays.toString(params), i, oldValue, value);
} else if (bigDecimalValue.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) { } else if (bigDecimalValue.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) {
Object oldValue = value; Object oldValue = value;
value = Integer.MIN_VALUE; value = Integer.MIN_VALUE;
log.error("Double value for insert as INTEGER is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]", log.error("Double value as INTEGER is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]",
insertStatement, Arrays.toString(params), i, oldValue, value); insertStatement, Arrays.toString(params), i, oldValue, value);
} }
return value; // value ok return value; // value ok
@ -280,7 +279,7 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
value = new BigDecimal(BigDecimal.valueOf(Math.pow(10, precision - dbScalePartLength)).longValue() - 1); value = new BigDecimal(BigDecimal.valueOf(Math.pow(10, precision - dbScalePartLength)).longValue() - 1);
if (negative) if (negative)
value = ((BigDecimal) value).negate(); value = ((BigDecimal) value).negate();
log.error("BigDecimal value for insert is too large [ insertSql: {}, params: {}, i:{}, incorrect value : {}, normalized value: {} ]", log.error("BigDecimal value is too large [insertSql: {}, params: {}, i: {}, incorrect value: {}, normalized value: {}]",
insertStatement, Arrays.toString(params), i, bigDecimalValue, value); insertStatement, Arrays.toString(params), i, bigDecimalValue, value);
} }
} }
@ -311,7 +310,7 @@ public abstract class SimpleObjectMapStore<T extends SpcexObjectBase> implements
return TextUtil.format(INSERT_TEMPLATE, params); return TextUtil.format(INSERT_TEMPLATE, params);
} }
private void updateMetaData() { private void readMetaData() {
if (columnMetaDataMap == null) synchronized (this) { if (columnMetaDataMap == null) synchronized (this) {
columnMetaDataMap = PostgresColumnTypeUtil.extractTableMetadata(jdbcTemplate, getTableName()); columnMetaDataMap = PostgresColumnTypeUtil.extractTableMetadata(jdbcTemplate, getTableName());
} }

View file

@ -10,20 +10,19 @@ import org.springframework.jdbc.core.JdbcTemplate;
public final class PostgresColumnTypeUtil { public final class PostgresColumnTypeUtil {
static Logger log = LoggerFactory.getLogger(PostgresColumnTypeUtil.class); static Logger log = LoggerFactory.getLogger(PostgresColumnTypeUtil.class);
static String SELECT_METADATA_CASE_SENS = "SELECT * FROM information_schema.columns WHERE table_name=?"; static String SELECT_METADATA_LIKE = "SELECT * FROM information_schema.columns WHERE table_name=?";
static String SELECT_METADATA_CASE_INSENS = "SELECT * FROM information_schema.columns WHERE table_name ilike ?"; static String SELECT_METADATA_ILIKE = "SELECT * FROM information_schema.columns WHERE table_name ilike ?";
public static Map<String, ColumnMetaData> extractTableMetadata(JdbcTemplate template, String tableName) { public static Map<String, ColumnMetaData> extractTableMetadata(JdbcTemplate template, String tableName) {
List<ColumnMetaData> columnMetaDataList = template.query(tableName.contains("\"") ? SELECT_METADATA_CASE_SENS List<ColumnMetaData> columnMetaDataList = template.query(tableName.contains("\"") ? SELECT_METADATA_LIKE
: SELECT_METADATA_CASE_INSENS, (rs, rn) -> new ColumnMetaData( : SELECT_METADATA_ILIKE, (rs, rn) -> new ColumnMetaData(
rs.getString("column_name"), rs.getString("column_name"),
rs.getString("data_type"), rs.getString("data_type"),
rs.getInt("character_maximum_length"), rs.getInt("character_maximum_length"),
rs.getInt("numeric_precision"), rs.getInt("numeric_precision"), rs.getInt("numeric_scale")
rs.getInt("numeric_scale")
), tableName.replace("\"", "")); ), tableName.replace("\"", ""));
log.debug("metadata of table \"{}\" is {}", tableName, columnMetaDataList); log.debug("metadata of table \"{}\" is {}", tableName, columnMetaDataList);
return columnMetaDataList.isEmpty() ? null : return columnMetaDataList.isEmpty() ? null :
columnMetaDataList.stream().collect(Collectors.toMap(ColumnMetaData::getName, Function.identity())); columnMetaDataList.stream().collect(Collectors.toMap(ColumnMetaData::getName, Function.identity()));
} }
} }