Skip to content

Commit

Permalink
Revert "Revert "Add comment support to JPQL console (#16)" (#17)"
Browse files Browse the repository at this point in the history
This reverts commit 4cd1c08
  • Loading branch information
mariodavid committed Nov 29, 2017
1 parent 4cd1c08 commit 1a8a14d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Binary file modified img/sql-console-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -65,7 +66,8 @@ class DbQueryParser {


if (DiagnoseType.JPQL == diagnoseType) {
analyseJpql(queryString)
String queryStringWithoutComments = excludeComments(queryString)
analyseJpql(queryStringWithoutComments)
}

statements
Expand Down Expand Up @@ -109,4 +111,11 @@ class DbQueryParser {

containsIllegalOperation
}

String excludeComments(String queryString) {
Pattern.compile('/\\*.*?\\*/', Pattern.DOTALL)
.matcher(queryString)
.replaceAll('')
.replaceAll('--.*\n?', '')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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"]
}
}

0 comments on commit 1a8a14d

Please sign in to comment.