Skip to content

Commit

Permalink
feat: add a method checking balanced brackets
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Jul 12, 2023
1 parent c3ffb86 commit 52df32d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -439,4 +440,43 @@ public static int getNestingDepth(String sql) {
}
return maxlevel;
}

public static int getUnbalancedPosition(String text) {
Stack<Character> stack = new Stack<>();
boolean insideQuote = false;

for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '"' || c == '\'') {
if (!insideQuote) {
stack.push(c); // Add quote to stack
} else if (stack.peek() == c) {
stack.pop(); // Matching quote found, remove from stack
}
insideQuote = !insideQuote; // Toggle insideQuote flag
} else if (!insideQuote && (c == '(' || c == '[' || c == '{')) {
stack.push(c); // Add opening bracket to stack
} else if (!insideQuote && (c == ')' || c == ']' || c == '}')) {
if (stack.isEmpty()) {
return i; // Return position of unbalanced closing bracket
}
char top = stack.pop();
if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) {
return i; // Return position of unbalanced closing bracket
}
}
}

if (!stack.isEmpty()) {
char unbalanced = stack.peek();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == unbalanced) {
return i; // Return position of unbalanced opening bracket or quote
}
}
}

return -1; // Return -1 if all brackets and quotes are balanced
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,13 @@ public void execute() throws Throwable {
executorService.shutdownNow();
CCJSqlParserUtil.LOGGER.setLevel(Level.OFF);
}

@Test
void testUnbalancedPosition() {
String sqlStr = "SELECT * from ( test ";
sqlStr = "select\n" +
" concat('{','\"dffs\":\"',if(dffs is null,'',cast(dffs as string),'\",\"djr\":\"',if(djr is null,'',cast(djr as string),'\",\"djrq\":\"',if(djrq is null,'',cast(djrq as string),'\",\"thjssj\":\"',if(thjssj is null,'',cast(thjssj as string),'\",\"thkssj\":\"',if(thkssj is null,'',cast(thkssj as string),'\",\"sjc\":\"',if(sjc is null,'',cast(sjc as string),'\",\"ldhm\":\"',if(ldhm is null,'',cast(ldhm as string),'\",\"lxdh\":\"',if(lxdh is null,'',cast(lxdh as string),'\",\"md\":\"',if(md is null,'',cast(md as string),'\",\"nr\":\"',if(nr is null,'',cast(nr as string),'\",\"nrfl\":\"',if(nrfl is null,'',cast(nrfl as string),'\",\"nrwjid\":\"',if(nrwjid is null,'',cast(nrwjid as string),'\",\"sfbm\":\"',if(sfbm is null,'',cast(sfbm as string),'\",\"sjly\":\"',if(sjly is null,'',cast(sjly as string),'\",\"wtsd\":\"',if(wtsd is null,'',cast(wtsd as string),'\",\"xb\":\"',if(xb is null,'',cast(xb as string),'\",\"xfjbh\":\"',if(xfjbh is null,'',cast(xfjbh as string),'\",\"xfjid\":\"',if(xfjid is null,'',cast(xfjid as string),'\",\"xm\":\"',if(xm is null,'',cast(xm as string),'\",\"zhut\":\"',if(zhut is null,'',cast(zhut as string),'\",\"zt\":\"',if(zt is null,'',cast(zt as string),'\"}')\n" +
" from tab";
assertEquals(-1 , CCJSqlParserUtil.getUnbalancedPosition(sqlStr));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5672,11 +5672,10 @@ void testParameterMultiPartName() throws JSQLParserException {
String sqlStr = "SELECT 1 FROM dual WHERE a = :paramMap.aValue";
PlainSelect select = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);

assertTrue(select
assertEquals("paramMap.aValue", select
.getWhere(EqualsTo.class)
.getRightExpression(JdbcNamedParameter.class)
.getName()
.equals("paramMap.aValue"));
.getName());
}

@Test
Expand Down

0 comments on commit 52df32d

Please sign in to comment.