Skip to content

Commit

Permalink
Merge pull request #22 from Codeforces/dev-mikemirzayanov
Browse files Browse the repository at this point in the history
TypeOracle#convertFromPartialRow(s)
  • Loading branch information
MikeMirzayanov authored Aug 29, 2024
2 parents d59ab0a + 8fcec9e commit b56c2a0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
1 change: 1 addition & 0 deletions code/bundle.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions code/src/main/java/org/jacuzzi/core/CachedTypeOracle.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @author Mike Mirzayanov
*/
@SuppressWarnings("unused")
class CachedTypeOracle<T> extends TypeOracle<T> {
private static final ThreadLocal<Map<Class<?>, TypeOracle<?>>> threadLocalCache = new ThreadLocal<Map<Class<?>, TypeOracle<?>>>() {
@Override
Expand Down Expand Up @@ -49,6 +50,16 @@ public List<T> convertFromRows(List<Row> rows) {
return typeOracle.convertFromRows(rows);
}

@Override
public T convertFromPartialRow(Row row) {
return typeOracle.convertFromPartialRow(row);
}

@Override
public List<T> convertFromPartialRows(List<Row> rows) {
return typeOracle.convertFromPartialRows(rows);
}

@Override
String getFieldList(boolean includeId, boolean useTablePrefix, OperationType operationType) {
return typeOracle.getFieldList(includeId, useTablePrefix, operationType);
Expand Down
4 changes: 4 additions & 0 deletions code/src/main/java/org/jacuzzi/core/TypeOracle.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public abstract class TypeOracle<T> {

public abstract List<T> convertFromRows(List<Row> rows);

public abstract T convertFromPartialRow(Row row);

public abstract List<T> convertFromPartialRows(List<Row> rows);

abstract String getFieldList(boolean includeId, boolean useTablePrefix, OperationType operationType);

abstract String getValuesPatternListForInsert(boolean includeId, T instance);
Expand Down
70 changes: 44 additions & 26 deletions code/src/main/java/org/jacuzzi/core/TypeOracleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class TypeOracleImpl<T> extends TypeOracle<T> {
this.clazz = clazz;

fastClazz = FastClass.create(clazz);
List<Field> internalFields = new ArrayList<Field>();
Map<String, Field> internalFieldByColumn = new HashMap<String, Field>();
List<Field> internalFields = new ArrayList<>();
Map<String, Field> internalFieldByColumn = new HashMap<>();

if (clazz.getAnnotation(MappedTo.class) == null) {
tableName = clazz.getSimpleName();
Expand Down Expand Up @@ -208,7 +208,7 @@ public String getValuesPatternListForInsert(boolean includeId, T instance) {

@Override
List<Object> getValueListForInsert(boolean includeId, T instance) {
List<Object> result = new ArrayList<Object>(fields.size());
List<Object> result = new ArrayList<>(fields.size());

try {
for (Field field : fields) {
Expand Down Expand Up @@ -258,7 +258,7 @@ public List<Object> getQuerySetArguments(T instance) {
throw new MappingException("Nothing to set for class " + clazz + '.');
}

List<Object> result = new ArrayList<Object>(fields.size() + 1);
List<Object> result = new ArrayList<>(fields.size() + 1);

for (Field field : fields) {
if (field.isIgnoreUpdate()) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<String> columns = row.keySet();
T instance = newInstance();

Map<String, String> columnNameByLowercaseColumnName = new HashMap<String, String>();
for (String column : fieldByColumn.keySet()) {
columnNameByLowercaseColumnName.put(toLowerCase(column), column);
Map<String, String> columnNameByLowercaseColumnName = new HashMap<>();
for (Map.Entry<String, Field> e : fieldByColumn.entrySet()) {
if (!e.getValue().isIgnoreSelect()) {
columnNameByLowercaseColumnName.put(toLowerCase(e.getKey()), e.getKey());
}
}

for (String column : columns) {
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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();

Expand All @@ -418,22 +415,43 @@ public T convertFromRow(Row row) {
}

message.append('.');

logger.error(message.toString());

throw new DatabaseException(exceptionMessage);
}

return instance;
}

@Override
public List<T> convertFromRows(List<Row> rows) {
List<T> instances = new ArrayList<T>(rows.size());
public List<T> convertFromRows(List<Row> rows, boolean allowPartialRow) {
List<T> 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<T> convertFromRows(List<Row> rows) {
return convertFromRows(rows, false);
}

@Override
public T convertFromPartialRow(Row row) {
return convertFromRow(row, true);
}

@Override
public List<T> convertFromPartialRows(List<Row> rows) {
return convertFromRows(rows, true);
}

@SuppressWarnings("unused")
private static class Field {
private FastMethod setter;
private FastMethod getter;
Expand Down

0 comments on commit b56c2a0

Please sign in to comment.