com.github.siwenyan.query.QueryExpression Maven / Gradle / Ivy
package com.github.siwenyan.query;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.Map;
public class QueryExpression {
private static Logger log = Logger.getLogger(QueryExpression.class);
private static Map factories = new HashMap<>();
{
putFactory("line", new IQueryFactory() {
@Override
public QueryBase createQuery(String source) {
return QueryBase.textSource(source);
}
});
putFactory("jsonpath", new IQueryFactory() {
@Override
public QueryBase createQuery(String source) {
return QueryBase.jsonpathSource(source);
}
});
putFactory("mybatis", new IQueryFactory() {
@Override
public QueryBase createQuery(String source) {
return QueryBase.mybatisSource(source);
}
});
}
public static IQueryFactory putFactory(String type, IQueryFactory factory) {
return factories.put(type, factory);
}
private String type;
private String query;
public QueryExpression(String queryExpression) {
String[] p = queryExpression.trim().split("\\s+", 2);
this.type = p[0].trim();
this.query = p[1].trim();
}
public QueryBase compile(String source) {
IQueryFactory factory = factories.get(getType());
if (null == factory) {
return null;
} else {
return factory.createQuery(source);
}
}
public String getType() {
return this.type;
}
public String getQuery() {
return this.query;
}
}