Skip to content

Commit

Permalink
repo-sqale: added ResultListRowTransformer#finishTransformation
Browse files Browse the repository at this point in the history
Method is default and empty, so the interface is still "SAM" (single
abstract method) interface.
Method finishTransformation() is called after all the rows were
transformed and it can be extremely useful if some data were collected
during the transformation.
Example: Previously, if parent object for container was needed, it was
obtained immediately (cache map for repeated parents could be used,
of course) which meant potentially up to N queries (by OID, sure, which
is fast, but you should still shudder when you hear infamous N+1).
Now you can just collect all the distinct OID and then loaded in one
SELECT ... WHERE oid IN (...) query. Cool, isn't it?
  • Loading branch information
virgo47 committed Sep 30, 2021
1 parent 3a51f37 commit 6720171
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,21 @@ public void processOptions(Collection<SelectorOptions<GetOperationOptions>> opti
/**
* Transforms result page with (bean + extension columns) tuple to schema type.
* JDBC session is provided as it may be needed for additional fetches.
* Instead of calling some transformation method row-by-row, transformer object is provided
* by the table mapper - which allows for potentially stateful processing.
* For example, row-by-row transformation can collect information for some additional processing
* which can be executed by implementing {@link ResultListRowTransformer#finishTransformation()}.
*/
public PageOf<S> transformToSchemaType(PageOf<Tuple> result, JdbcSession jdbcSession)
throws SchemaException, QueryException {
try {
ResultListRowTransformer<S, Q, R> rowTransformer =
entityPathMapping.createRowTransformer(this, jdbcSession);
return result.map(row -> rowTransformer.transform(row, root(), options));

PageOf<S> transformedResult = result.map(row -> rowTransformer.transform(row, root(), options));
rowTransformer.finishTransformation();

return transformedResult;
} catch (RepositoryMappingException e) {
Throwable cause = e.getCause();
if (cause instanceof SchemaException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public interface ResultListRowTransformer<S, Q extends FlexibleRelationalPathBas

S transform(Tuple rowTuple, Q entityPath,
Collection<SelectorOptions<GetOperationOptions>> options);

/**
* This method is called after all the rows were transformed and allows for additional
* final shake-up for more complex stateful transformations.
*/
default void finishTransformation() {
// nothing by default
}
}

0 comments on commit 6720171

Please sign in to comment.