Skip to content

Commit

Permalink
fix(#984): allow with sql keyword for query action
Browse files Browse the repository at this point in the history
the `WITH` clause (also known as Common Table Expressions (CTEs)) is part of the SQL standard SQL:1999.
according to the [list of SQL reserved words](https://en.wikipedia.org/wiki/List_of_SQL_reserved_words)
all modern databases support it (as they should).
  • Loading branch information
bbortt committed Sep 15, 2023
1 parent f210123 commit fed0d3a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,18 @@ public void doExecute(TestContext context) {
* @param context
*/
protected void executeStatements(List<String> statements, List<Map<String, Object>> allResultRows, Map<String, List<String>> columnValuesMap, TestContext context) {
for (String stmt : statements) {
validateSqlStatement(stmt);
final String toExecute;
if (getJdbcTemplate() == null) {
throw new CitrusRuntimeException("No JdbcTemplate configured for query execution!");
}

if (stmt.trim().endsWith(";")) {
toExecute = context.replaceDynamicContentInString(stmt.trim().substring(0, stmt.trim().length()-1));
for (String statement : statements) {
validateSqlStatement(statement);

final String toExecute;
if (statement.trim().endsWith(";")) {
toExecute = context.replaceDynamicContentInString(statement.trim().substring(0, statement.trim().length() - 1));
} else {
toExecute = context.replaceDynamicContentInString(stmt.trim());
toExecute = context.replaceDynamicContentInString(statement.trim());
}

if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -333,11 +337,12 @@ private void performControlResultSetValidation(final Map<String, List<String>> c

/**
* Does some simple validation on the SQL statement.
* @param stmt The statement which is to be validated.
* @param statement The statement which is to be validated.
*/
protected void validateSqlStatement(String stmt) {
if (!stmt.toLowerCase().startsWith("select")) {
throw new CitrusRuntimeException("Missing keyword SELECT in statement: " + stmt);
protected void validateSqlStatement(String statement) {
String trimmedStatement = statement.toLowerCase().trim();
if (!(trimmedStatement.startsWith("select") || trimmedStatement.startsWith("with"))) {
throw new CitrusRuntimeException("Missing SELECT or WITH keyword in statement: " + trimmedStatement);
}
}

Expand Down
Loading

0 comments on commit fed0d3a

Please sign in to comment.