-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/adding some generics #5830
base: master
Are you sure you want to change the base?
Changes from 4 commits
1681e3b
60bc759
352ee6b
053de9e
08a5e5b
33c7f34
08bdbde
bb9fe57
96a717c
51c2474
147f8a9
8f47362
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
* @author Spring Framework | ||
*/ | ||
@SuppressWarnings({"unchecked"}) | ||
public class ColumnMapRowMapper implements RowMapper { | ||
public class ColumnMapRowMapper implements RowMapper<Map<String,?>> { | ||
|
||
private final boolean caseSensitiveDatabase; | ||
|
||
|
@@ -31,7 +31,7 @@ public ColumnMapRowMapper(boolean caseSensitiveDatabase) { | |
} | ||
|
||
@Override | ||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
public Map<String,?> mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
ResultSetMetaData rsmd = rs.getMetaData(); | ||
int columnCount = rsmd.getColumnCount(); | ||
Map mapOfColValues = createColumnMap(columnCount); | ||
|
@@ -49,8 +49,8 @@ public Object mapRow(ResultSet rs, int rowNum) throws SQLException { | |
* capacity for the Map | ||
* @return the new Map instance | ||
*/ | ||
protected Map createColumnMap(int columnCount) { | ||
return new LinkedHashMap(columnCount); | ||
protected Map<String,?> createColumnMap(int columnCount) { | ||
return new LinkedHashMap<>(columnCount); | ||
} | ||
|
||
/** | ||
|
@@ -80,5 +80,4 @@ protected String getColumnKey(String columnName) { | |
protected Object getColumnValue(ResultSet rs, int index) throws SQLException { | ||
return JdbcUtil.getResultSetValue(rs, index); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ | |
return true; | ||
} | ||
|
||
public Object execute(StatementCallback action, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
public <T> T execute(StatementCallback<T> action, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
DatabaseConnection con = database.getConnection(); | ||
Statement stmt = null; | ||
try { | ||
|
@@ -77,7 +77,7 @@ | |
stmt = ((JdbcConnection) con).getUnderlyingConnection().createStatement(); | ||
Statement stmtToUse = stmt; | ||
|
||
Object object = action.doInStatement(stmtToUse); | ||
T object = action.doInStatement(stmtToUse); | ||
if (stmtToUse.getWarnings() != null) { | ||
showSqlWarnings(stmtToUse); | ||
} | ||
|
@@ -121,7 +121,7 @@ | |
// into a query. Instead, we process a whole query. The check should be performed at the places where | ||
// the query is composed. | ||
@SuppressWarnings("squid:S2077") | ||
public Object execute(CallableStatementCallback action, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
public <T> T execute(CallableStatementCallback<T> action, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
DatabaseConnection con = database.getConnection(); | ||
|
||
if (con instanceof OfflineConnection) { | ||
|
@@ -167,7 +167,6 @@ | |
} | ||
} | ||
|
||
|
||
if (sql instanceof ExecutablePreparedStatement) { | ||
((ExecutablePreparedStatement) sql).execute(new PreparedStatementFactory((JdbcConnection) database.getConnection())); | ||
return; | ||
|
@@ -202,11 +201,11 @@ | |
} | ||
|
||
|
||
public Object query(final SqlStatement sql, final ResultSetExtractor rse) throws DatabaseException { | ||
public <T> List<T> query(final SqlStatement sql, final ResultSetExtractor<T> rse) throws DatabaseException { | ||
return query(sql, rse, new ArrayList<>()); | ||
} | ||
|
||
public Object query(final SqlStatement sql, final ResultSetExtractor rse, final List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
public <T> List<T> query(final SqlStatement sql, final ResultSetExtractor<T> rse, final List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
if (sql instanceof RawParameterizedSqlStatement) { | ||
PreparedStatementFactory factory = new PreparedStatementFactory((JdbcConnection) database.getConnection()); | ||
|
||
|
@@ -221,26 +220,26 @@ | |
} | ||
|
||
if (sql instanceof CallableSqlStatement) { | ||
return execute(new QueryCallableStatementCallback(sql, rse), sqlVisitors); | ||
return execute(new QueryCallableStatementCallback<>(sql, rse), sqlVisitors); | ||
} | ||
|
||
return execute(new QueryStatementCallback(sql, rse, sqlVisitors), sqlVisitors); | ||
return execute(new QueryStatementCallback<>(sql, rse, sqlVisitors), sqlVisitors); | ||
} | ||
|
||
public List query(SqlStatement sql, RowMapper rowMapper) throws DatabaseException { | ||
public <T> List<T> query(SqlStatement sql, RowMapper<T> rowMapper) throws DatabaseException { | ||
return query(sql, rowMapper, new ArrayList<>()); | ||
} | ||
|
||
public List query(SqlStatement sql, RowMapper rowMapper, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
return (List) query(sql, new RowMapperResultSetExtractor(rowMapper), sqlVisitors); | ||
public <T> List<T> query(SqlStatement sql, RowMapper<T> rowMapper, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
return query(sql, new RowMapperResultSetExtractor<>(rowMapper), sqlVisitors); | ||
} | ||
|
||
public Object queryForObject(SqlStatement sql, RowMapper rowMapper) throws DatabaseException { | ||
public <T> T queryForObject(SqlStatement sql, RowMapper<T> rowMapper) throws DatabaseException { | ||
return queryForObject(sql, rowMapper, new ArrayList<>()); | ||
} | ||
|
||
public Object queryForObject(SqlStatement sql, RowMapper rowMapper, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
List results = query(sql, rowMapper, sqlVisitors); | ||
public <T> T queryForObject(SqlStatement sql, RowMapper<T> rowMapper, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
List<T> results = query(sql, rowMapper, sqlVisitors); | ||
try { | ||
return JdbcUtil.requiredSingleResult(results); | ||
} catch (DatabaseException e) { | ||
|
@@ -281,12 +280,12 @@ | |
} | ||
|
||
@Override | ||
public List queryForList(SqlStatement sql, Class elementType) throws DatabaseException { | ||
public <T> List<T> queryForList(SqlStatement sql, Class<T> elementType) throws DatabaseException { | ||
return queryForList(sql, elementType, new ArrayList<>()); | ||
} | ||
|
||
@Override | ||
public List queryForList(SqlStatement sql, Class elementType, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
public <T> List<T> queryForList(SqlStatement sql, Class<T> elementType, List<SqlVisitor> sqlVisitors) throws DatabaseException { | ||
return query(sql, getSingleColumnRowMapper(elementType), sqlVisitors); | ||
} | ||
|
||
|
@@ -315,9 +314,9 @@ | |
throw new DatabaseException("Direct update using CallableSqlStatement not currently implemented"); | ||
} | ||
|
||
class UpdateStatementCallback implements StatementCallback { | ||
class UpdateStatementCallback implements StatementCallback<Integer> { | ||
@Override | ||
public Object doInStatement(Statement stmt) throws SQLException, DatabaseException { | ||
public Integer doInStatement(Statement stmt) throws SQLException, DatabaseException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this case I would leave it as it was because it's updating a public method and there is only one place where it is cast to an Integer. |
||
String[] sqlToExecute = applyVisitors(sql, sqlVisitors); | ||
if (sqlToExecute.length != 1) { | ||
throw new DatabaseException("Cannot call update on Statement that returns back multiple Sql objects"); | ||
|
@@ -343,7 +342,7 @@ | |
* @return the RowMapper to use | ||
* @see ColumnMapRowMapper | ||
*/ | ||
protected RowMapper getColumnMapRowMapper() { | ||
protected RowMapper<Map<String,?>> getColumnMapRowMapper() { | ||
return new ColumnMapRowMapper(database.isCaseSensitive()); | ||
} | ||
|
||
|
@@ -354,8 +353,8 @@ | |
* @return the RowMapper to use | ||
* @see SingleColumnRowMapper | ||
*/ | ||
protected RowMapper getSingleColumnRowMapper(Class requiredType) { | ||
return new SingleColumnRowMapper(requiredType); | ||
protected <T> RowMapper<T> getSingleColumnRowMapper(Class<T> requiredType) { | ||
return new SingleColumnRowMapper<>(requiredType); | ||
} | ||
|
||
@Override | ||
|
@@ -430,7 +429,7 @@ | |
return ""; | ||
} | ||
|
||
private class ExecuteStatementCallback implements StatementCallback { | ||
private class ExecuteStatementCallback implements StatementCallback<Void> { | ||
|
||
private final SqlStatement sql; | ||
private final List<SqlVisitor> sqlVisitors; | ||
|
@@ -441,7 +440,7 @@ | |
} | ||
|
||
@Override | ||
public Object doInStatement(Statement stmt) throws SQLException, DatabaseException { | ||
public Void doInStatement(Statement stmt) throws SQLException, DatabaseException { | ||
Logger log = Scope.getCurrentScope().getLog(getClass()); | ||
|
||
for (String statement : applyVisitors(sql, sqlVisitors)) { | ||
|
@@ -507,13 +506,13 @@ | |
} | ||
} | ||
|
||
private class QueryStatementCallback implements StatementCallback { | ||
private class QueryStatementCallback<T> implements StatementCallback<List<T>> { | ||
|
||
private final SqlStatement sql; | ||
private final List<SqlVisitor> sqlVisitors; | ||
private final ResultSetExtractor rse; | ||
private final ResultSetExtractor<T> rse; | ||
|
||
private QueryStatementCallback(SqlStatement sql, ResultSetExtractor rse, List<SqlVisitor> sqlVisitors) { | ||
private QueryStatementCallback(SqlStatement sql, ResultSetExtractor<T> rse, List<SqlVisitor> sqlVisitors) { | ||
this.sql = sql; | ||
this.rse = rse; | ||
this.sqlVisitors = sqlVisitors; | ||
|
@@ -535,7 +534,7 @@ | |
// parameter into a query. Instead, we process a whole query. The check should be performed at the places where | ||
// the query is composed. | ||
@SuppressWarnings("squid:S2077") | ||
public Object doInStatement(Statement stmt) throws SQLException, DatabaseException { | ||
public List<T> doInStatement(Statement stmt) throws SQLException, DatabaseException { | ||
ResultSet rs = null; | ||
try { | ||
String[] sqlToExecute = applyVisitors(sql, sqlVisitors); | ||
|
@@ -567,19 +566,19 @@ | |
} | ||
} | ||
|
||
private class QueryCallableStatementCallback implements CallableStatementCallback { | ||
private class QueryCallableStatementCallback<T> implements CallableStatementCallback<List<T>> { | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved
Hide resolved
|
||
|
||
private final SqlStatement sql; | ||
private final ResultSetExtractor rse; | ||
private final ResultSetExtractor<T> rse; | ||
|
||
private QueryCallableStatementCallback(SqlStatement sql, ResultSetExtractor rse) { | ||
private QueryCallableStatementCallback(SqlStatement sql, ResultSetExtractor<T> rse) { | ||
this.sql = sql; | ||
this.rse = rse; | ||
} | ||
|
||
|
||
@Override | ||
public Object doInCallableStatement(CallableStatement cs) throws SQLException, DatabaseException { | ||
public List<T> doInCallableStatement(CallableStatement cs) throws SQLException, DatabaseException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
ResultSet rs = null; | ||
try { | ||
rs = cs.executeQuery(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this could have a negative impact, but we try to avoid updating public elements (return types, method signatures, class definition, etc) as in some cases it could be a breaking change for some people depending on how they are using methods/classes. Maybe this case doesn't hurt, but considering our guidelines I should revert this one.