-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from FuriousPws002/05-result-set-handler-with-r…
…esult-type 05 result set handler with result type
- Loading branch information
Showing
29 changed files
with
495 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/org/apache/ibatis/executor/result/ResultMapException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.apache.ibatis.executor.result; | ||
|
||
/** | ||
* @author furious 2024/4/11 | ||
*/ | ||
public class ResultMapException extends RuntimeException{ | ||
|
||
public ResultMapException() { | ||
super(); | ||
} | ||
|
||
public ResultMapException(String message) { | ||
super(message); | ||
} | ||
|
||
public ResultMapException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ResultMapException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
protected ResultMapException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package org.apache.ibatis.executor.resultset; | ||
|
||
import java.lang.reflect.Field; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.apache.commons.lang3.reflect.FieldUtils; | ||
import org.apache.ibatis.mapping.MappedStatement; | ||
import org.apache.ibatis.mapping.ResultMap; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.type.TypeHandler; | ||
import org.apache.ibatis.type.TypeHandlerRegistry; | ||
|
||
/** | ||
* @author furious 2024/4/12 | ||
*/ | ||
public class DefaultResultSetHandler implements ResultSetHandler { | ||
|
||
private final Configuration configuration; | ||
private final MappedStatement mappedStatement; | ||
private final TypeHandlerRegistry typeHandlerRegistry; | ||
|
||
public DefaultResultSetHandler(MappedStatement mappedStatement) { | ||
this.configuration = mappedStatement.getConfiguration(); | ||
this.mappedStatement = mappedStatement; | ||
this.typeHandlerRegistry = configuration.getTypeHandlerRegistry(); | ||
} | ||
|
||
@Override | ||
public <T> List<T> handleResultSets(Statement stmt) throws SQLException { | ||
final List<Object> multipleResults = new ArrayList<>(); | ||
ResultSet rs = stmt.getResultSet(); | ||
if (Objects.isNull(rs)) { | ||
return Collections.emptyList(); | ||
} | ||
ResultSetWrapper rsw = new ResultSetWrapper(rs, configuration); | ||
ResultMap resultMap = mappedStatement.getResultMap(); | ||
handleResultSet(rsw, resultMap, multipleResults); | ||
return (List<T>) multipleResults; | ||
} | ||
|
||
private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults) throws SQLException { | ||
try { | ||
handleRowValues(rsw, resultMap, multipleResults); | ||
} finally { | ||
closeResultSet(rsw.getResultSet()); | ||
} | ||
} | ||
|
||
private void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults) throws SQLException { | ||
handleRowValuesForSimpleResultMap(rsw, resultMap, multipleResults); | ||
} | ||
|
||
private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults) throws SQLException { | ||
ResultSet resultSet = rsw.getResultSet(); | ||
while (!resultSet.isClosed() && resultSet.next()) { | ||
Object rowValue = getRowValue(rsw, resultMap); | ||
multipleResults.add(rowValue); | ||
} | ||
} | ||
|
||
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException { | ||
Object rowValue = createResultObject(resultMap); | ||
applyAutomaticMappings(rsw, rowValue); | ||
return rowValue; | ||
} | ||
|
||
private boolean applyAutomaticMappings(ResultSetWrapper rsw, Object rowValue) throws SQLException { | ||
List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, rowValue); | ||
boolean foundValues = false; | ||
if (!autoMapping.isEmpty()) { | ||
for (UnMappedColumnAutoMapping mapping : autoMapping) { | ||
final Object value = mapping.typeHandler.getResult(rsw.getResultSet(), mapping.column); | ||
if (value != null) { | ||
foundValues = true; | ||
} | ||
try { | ||
FieldUtils.writeField(rowValue, mapping.property, value, true); | ||
} catch (IllegalAccessException e) { | ||
ExceptionUtils.rethrow(e); | ||
} | ||
} | ||
} | ||
return foundValues; | ||
} | ||
|
||
private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper rsw, Object rowValue) { | ||
List<UnMappedColumnAutoMapping> autoMapping = new ArrayList<>(); | ||
List<String> columnLabels = rsw.getColumnLabels(); | ||
for (String columnLabel : columnLabels) { | ||
Field field = FieldUtils.getDeclaredField(rowValue.getClass(), columnLabel, true); | ||
String property = Objects.isNull(field) ? null : columnLabel; | ||
if (Objects.nonNull(property)) { | ||
Class<?> propertyType = field.getType(); | ||
if (typeHandlerRegistry.hasTypeHandler(propertyType)) { | ||
final TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(propertyType); | ||
autoMapping.add(new UnMappedColumnAutoMapping(columnLabel, property, typeHandler)); | ||
} | ||
} | ||
} | ||
return autoMapping; | ||
} | ||
|
||
private Object createResultObject(ResultMap resultMap) throws SQLException { | ||
try { | ||
return resultMap.getType().newInstance(); | ||
} catch (Exception e) { | ||
throw new SQLException("create return type fail " + e); | ||
} | ||
} | ||
|
||
private void closeResultSet(ResultSet rs) { | ||
try { | ||
if (rs != null) { | ||
rs.close(); | ||
} | ||
} catch (SQLException ignored) { | ||
} | ||
} | ||
|
||
/** | ||
* 数据库中列和对象中属性映射 | ||
*/ | ||
private static class UnMappedColumnAutoMapping { | ||
private final String column; | ||
private final String property; | ||
private final TypeHandler<?> typeHandler; | ||
|
||
public UnMappedColumnAutoMapping(String column, String property, TypeHandler<?> typeHandler) { | ||
this.column = column; | ||
this.property = property; | ||
this.typeHandler = typeHandler; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/apache/ibatis/executor/resultset/ResultSetHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.apache.ibatis.executor.resultset; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
|
||
/** | ||
* @author furious 2024/4/11 | ||
*/ | ||
public interface ResultSetHandler { | ||
|
||
<T> List<T> handleResultSets(Statement stmt) throws SQLException; | ||
} |
Oops, something went wrong.