Skip to content

Commit

Permalink
refactor: renamed variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Jun 30, 2022
1 parent 5b23e16 commit 15ee6c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
Expand All @@ -21,7 +20,6 @@
import io.micrometer.core.instrument.Timer;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.util.TablesNamesFinder;

public class SidecarStatement implements Statement {
Expand All @@ -38,7 +36,6 @@ public class SidecarStatement implements Statement {
protected String sql;
private long ttl = Config.TTL_NO_CACHE;
private Optional<ResultSet> resultSet = Optional.empty();
private List<String> tables = Collections.emptyList();

public SidecarStatement(SidecarConnection connection, Statement statement) {
this.connection = connection;
Expand Down Expand Up @@ -72,8 +69,7 @@ protected boolean execute(String sql, Executable executable) throws SQLException

protected boolean execute(Executable executable) throws SQLException {
checkClosed();
parseSQL();
if (resultSet.isPresent()) {
if (get().isPresent()) {
return true;
}
try {
Expand All @@ -99,23 +95,23 @@ protected ResultSet executeQuery(String sql, QueryExecutable executable) throws

protected ResultSet executeQuery(QueryExecutable executable) throws SQLException {
checkClosed();
parseSQL();
if (resultSet.isPresent()) {
return resultSet.get();
Optional<ResultSet> cachedResultSet = get();
if (cachedResultSet.isPresent()) {
return cachedResultSet.get();
}
ResultSet resultSet;
ResultSet backendResultSet;
try {
resultSet = queryTimer.recordCallable(executable::execute);
backendResultSet = queryTimer.recordCallable(executable::execute);
} catch (SQLException e) {
throw e;
} catch (Exception e) {
// Should not happen but rethrow anyway
throw new SQLException(e);
}
if (isCachingEnabled()) {
return cache(resultSet);
return cache(backendResultSet);
}
return resultSet;
return backendResultSet;

}

Expand All @@ -141,26 +137,30 @@ private boolean isCachingEnabled() {
return ttl != Config.TTL_NO_CACHE;
}

protected void parseSQL() {
protected Optional<ResultSet> get() {
String key = key(sql);
net.sf.jsqlparser.statement.Statement statement;
List<String> tables;
try {
statement = CCJSqlParserUtil.parse(sql);
if (statement instanceof Select) {
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
tables = tablesNamesFinder.getTableList((Select) statement);
for (Rule rule : connection.getConfig().getRules()) {
if (rule.getTable() == null || tables.contains(rule.getTable())) {
ttl = rule.getTtl();
}
}
if (isCachingEnabled()) {
resultSet = connection.getCache().get(key);
}
}
net.sf.jsqlparser.statement.Statement statement = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
tables = tablesNamesFinder.getTableList(statement);
} catch (JSQLParserException e) {
log.log(Level.FINE, "Could not parse SQL: " + sql, e);
log.log(Level.FINE, String.format("Could not parse SQL: %s", sql), e);
return Optional.empty();
}
if (tables.isEmpty()) {
return Optional.empty();
}
for (Rule rule : connection.getConfig().getRules()) {
if (rule.getTable() == null || tables.contains(rule.getTable())) {
ttl = rule.getTtl();
}
}
if (isCachingEnabled()) {
resultSet = connection.getCache().get(key);
return resultSet;
}
return Optional.empty();
}

@Override
Expand Down Expand Up @@ -238,11 +238,11 @@ public ResultSet getResultSet() throws SQLException {
if (resultSet.isPresent()) {
return resultSet.get();
}
ResultSet resultSet = statement.getResultSet();
ResultSet backendResultSet = statement.getResultSet();
if (isCachingEnabled()) {
return cache(resultSet);
return cache(backendResultSet);
}
return resultSet;
return backendResultSet;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void setupAll() throws SQLException, IOException {
runScript(backendConnection, "postgres/northwind.sql");
}

// @Test
@Test
void testPreparedStatement() throws Exception {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:redis://localhost:6379");
Expand Down

0 comments on commit 15ee6c4

Please sign in to comment.