All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.query.QueryExpression Maven / Gradle / Ivy

There is a newer version: 1.25
Show newest version
package com.github.siwenyan.query;

import com.github.siwenyan.common.InvalidExpressionException;
import com.github.siwenyan.common.StringTools;
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<>();

    static {
        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);
            }
        });
        putFactory("js", new IQueryFactory() {
            @Override
            public QueryBase createQuery(String source) {
                return new JsQuery();
            }
        });
    }

    public static IQueryFactory putFactory(String type, IQueryFactory factory) {
        return factories.put(type, factory);
    }

    private final IQueryFactory factory;
    private final String query;

    public QueryExpression(String queryExpression) throws InvalidExpressionException {
        if (StringTools.isEmpty(queryExpression)) {
            throw new InvalidExpressionException("Empty expression: " + queryExpression);
        }
        queryExpression = queryExpression.trim();
        String[] p = queryExpression.split("\\s+", 2);
        factory = factories.get(p[0].trim());
        if (null == factory) {
            throw new InvalidExpressionException("Unknown query type: " + queryExpression);
        }
        this.query = p[1].trim();
    }

    public QueryBase compile(String source) {
        return factory.createQuery(source);
    }

    public String getQuery() {
        return this.query;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy