diff --git a/img/sql-console-screenshot.png b/img/sql-console-screenshot.png index 715f78a..08270af 100644 Binary files a/img/sql-console-screenshot.png and b/img/sql-console-screenshot.png differ diff --git a/modules/core/src/de/diedavids/cuba/runtimediagnose/db/DbQueryParser.groovy b/modules/core/src/de/diedavids/cuba/runtimediagnose/db/DbQueryParser.groovy index 563217a..5ce3d2b 100644 --- a/modules/core/src/de/diedavids/cuba/runtimediagnose/db/DbQueryParser.groovy +++ b/modules/core/src/de/diedavids/cuba/runtimediagnose/db/DbQueryParser.groovy @@ -27,6 +27,7 @@ import net.sf.jsqlparser.statement.update.Update import org.springframework.stereotype.Component import javax.inject.Inject +import java.util.regex.Pattern @Component class DbQueryParser { @@ -65,7 +66,8 @@ class DbQueryParser { if (DiagnoseType.JPQL == diagnoseType) { - analyseJpql(queryString) + String queryStringWithoutComments = excludeComments(queryString) + analyseJpql(queryStringWithoutComments) } statements @@ -109,4 +111,11 @@ class DbQueryParser { containsIllegalOperation } + + String excludeComments(String queryString) { + Pattern.compile('/\\*.*?\\*/', Pattern.DOTALL) + .matcher(queryString) + .replaceAll('') + .replaceAll('--.*\n?', '') + } } \ No newline at end of file diff --git a/modules/core/test/de/diedavids/cuba/runtimediagnose/db/DbQueryParserSpec.groovy b/modules/core/test/de/diedavids/cuba/runtimediagnose/db/DbQueryParserSpec.groovy index cf09ebf..7224681 100644 --- a/modules/core/test/de/diedavids/cuba/runtimediagnose/db/DbQueryParserSpec.groovy +++ b/modules/core/test/de/diedavids/cuba/runtimediagnose/db/DbQueryParserSpec.groovy @@ -9,6 +9,7 @@ import net.sf.jsqlparser.statement.Statements import net.sf.jsqlparser.statement.drop.Drop import net.sf.jsqlparser.statement.insert.Insert import spock.lang.Specification +import spock.lang.Unroll class DbQueryParserSpec extends Specification { @@ -135,4 +136,69 @@ class DbQueryParserSpec extends Specification { dropStatement instanceof SetStatement dropStatement.toString() == "SET OPTION = VALUE" } + + @Unroll + def "excludeComments excludes single line and multiline comments from SQL query (#type)"() { + given: + def escape = { String str -> str.replace('*', '\\*')} + + when: + String result = sut.excludeComments(query) + + then: + result ==~ /\s*${escape("select * from SEC_USER")}\s*/ + + where: + query << [ +"""-- first comment +select * from SEC_USER""", + +""" +/* multi-line + comment*/ +select * from SEC_USER""", + +""" +/* multi-line + line */ +-- single line comment +select * from SEC_USER"""] + type << ["single line", "multi-line", "mixing"] + } + + @Unroll + def "analyseQueryString exludes comments in JPQL queries (#type)"() { + given: + def localSut = new DbQueryParser() { + @Override + void analyseJpql(String queryString) {} + } + localSut.with { + configuration = runtimeDiagnoseConfiguration + messages = messages + } + + when: + Statements result = localSut.analyseQueryString(query, DiagnoseType.JPQL) + + then: + result.statements[0].toString() == "select * from SEC_USER".toUpperCase() + + where: + query << [ +"""-- first comment +select * from SEC_USER""", + +""" +/* multi-line + comment*/ +select * from SEC_USER""", + +""" +/* multi-line + line */ +-- single line comment +select * from SEC_USER"""] + type << ["single line", "multi-line", "mixing"] + } }