Skip to content
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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public static ClassLoader getArtifactClassloader(MavenProject project,
// Find project dependencies, including the transitive ones.
Set<Artifact> dependencies = project.getArtifacts();
if ((dependencies != null) && !dependencies.isEmpty()) {
for (Iterator it = dependencies.iterator(); it.hasNext(); ) {
addArtifact(uris, (Artifact) it.next(), log, verbose);
for (Artifact artifact : dependencies) {
addArtifact(uris, artifact, log, verbose);
}
} else {
log.info("there are no resolved artifacts for the Maven project.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public List<RanChangeSet> getRanChangeSets() throws DatabaseException {
if (hasDatabaseChangeLogTable()) {
Scope.getCurrentScope().getLog(getClass()).info("Reading from " + databaseChangeLogTableName);
List<Map<String, ?>> results = queryDatabaseChangeLogTable(database);
for (Map rs : results) {
for (Map<String, ?> rs : results) {
String storedFileName = rs.get("FILENAME").toString();
String fileName = DatabaseChangeLog.normalizePath(storedFileName);
String author = rs.get("AUTHOR").toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @deprecated Implement commands with {@link liquibase.command.CommandStep} and call them with {@link liquibase.command.CommandFactory#getCommandDefinition(String...)}.
*/
public class HistoryCommand extends AbstractCommand {
public class HistoryCommand extends AbstractCommand<CommandResult> {
Copy link
Contributor

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.


private Database database;
private final DateFormat dateFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class AbstractChangelogCommandStep extends AbstractCommandStep {
}

protected static void validateRunOnChangeTypes(final CommandScope commandScope) throws CommandValidationException {
final Collection<String> runOnChangeTypes = new ArrayList(Arrays.asList(commandScope.getArgumentValue(RUN_ON_CHANGE_TYPES_ARG).split("\\s*,\\s*")));
final Collection<String> runOnChangeTypes = new ArrayList<>(Arrays.asList(commandScope.getArgumentValue(RUN_ON_CHANGE_TYPES_ARG).split("\\s*,\\s*")));
final Collection<String> supportedRunOnChangeTypes = supportedRunOnChangeTypes().collect(Collectors.toList());
supportedRunOnChangeTypes.add("none");
runOnChangeTypes.removeAll(supportedRunOnChangeTypes);
Expand All @@ -46,7 +46,7 @@ protected static void validateRunOnChangeTypes(final CommandScope commandScope)
}

protected static void validateReplaceIfExistsTypes(final CommandScope commandScope) throws CommandValidationException {
final Collection<String> replaceIfExistsTypes = new ArrayList(Arrays.asList(commandScope.getArgumentValue(REPLACE_IF_EXISTS_TYPES_ARG).split("\\s*,\\s*")));
final Collection<String> replaceIfExistsTypes = new ArrayList<>(Arrays.asList(commandScope.getArgumentValue(REPLACE_IF_EXISTS_TYPES_ARG).split("\\s*,\\s*")));
final Collection<String> supportedReplaceIfExistsTypes = supportedReplaceIfExistsTypes().collect(Collectors.toList());
supportedReplaceIfExistsTypes.add("none");
replaceIfExistsTypes.removeAll(supportedReplaceIfExistsTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import liquibase.serializer.AbstractLiquibaseSerializable;
import liquibase.serializer.LiquibaseSerializable;

public class Difference extends AbstractLiquibaseSerializable implements Comparable, LiquibaseSerializable {
public class Difference extends AbstractLiquibaseSerializable implements Comparable<Difference>, LiquibaseSerializable {
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
private final String message;
private final String field;
private final Object referenceValue;
Expand Down Expand Up @@ -55,7 +55,7 @@
}

@Override
public int compareTo(Object o) {
return this.getField().compareTo(((Difference) o).getField());
public int compareTo(Difference o) {
return this.getField().compareTo(o.getField());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public interface Executor extends Plugin {
*/
int queryForInt(SqlStatement sql, List<SqlVisitor> sqlVisitors) throws DatabaseException;

List queryForList(SqlStatement sql, Class elementType) throws DatabaseException;
<T> List<T> queryForList(SqlStatement sql, Class<T> elementType) throws DatabaseException;

List queryForList(SqlStatement sql, Class elementType, List<SqlVisitor> sqlVisitors) throws DatabaseException;
<T> List<T> queryForList(SqlStatement sql, Class<T> elementType, List<SqlVisitor> sqlVisitors) throws DatabaseException;

/**
* Executes a given SQL statement and returns a List of rows. Each row is represented a a Map<String, ?>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ public int queryForInt(SqlStatement sql, List<SqlVisitor> sqlVisitors) throws Da
}

@Override
public List queryForList(SqlStatement sql, Class elementType) throws DatabaseException {
public <T> List<T> queryForList(SqlStatement sql, Class<T> elementType) throws DatabaseException {
return delegatedReadExecutor.queryForList(sql, elementType);
}

@Override
public List queryForList(SqlStatement sql, Class elementType, List<SqlVisitor> sqlVisitors)
public <T> List<T> queryForList(SqlStatement sql, Class<T> elementType, List<SqlVisitor> sqlVisitors)
throws DatabaseException {
return delegatedReadExecutor.queryForList(sql, elementType, sqlVisitors);
}
Expand Down Expand Up @@ -265,6 +265,4 @@ public void close() throws IOException {
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author Spring Framework
*/
interface CallableStatementCallback {
interface CallableStatementCallback<T> {

/**
* Gets called by <code>JdbcTemplate.execute</code> with an active JDBC
Expand All @@ -27,7 +27,7 @@ interface CallableStatementCallback {
* into a DataAccessException by a SQLExceptionTranslator
* @throws liquibase.exception.DatabaseException in case of custom exceptions
*/
Object doInCallableStatement(CallableStatement cs) throws SQLException, DatabaseException;
T doInCallableStatement(CallableStatement cs) throws SQLException, DatabaseException;

SqlStatement getStatement();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

/**
Expand Down Expand Up @@ -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
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -167,7 +167,6 @@
}
}


if (sql instanceof ExecutablePreparedStatement) {
((ExecutablePreparedStatement) sql).execute(new PreparedStatementFactory((JdbcConnection) database.getConnection()));
return;
Expand Down Expand Up @@ -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());

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 {
Copy link
Contributor

@MalloD12 MalloD12 May 6, 2024

Choose a reason for hiding this comment

The 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");
Expand All @@ -343,7 +342,7 @@
* @return the RowMapper to use
* @see ColumnMapRowMapper
*/
protected RowMapper getColumnMapRowMapper() {
protected RowMapper<Map<String,?>> getColumnMapRowMapper() {
return new ColumnMapRowMapper(database.isCaseSensitive());
}

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

ResultSet rs = null;
try {
rs = cs.executeQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
* Callback interface used by {@link liquibase.executor.Executor}'s query methods.
Expand All @@ -16,7 +17,7 @@
* @see RowCallbackHandler
* @see RowMapper
*/
interface ResultSetExtractor {
interface ResultSetExtractor<T> {

/**
* Implementations must implement this method to process the entire ResultSet.
Expand All @@ -29,6 +30,5 @@ interface ResultSetExtractor {
* values or navigating (that is, there's no need to catch SQLException)
* @throws liquibase.exception.DatabaseException in case of custom exceptions
*/
Object extractData(ResultSet rs) throws SQLException;

List<T> extractData(ResultSet rs) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @see RowCallbackHandler
* @see ResultSetExtractor
*/
public interface RowMapper {
public interface RowMapper<T> {

/**
* Implementations must implement this method to map each row of data
Expand All @@ -30,6 +30,5 @@ public interface RowMapper {
* @throws java.sql.SQLException if a SQLException is encountered getting
* column values (that is, there's no need to catch SQLException)
*/
Object mapRow(ResultSet rs, int rowNum) throws SQLException;

T mapRow(ResultSet rs, int rowNum) throws SQLException;
}