Skip to content

Commit

Permalink
RawSqlStatement Liquibase PRO Refactoring (liquibase#6080)
Browse files Browse the repository at this point in the history
* Update setParameters method to deal with Array of parameters.

* Extracted common behavior for setting up a parameter in a separate method.
  • Loading branch information
MalloD12 authored Aug 14, 2024
1 parent 6dbc463 commit 0080d84
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,28 @@ public void execute(final SqlStatement sql, final List<SqlVisitor> sqlVisitors)
private void setParameters(final PreparedStatement pstmt, final RawParameterizedSqlStatement sql) throws SQLException {
final List<Object> parameters = sql.getParameters();
for (int i = 0; i < parameters.size(); i++) {
pstmt.setObject(i + 1, parameters.get(i));
Object parameter = parameters.get(i);
if(parameter instanceof ArrayList){
int finalI = i;
((ArrayList<?>) parameter).forEach(param -> {
try {
setParameter(pstmt, finalI, param);
} catch (SQLException e) {
throw new RuntimeException(e);
}
});
}
else {
setParameter(pstmt, i, parameter);
}
}
}

private static void setParameter(PreparedStatement pstmt, int parameterIndex, Object parameter) throws SQLException {
if (parameter instanceof String) {
pstmt.setString(parameterIndex + 1, (String) parameter);
} else {
pstmt.setObject(parameterIndex + 1, parameter);
}
}

Expand Down

0 comments on commit 0080d84

Please sign in to comment.