Skip to content

Commit

Permalink
[FEATURE] Ignore parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemick committed Feb 1, 2025
1 parent 951a1fb commit 78d5de3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.mickaelb</groupId>
<artifactId>hibernate-query-asserts</artifactId>
<version>2.1.0-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>A library that can assert statement generated by Hibernate in Spring tests</description>
Expand Down Expand Up @@ -49,7 +49,7 @@
<maven.compiler.target>11</maven.compiler.target>
<spring-boot-starter-test.version>2.6.6</spring-boot-starter-test.version>
<hibernate-core-jakarta.version>5.6.7.Final</hibernate-core-jakarta.version>
<jsqlparser.version>4.4</jsqlparser.version>
<jsqlparser.version>5.1</jsqlparser.version>
<junit-jupiter-engine.version>5.8.2</junit-jupiter-engine.version>
<mockito-junit-jupiter.version>4.4.0</mockito-junit-jupiter.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitorAdapter;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
Expand All @@ -15,29 +14,17 @@ public class JSQLHibernateStatementParser implements HibernateStatementParser {
public void parseSqlStatement(String sql, HibernateStatementListener statementCountListener) {
try {
Statement statement = CCJSqlParserUtil.parse(sql);
statement.accept(new StatementVisitorAdapter() {
@Override
public void visit(Select select) {
statementCountListener.notifySelectStatement(sql);
}

@Override
public void visit(Insert insert) {
statementCountListener.notifyInsertStatement(sql);
}

@Override
public void visit(Update update) {
statementCountListener.notifyUpdateStatement(sql);
}

@Override
public void visit(Delete delete) {
statementCountListener.notifyDeleteStatement(sql);
}
});
} catch (JSQLParserException e) {
throw new RuntimeException(e);
if (statement instanceof Delete) {
statementCountListener.notifyDeleteStatement(sql);
} else if (statement instanceof Insert) {
statementCountListener.notifyInsertStatement(sql);
} else if (statement instanceof Select) {
statementCountListener.notifySelectStatement(sql);
} else if (statement instanceof Update) {
statementCountListener.notifyUpdateStatement(sql);
}
} catch (JSQLParserException ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.description;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class JSQLHibernateStatementParserTest {
Expand Down Expand Up @@ -45,4 +44,11 @@ public void _parseSqlStatement_delete() {

verify(hibernateStatementListener, description("the correct notifier is called")).notifyDeleteStatement("DELETE FROM Post");
}

@Test
public void _incorrect_statement_should_not_throw() {
model.parseSqlStatement("This is not really SQL", hibernateStatementListener);

verifyNoInteractions(hibernateStatementListener);
}
}

0 comments on commit 78d5de3

Please sign in to comment.