Skip to content

Commit

Permalink
feat: Added graceful handling of parsing failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Feb 14, 2023
1 parent 5be9eab commit 47756db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public String getSql() {
}

public Stream<Table> getTables() throws ParsingException {
if (statement == null) {
return Stream.empty();
}
return statement.accept(DepthFirstVisitor.by(new TableVisitor()), null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
import javax.sql.rowset.RowSetFactory;

import com.redis.smartcache.core.Query;
import com.redis.smartcache.core.RowSetCache;
import com.redis.smartcache.core.QueryRuleSession;
import com.redis.smartcache.core.RowSetCache;

import io.micrometer.core.instrument.MeterRegistry;
import io.trino.sql.parser.ParsingException;
import io.trino.sql.parser.ParsingOptions;
import io.trino.sql.parser.SqlParser;

public class SmartConnection implements Connection {

private static final ParsingOptions PARSING_OPTIONS = new ParsingOptions();

private final SqlParser parser = new SqlParser();
private final ParsingOptions options = new ParsingOptions();
private final Map<String, Query> queryCache = new HashMap<>();

private final Connection connection;
Expand Down Expand Up @@ -354,11 +356,20 @@ public Query getQuery(String sql) {
if (queryCache.containsKey(key)) {
return queryCache.get(key);
}
Query query = new Query(key, sql, parser.createStatement(sql, options));
Query query = new Query(key, sql, parse(sql));
queryCache.put(key, query);
return query;
}

private io.trino.sql.tree.Statement parse(String sql) {
try {
return parser.createStatement(sql, PARSING_OPTIONS);
} catch (ParsingException e) {
// Fail gracefully
return null;
}
}

public static String crc32(String string) {
CRC32 crc = new CRC32();
crc.update(string.getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
Expand Down Expand Up @@ -82,6 +83,29 @@ void testCallableStatementParams(RedisTestContext redis) throws Exception {
}
}

@ParameterizedTest
@RedisTestContextsSource
void testCallableStatementRefCursor(RedisTestContext redis) throws Exception {
String runFunction = "{? = call getUsers()}";
try (Connection connection = connection(POSTGRESQL, redis);
Statement statement = connection.createStatement();
CallableStatement cs = connection.prepareCall(runFunction)) {
// We must be inside a transaction for cursors to work.
connection.setAutoCommit(false);
// register output
cs.registerOutParameter(1, Types.REF_CURSOR);
// run function
cs.execute();
// get refcursor and convert it to ResultSet
ResultSet resultSet = (ResultSet) cs.getObject(1);
while (resultSet.next()) {
Assertions.assertEquals("test", resultSet.getString("usename"));
Assertions.assertEquals("********", resultSet.getString("passwd"));
}

}
}

@ParameterizedTest
@RedisTestContextsSource
void testCallableStatementGetResultSet(RedisTestContext redis) throws Exception {
Expand Down

0 comments on commit 47756db

Please sign in to comment.