REQ-2135: refactor SimpleAnalyze

This commit is contained in:
yanglin 2024-04-03 09:39:52 +08:00
parent 289202ba85
commit 004207b249
2 changed files with 13 additions and 7 deletions

View File

@ -174,7 +174,7 @@ public class TodoRangeQueryService {
result.addAnalysis("eval", () -> {
String sql = execSQL.get();
if (sql == null || analyzeItem == null) return null;
return new SimpleAnalyzer().analyze(sql, analyzeItem);
return new SimpleAnalyzer(sql).analyze(analyzeItem);
});
return result;
} finally {

View File

@ -16,9 +16,16 @@ import javax.annotation.Nullable;
@Slf4j
public class SimpleAnalyzer {
public AnalyzeResult analyze(String sql, Object value) {
private final SQLExpr whereExpr;
public SimpleAnalyzer(String sql) {
SQLStatement stmt = new MySqlStatementParser(sql).parseSelect();
this.whereExpr = findWhereExpr(stmt);
}
public AnalyzeResult analyze(Object value) {
try {
return analyzeImpl(sql, value);
return analyzeImpl(value);
} catch (Exception e) {
log.warn("Analyze error", e);
AnalyzeResult result = new AnalyzeResult();
@ -27,14 +34,13 @@ public class SimpleAnalyzer {
}
}
private AnalyzeResult analyzeImpl(String sql, Object value) {
SQLStatement stmt = new MySqlStatementParser(sql).parseSelect();
private AnalyzeResult analyzeImpl(Object value) {
AnalyzeResult result = new AnalyzeResult();
SQLExpr whereExpr = findWhereExpr(stmt);
if (whereExpr == null) {
if (this.whereExpr == null) {
result.setWarn("Can't find where expr!");
return result;
}
SQLExpr whereExpr = this.whereExpr.clone();
Record record = new Record(value);
whereExpr.accept(EvalTypeVisitor.INSTANCE);
whereExpr.accept(new EvalValueVisitor(record));