From 8fcec9ecf32620339b174e7e7f58e0e394e30aef Mon Sep 17 00:00:00 2001 From: mikemirzayanov Date: Tue, 13 Aug 2024 00:40:39 +0300 Subject: [PATCH] TypeOracle#convertFromPartialRow(s) --- code/bundle.bat | 1 + .../org/jacuzzi/core/CachedTypeOracle.java | 11 +++ .../java/org/jacuzzi/core/TypeOracle.java | 4 ++ .../java/org/jacuzzi/core/TypeOracleImpl.java | 70 ++++++++++++------- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/code/bundle.bat b/code/bundle.bat index 9e3e830..6f8d2d3 100644 --- a/code/bundle.bat +++ b/code/bundle.bat @@ -7,3 +7,4 @@ if not "!JAVA8_64_HOME!"=="" ( call mvn.cmd validate --batch-mode call mvn.cmd -Dfile.encoding=UTF-8 -DcreateChecksum=true --batch-mode clean source:jar javadoc:jar repository:bundle-create install +call mvn.cmd test diff --git a/code/src/main/java/org/jacuzzi/core/CachedTypeOracle.java b/code/src/main/java/org/jacuzzi/core/CachedTypeOracle.java index 47c1587..46ad07e 100644 --- a/code/src/main/java/org/jacuzzi/core/CachedTypeOracle.java +++ b/code/src/main/java/org/jacuzzi/core/CachedTypeOracle.java @@ -7,6 +7,7 @@ /** * @author Mike Mirzayanov */ +@SuppressWarnings("unused") class CachedTypeOracle extends TypeOracle { private static final ThreadLocal, TypeOracle>> threadLocalCache = new ThreadLocal, TypeOracle>>() { @Override @@ -49,6 +50,16 @@ public List convertFromRows(List rows) { return typeOracle.convertFromRows(rows); } + @Override + public T convertFromPartialRow(Row row) { + return typeOracle.convertFromPartialRow(row); + } + + @Override + public List convertFromPartialRows(List rows) { + return typeOracle.convertFromPartialRows(rows); + } + @Override String getFieldList(boolean includeId, boolean useTablePrefix, OperationType operationType) { return typeOracle.getFieldList(includeId, useTablePrefix, operationType); diff --git a/code/src/main/java/org/jacuzzi/core/TypeOracle.java b/code/src/main/java/org/jacuzzi/core/TypeOracle.java index ebb27fa..9c46f71 100644 --- a/code/src/main/java/org/jacuzzi/core/TypeOracle.java +++ b/code/src/main/java/org/jacuzzi/core/TypeOracle.java @@ -20,6 +20,10 @@ public abstract class TypeOracle { public abstract List convertFromRows(List rows); + public abstract T convertFromPartialRow(Row row); + + public abstract List convertFromPartialRows(List rows); + abstract String getFieldList(boolean includeId, boolean useTablePrefix, OperationType operationType); abstract String getValuesPatternListForInsert(boolean includeId, T instance); diff --git a/code/src/main/java/org/jacuzzi/core/TypeOracleImpl.java b/code/src/main/java/org/jacuzzi/core/TypeOracleImpl.java index f616fcf..9cb5835 100644 --- a/code/src/main/java/org/jacuzzi/core/TypeOracleImpl.java +++ b/code/src/main/java/org/jacuzzi/core/TypeOracleImpl.java @@ -32,8 +32,8 @@ class TypeOracleImpl extends TypeOracle { this.clazz = clazz; fastClazz = FastClass.create(clazz); - List internalFields = new ArrayList(); - Map internalFieldByColumn = new HashMap(); + List internalFields = new ArrayList<>(); + Map internalFieldByColumn = new HashMap<>(); if (clazz.getAnnotation(MappedTo.class) == null) { tableName = clazz.getSimpleName(); @@ -208,7 +208,7 @@ public String getValuesPatternListForInsert(boolean includeId, T instance) { @Override List getValueListForInsert(boolean includeId, T instance) { - List result = new ArrayList(fields.size()); + List result = new ArrayList<>(fields.size()); try { for (Field field : fields) { @@ -258,7 +258,7 @@ public List getQuerySetArguments(T instance) { throw new MappingException("Nothing to set for class " + clazz + '.'); } - List result = new ArrayList(fields.size() + 1); + List result = new ArrayList<>(fields.size() + 1); for (Field field : fields) { if (field.isIgnoreUpdate()) { @@ -332,7 +332,7 @@ private static String toString(Row row) { return result.toString(); } - private static final String toLowerCase(String s) { + private static String toLowerCase(String s) { if (s == null) { return null; } @@ -354,14 +354,15 @@ private static final String toLowerCase(String s) { } @SuppressWarnings({"OverlyComplexMethod", "OverlyLongMethod"}) - @Override - public T convertFromRow(Row row) { + public T convertFromRow(Row row, boolean allowPartialRow) { Set columns = row.keySet(); T instance = newInstance(); - Map columnNameByLowercaseColumnName = new HashMap(); - for (String column : fieldByColumn.keySet()) { - columnNameByLowercaseColumnName.put(toLowerCase(column), column); + Map columnNameByLowercaseColumnName = new HashMap<>(); + for (Map.Entry e : fieldByColumn.entrySet()) { + if (!e.getValue().isIgnoreSelect()) { + columnNameByLowercaseColumnName.put(toLowerCase(e.getKey()), e.getKey()); + } } for (String column : columns) { @@ -373,15 +374,7 @@ public T convertFromRow(Row row) { Class expectedParameterType = field.getSetter().getParameterTypes()[0]; Object castedParameter = convertTo(parameter, expectedParameterType); field.getSetter().invoke(instance, new Object[]{castedParameter}); - } catch (InvocationTargetException e) { - if (row.get(column) != null) { - throw new MappingException("Can't invoke setter " + field.getSetter() + "[clazz=" - + clazz.getName() + "] for parameter " + row.get(column).getClass() + " [row=" + toString(row) + "].", e); - } else { - throw new MappingException("Can't invoke setter " + field.getSetter() + " for class " - + clazz.getName() + " [row=" + toString(row) + "].", e); - } - } catch (RuntimeException e) { + } catch (InvocationTargetException | RuntimeException e) { if (row.get(column) != null) { throw new MappingException("Can't invoke setter " + field.getSetter() + "[clazz=" + clazz.getName() + "] for parameter " + row.get(column).getClass() + " [row=" + toString(row) + "].", e); @@ -393,7 +386,7 @@ public T convertFromRow(Row row) { } } - if (!columnNameByLowercaseColumnName.isEmpty()) { + if (!allowPartialRow && !columnNameByLowercaseColumnName.isEmpty()) { StringBuilder message = new StringBuilder("There is uninitialized field(s) remained in the entity ") .append(instance); @@ -405,7 +398,11 @@ public T convertFromRow(Row row) { message.append(columnName).append('\''); } - message.append(".\n\tStack trace:"); + message.append('.'); + + String exceptionMessage = message.toString(); + + message.append("\n\tStack trace:"); StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); @@ -418,22 +415,43 @@ public T convertFromRow(Row row) { } message.append('.'); - logger.error(message.toString()); + + throw new DatabaseException(exceptionMessage); } return instance; } - @Override - public List convertFromRows(List rows) { - List instances = new ArrayList(rows.size()); + public List convertFromRows(List rows, boolean allowPartialRow) { + List instances = new ArrayList<>(rows.size()); for (Row row : rows) { - instances.add(convertFromRow(row)); + instances.add(convertFromRow(row, allowPartialRow)); } return instances; } + @Override + public T convertFromRow(Row row) { + return convertFromRow(row, false); + } + + @Override + public List convertFromRows(List rows) { + return convertFromRows(rows, false); + } + + @Override + public T convertFromPartialRow(Row row) { + return convertFromRow(row, true); + } + + @Override + public List convertFromPartialRows(List rows) { + return convertFromRows(rows, true); + } + + @SuppressWarnings("unused") private static class Field { private FastMethod setter; private FastMethod getter;