From 20aef32798c5938bb246b0c8b5a8ec18ec137eb3 Mon Sep 17 00:00:00 2001 From: AKurakin Date: Thu, 12 Sep 2024 19:09:54 +0300 Subject: [PATCH] =?UTF-8?q?http://jira.mfd.msk:8088/browse/CLS-754=20?= =?UTF-8?q?=D0=BC=D0=B5=D0=BB=D0=BA=D0=B8=D0=B9=20=D1=80=D0=B5=D1=84=D0=B0?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imdg/base/SimpleObjectMapStore.java | 29 +++++++++---------- .../imdg/util/PostgresColumnTypeUtil.java | 19 ++++++------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/base/SimpleObjectMapStore.java b/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/base/SimpleObjectMapStore.java index 541726a30..b1539c587 100644 --- a/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/base/SimpleObjectMapStore.java +++ b/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/base/SimpleObjectMapStore.java @@ -7,7 +7,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Types; -import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.util.ArrayList; @@ -39,7 +38,6 @@ public abstract class SimpleObjectMapStore implements private final static int BATCH_SIZE = 1000; protected final Logger log = LoggerFactory.getLogger(this.getClass()); protected final JdbcTemplate jdbcTemplate; - protected SimpleDateFormat DB_DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); protected Map columnMetaDataMap; protected SimpleObjectMapStore(JdbcTemplate jdbcTemplate) { @@ -160,7 +158,7 @@ public abstract class SimpleObjectMapStore implements } protected void batchInsertUpdate(@NonNull String insertStatement, @NonNull List args) { - updateMetaData(); + readMetaData(); try { int[][] ret = jdbcTemplate.batchUpdate(insertStatement, args, BATCH_SIZE, (ps, params) -> { for (int i = 0; i < params.length; i++) { @@ -181,10 +179,11 @@ public abstract class SimpleObjectMapStore implements } } - Object normalizeValue(Object value, ParameterMetaData metaData, Map columnMetaDataMap, int i, String insertStatement, Object[] params) throws SQLException { + Object normalizeValue(Object value, ParameterMetaData metaData, Map columnMetaDataMap, int i, + String insertStatement, Object[] params) throws SQLException { if (value == null) return null; - i++; // нумерация в ParameterMetaData с 1 + i++; // нумерация в ParameterMetaData начинается с 1 final int type = metaData.getParameterType(i); final String fieldName = getFields()[i - 1].toLowerCase(); @@ -196,24 +195,24 @@ public abstract class SimpleObjectMapStore implements if (columnMetaDataMap == null) { length = metaData.getPrecision(i + 1); } else { - // в postgresql для String ParameterMetaData.getPrecision возвращает 0, по этому используется другой способ через columnMetaDataMap. + // в postgresql для String ParameterMetaData.getPrecision возвращает 0, используется columnMetaDataMap length = columnMetaDataMap.get(fieldName).getPrecision(); } if (length > 0 && stringValue.length() > 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); } } else if ((type == Types.DECIMAL || type == Types.NUMERIC || type == Types.BIGINT || type == Types.INTEGER) && (value instanceof BigDecimal || value instanceof Long || value instanceof Integer)) { if (value instanceof Integer && (type == Types.INTEGER || type == Types.BIGINT)) { - return value; // value ok + return value; // ok } if (value instanceof Long vl) { if (type == Types.BIGINT) { - return value; // value ok + return value; // ok } if (type == Types.INTEGER) { if (vl > Integer.MAX_VALUE) { @@ -242,12 +241,12 @@ public abstract class SimpleObjectMapStore implements if (bigDecimalValue.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) { Object oldValue = 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); } else if (bigDecimalValue.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) { Object oldValue = 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); } return value; // value ok @@ -256,12 +255,12 @@ public abstract class SimpleObjectMapStore implements if (bigDecimalValue.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) { Object oldValue = 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); } else if (bigDecimalValue.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) { Object oldValue = 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); } return value; // value ok @@ -280,7 +279,7 @@ public abstract class SimpleObjectMapStore implements value = new BigDecimal(BigDecimal.valueOf(Math.pow(10, precision - dbScalePartLength)).longValue() - 1); if (negative) 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); } } @@ -311,7 +310,7 @@ public abstract class SimpleObjectMapStore implements return TextUtil.format(INSERT_TEMPLATE, params); } - private void updateMetaData() { + private void readMetaData() { if (columnMetaDataMap == null) synchronized (this) { columnMetaDataMap = PostgresColumnTypeUtil.extractTableMetadata(jdbcTemplate, getTableName()); } diff --git a/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/util/PostgresColumnTypeUtil.java b/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/util/PostgresColumnTypeUtil.java index 1d6b7035a..1b16dfcf0 100644 --- a/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/util/PostgresColumnTypeUtil.java +++ b/clearing-parent/imdg/src/main/java/ru/spcex/clearing/imdg/util/PostgresColumnTypeUtil.java @@ -10,20 +10,19 @@ import org.springframework.jdbc.core.JdbcTemplate; public final class PostgresColumnTypeUtil { 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_CASE_INSENS = "SELECT * FROM information_schema.columns WHERE table_name ilike ?"; + static String SELECT_METADATA_LIKE = "SELECT * FROM information_schema.columns WHERE table_name=?"; + static String SELECT_METADATA_ILIKE = "SELECT * FROM information_schema.columns WHERE table_name ilike ?"; public static Map extractTableMetadata(JdbcTemplate template, String tableName) { - List columnMetaDataList = template.query(tableName.contains("\"") ? SELECT_METADATA_CASE_SENS - : SELECT_METADATA_CASE_INSENS, (rs, rn) -> new ColumnMetaData( - rs.getString("column_name"), - rs.getString("data_type"), - rs.getInt("character_maximum_length"), - rs.getInt("numeric_precision"), - rs.getInt("numeric_scale") + List columnMetaDataList = template.query(tableName.contains("\"") ? SELECT_METADATA_LIKE + : SELECT_METADATA_ILIKE, (rs, rn) -> new ColumnMetaData( + rs.getString("column_name"), + rs.getString("data_type"), + rs.getInt("character_maximum_length"), + rs.getInt("numeric_precision"), rs.getInt("numeric_scale") ), tableName.replace("\"", "")); log.debug("metadata of table \"{}\" is {}", tableName, columnMetaDataList); return columnMetaDataList.isEmpty() ? null : - columnMetaDataList.stream().collect(Collectors.toMap(ColumnMetaData::getName, Function.identity())); + columnMetaDataList.stream().collect(Collectors.toMap(ColumnMetaData::getName, Function.identity())); } }