-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduced ParsedSQL class to hold statement metadata
- Loading branch information
Julien Ruaux
committed
May 27, 2022
1 parent
504d419
commit 37742bd
Showing
2 changed files
with
70 additions
and
30 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
subprojects/redis-sidecar-jdbc/src/main/java/com/redis/sidecar/jdbc/ParsedSQL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters