Skip to content

Commit

Permalink
sql field comment semicolon with escape
Browse files Browse the repository at this point in the history
  • Loading branch information
sjgllgh committed Nov 15, 2023
1 parent 2b47014 commit 6c2f29c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,32 @@ trait CommentHelper {
object SQLCommentHelper extends CommentHelper {
override val commentPattern: Regex = """\s*--.+\s*""".r.unanchored
private val comment = "(?ms)('(?:''|[^'])*')|--.*?$|/\\*.*?\\*/|#.*?$|"
private val comment_sem = "(?i)(comment)\\s+'([^']*)'"
private val logger: Logger = LoggerFactory.getLogger(getClass)

def replaceComment(code: String): String = {
try {
val pattern = Pattern.compile(comment_sem)
val matcher = pattern.matcher(code)
val sb = new StringBuffer
while (matcher.find()) {
val commentKeyword = matcher.group(1)
val comment = matcher.group(2)
val escapedComment = comment.replaceAll(";", "\\\\\\\\;")
matcher.appendReplacement(sb, commentKeyword + " '" + escapedComment + "'")
}
matcher.appendTail(sb)
sb.toString
} catch {
case e: Exception =>
logger.warn("sql comment semicolon replace failed")
code
case t: Throwable =>
logger.warn("sql comment semicolon replace failed")
code
}
}

override def dealComment(code: String): String = {
try {
val p = Pattern.compile(comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ object SQLExplain extends Explain {
logAppender: java.lang.StringBuilder
): Unit = {
val fixedCode: ArrayBuffer[String] = new ArrayBuffer[String]()
val tempCode = SQLCommentHelper.dealComment(executionCode)
val tempCode1 = SQLCommentHelper.dealComment(executionCode)
val tempCode = SQLCommentHelper.replaceComment(tempCode1)
val isNoLimitAllowed = Utils.tryCatch {
IDE_ALLOW_NO_LIMIT_REGEX.findFirstIn(executionCode).isDefined
} { case e: Exception =>
Expand Down

0 comments on commit 6c2f29c

Please sign in to comment.