Skip to content

Commit

Permalink
feat: Introduced ParsedSQL class to hold statement metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed May 27, 2022
1 parent 504d419 commit 37742bd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.redis.sidecar.jdbc;

import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.util.TablesNamesFinder;

public class ParsedSQL {

private static final Logger log = Logger.getLogger(ParsedSQL.class.getName());

private final String sql;
private final List<String> tables;

public ParsedSQL(String sql) {
this(sql, Collections.emptyList());
}

public ParsedSQL(String sql, List<String> tables) {
this.sql = sql;
this.tables = tables;
}

public String getSQL() {
return sql;
}

public List<String> getTables() {
return tables;
}

public static ParsedSQL parse(String sql) {
try {
Statement statement = CCJSqlParserUtil.parse(sql);
if (statement instanceof Select) {
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableNames = tablesNamesFinder.getTableList((Select) statement);
return new ParsedSQL(sql, tableNames);
}
} catch (JSQLParserException e) {
log.log(Level.FINE, "Could not parse sql: " + sql, e);
}
return new ParsedSQL(sql);

}
}
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.List;
import java.util.concurrent.Callable;
import java.util.zip.CRC32;

Expand All @@ -17,10 +16,6 @@

import io.micrometer.core.instrument.Metrics;
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 @@ -30,7 +25,7 @@ public class SidecarStatement implements Statement {
private final Timer requestTimer = Metrics.timer("requests");
private final Timer queryTimer = Metrics.timer("queries");

protected String sql;
protected ParsedSQL parsedSQL;
private long ttl;
protected ResultSet resultSet;

Expand All @@ -41,7 +36,7 @@ public SidecarStatement(SidecarConnection connection, Statement statement) {

protected SidecarStatement(SidecarConnection connection, Statement statement, String sql) {
this(connection, statement);
setSQL(sql);
parseSQL(sql);
}

@Override
Expand All @@ -64,7 +59,7 @@ protected <T> T recordQuery(Callable<T> callable) throws SQLException {
}

private ResultSet doExecuteQuery(String sql) throws SQLException {
setSQL(sql);
parseSQL(sql);
this.resultSet = get();
if (this.resultSet == null) {
ResultSet databaseResultSet = recordDatabase(() -> statement.executeQuery(sql));
Expand Down Expand Up @@ -95,7 +90,7 @@ protected ResultSet get() throws SQLException {
}

protected String key() {
return connection.getConfig().key(KEY_PREFIX, crc(sql));
return connection.getConfig().key(KEY_PREFIX, crc(parsedSQL.getSQL()));
}

protected final String crc(String string) {
Expand All @@ -115,24 +110,17 @@ protected ResultSet cache(ResultSet resultSet) throws SQLException {
return rowSet;
}

private void setSQL(String sql) {
this.sql = sql;
this.ttl = ttl(sql);
private void parseSQL(String sql) {
this.parsedSQL = ParsedSQL.parse(sql);
this.ttl = ttl(parsedSQL);
}

private long ttl(String sql) {
net.sf.jsqlparser.statement.Statement statement;
try {
statement = CCJSqlParserUtil.parse(sql);
} catch (JSQLParserException e) {
return Config.TTL_NO_CACHE;
}
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
for (Rule rule : connection.getConfig().getRules()) {
if (rule.getTable() == null || tableList.contains(rule.getTable())) {
return rule.getTtl();
private long ttl(ParsedSQL parsedSQL) {
if (!parsedSQL.getTables().isEmpty()) {
for (Rule rule : connection.getConfig().getRules()) {
if (rule.getTable() == null || parsedSQL.getTables().contains(rule.getTable())) {
return rule.getTtl();
}
}
}
return Config.TTL_NO_CACHE;
Expand Down Expand Up @@ -210,7 +198,7 @@ public boolean execute(String sql) throws SQLException {
}

private boolean doExecute(String sql) throws SQLException {
setSQL(sql);
parseSQL(sql);
resultSet = get();
if (resultSet == null) {
recordDatabase(() -> statement.execute(sql));
Expand Down Expand Up @@ -304,7 +292,7 @@ public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException
}

private void setSQLNoCache(String sql) {
this.sql = sql;
this.parsedSQL = new ParsedSQL(sql);
this.ttl = Config.TTL_NO_CACHE;
}

Expand All @@ -326,7 +314,7 @@ public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
}

private boolean doExecute(String sql, int autoGeneratedKeys) throws SQLException {
setSQL(sql);
parseSQL(sql);
resultSet = get();
if (resultSet == null) {
return recordDatabase(() -> statement.execute(sql, autoGeneratedKeys));
Expand All @@ -340,7 +328,7 @@ public boolean execute(String sql, int[] columnIndexes) throws SQLException {
}

private boolean doExecute(String sql, int[] columnIndexes) throws SQLException {
setSQL(sql);
parseSQL(sql);
resultSet = get();
if (resultSet == null) {
return recordDatabase(() -> statement.execute(sql, columnIndexes));
Expand All @@ -354,7 +342,7 @@ public boolean execute(String sql, String[] columnNames) throws SQLException {
}

private boolean doExecute(String sql, String[] columnNames) throws SQLException {
setSQL(sql);
parseSQL(sql);
resultSet = get();
if (resultSet == null) {
return recordDatabase(() -> statement.execute(sql, columnNames));
Expand Down

0 comments on commit 37742bd

Please sign in to comment.