Skip to content

Commit

Permalink
feat: add CCJSqlParserUtil.sanitizeSingleSql(String sqlStr) to help…
Browse files Browse the repository at this point in the history
… MyBatikPlus users to clean their statements

Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Apr 18, 2024
1 parent ced6ace commit 1606e5f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
Expand Down Expand Up @@ -525,4 +527,19 @@ public static int getUnbalancedPosition(String text) {
return -1; // Return -1 if all brackets and quotes are balanced
}

public static String sanitizeSingleSql(String sqlStr) {
final Pattern SQL_DELIMITER_SPLIT =
Pattern.compile("((?:'[^']*+'|[^\\n])*+)");
final StringBuilder builder = new StringBuilder();
final Matcher matcher = SQL_DELIMITER_SPLIT.matcher(sqlStr);
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
if (!matcher.group(i).isEmpty()) {
builder.append("\n").append(matcher.group(i));
}
}
}
return builder.toString();
}

}
24 changes: 24 additions & 0 deletions src/test/java/net/sf/jsqlparser/parser/CCJSqlParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.TableStatement;
import net.sf.jsqlparser.test.MemoryLeakVerifier;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -519,4 +520,27 @@ void testParseEmpty() throws JSQLParserException {
assertNull(CCJSqlParserUtil.parse(""));
assertNull(CCJSqlParserUtil.parse((String) null));
}

@Test
void testSingleStatementWithEmptyLines() throws JSQLParserException {
String sqlStr = "update shop_info set title=?,\n"
+ "\n"
+ "\n"
+ "\n"
+ "content='abc\n"
+ "\n"
+ "\n"
+ "\n"
+ "def'\n"
+ "where id=?";

Statement statement = CCJSqlParserUtil.parse(CCJSqlParserUtil.sanitizeSingleSql(sqlStr));
TestUtils.assertStatementCanBeDeparsedAs(statement, "update shop_info set title=?,\n"
+ "content='abc\n"
+ "\n"
+ "\n"
+ "\n"
+ "def'\n"
+ "where id=?", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
*/
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.*;
import net.sf.jsqlparser.parser.*;
import net.sf.jsqlparser.test.*;
import org.junit.jupiter.api.*;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class StatementSeparatorTest {

Expand Down

0 comments on commit 1606e5f

Please sign in to comment.