Skip to content

Commit

Permalink
NIFI-8027 In AbstractExecuteSQL make 'SQL Pre-Query' and 'SQL Post-Qu…
Browse files Browse the repository at this point in the history
…ery' properties support escaping semicolons to allow them be part of statements.

Signed-off-by: Matthew Burgess <[email protected]>

This closes apache#4732
  • Loading branch information
tpalfy authored and mattyb149 committed Dec 16, 2020
1 parent 02665d7 commit 989287a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public abstract class AbstractExecuteSQL extends AbstractProcessor {
.displayName("SQL Pre-Query")
.description("A semicolon-delimited list of queries executed before the main SQL query is executed. " +
"For example, set session properties before main query. " +
"It's possible to include semicolons in the statements themselves by escaping them with a backslash ('\\;'). " +
"Results/outputs from these queries will be suppressed if there are no errors.")
.required(false)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
Expand All @@ -114,6 +115,7 @@ public abstract class AbstractExecuteSQL extends AbstractProcessor {
.displayName("SQL Post-Query")
.description("A semicolon-delimited list of queries executed after the main SQL query is executed. " +
"Example like setting session properties after main query. " +
"It's possible to include semicolons in the statements themselves by escaping them with a backslash ('\\;'). " +
"Results/outputs from these queries will be suppressed if there are no errors.")
.required(false)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
Expand Down Expand Up @@ -473,7 +475,8 @@ protected List<String> getQueries(final String value) {
return null;
}
final List<String> queries = new LinkedList<>();
for (String query : value.split(";")) {
for (String query : value.split("(?<!\\\\);")) {
query = query.replaceAll("\\\\;", ";");
if (query.trim().length() > 0) {
queries.add(query.trim());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.nifi.processors.standard;

import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processors.standard.sql.SqlWriter;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class TestAbstractExecuteSQL {
private AbstractExecuteSQL testSubject;

@Before
public void setUp() throws Exception {
testSubject = new AbstractExecuteSQL() {
@Override
protected SqlWriter configureSqlWriter(ProcessSession session, ProcessContext context, FlowFile fileToProcess) {
return null;
}
};
}

@Test
public void testGetQueries() throws Exception {
// GIVEN
String queriesString = "SOME kind of PRE-QUERY statement;\n" +
"AND another PRE-QUERY statment;";

List<String> expected = Arrays.asList(
"SOME kind of PRE-QUERY statement",
"AND another PRE-QUERY statment"
);

// WHEN
List<String> actual = testSubject.getQueries(queriesString);

// THEN
assertEquals(expected, actual);
}

@Test
public void testGetQueriesWithEscapedSemicolon() throws Exception {
// GIVEN
String queriesString = "SET COMPLEX_KEY = 'KEYPART_1=value1\\;KEYPART_2=<valuePart2>\\;’ FOR SESSION;\n" +
"SOME other PRE-QUERY statement;\n" +
"AND another PRE-QUERY statment;";

List<String> expected = Arrays.asList(
"SET COMPLEX_KEY = 'KEYPART_1=value1;KEYPART_2=<valuePart2>;’ FOR SESSION",
"SOME other PRE-QUERY statement",
"AND another PRE-QUERY statment"
);

// WHEN
List<String> actual = testSubject.getQueries(queriesString);

// THEN
assertEquals(expected, actual);
}
}

0 comments on commit 989287a

Please sign in to comment.