Search in sources :

Example 11 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.

the class Mongo method findTop.

/**
 * @todo 查询前多少条记录
 * @param topSize
 * @return
 */
public List<?> findTop(final Float topSize) {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    try {
        QueryExecutorExtend extend = queryExecutor.getInnerModel();
        // 最后的执行语句
        String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig));
        return findTop(getMongoTemplate(), sqlToyConfig, topSize, realMql, (Class) extend.resultType, extend.humpMapLabel);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException(e);
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 12 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.

the class Mongo method findPage.

/**
 * @todo 分页查询
 * @param page
 * @return
 */
public Page findPage(Page page) {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    try {
        QueryExecutorExtend extend = queryExecutor.getInnerModel();
        // 最后的执行语句
        String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig));
        return findPage(getMongoTemplate(), sqlToyConfig, page, realMql, (Class) extend.resultType, extend.humpMapLabel);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException(e);
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 13 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.

the class ElasticSearchPlugin method executeQuery.

/**
 * @todo 执行实际查询处理
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param jsonQuery
 * @param resultClass
 * @param humpMapLabel
 * @return
 * @throws Exception
 */
private static DataSetResult executeQuery(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject jsonQuery, Class resultClass, boolean humpMapLabel) throws Exception {
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    ElasticEndpoint esConfig = sqlToyContext.getElasticEndpoint(noSqlModel.getEndpoint());
    String source = "_source";
    // 是否设置了fields
    boolean hasFields = false;
    if (jsonQuery.containsKey(source)) {
        hasFields = true;
    } else if (jsonQuery.containsKey(source.toUpperCase())) {
        hasFields = true;
        source = source.toUpperCase();
    }
    String[] fields = null;
    if (noSqlModel.getFields() != null) {
        fields = noSqlModel.getFields();
        // 没有设置显示字段,且是非聚合查询时将配置的fields设置到查询json中
        if (!hasFields && !noSqlModel.isHasAggs()) {
            JSONArray array = new JSONArray();
            int aliasIndex;
            for (String field : fields) {
                aliasIndex = field.indexOf(":");
                if (aliasIndex == -1) {
                    array.add(field);
                } else {
                    array.add(field.substring(0, aliasIndex).trim());
                }
            }
            jsonQuery.fluentPut("_source", array);
        }
    } else if (hasFields) {
        Object[] array = (Object[]) jsonQuery.getJSONArray(source).toArray();
        fields = new String[array.length];
        for (int i = 0; i < fields.length; i++) {
            fields[i] = array[i].toString();
        }
    } else if (resultClass != null) {
        Class superClass = resultClass.getSuperclass();
        if (!resultClass.equals(ArrayList.class) && !resultClass.equals(List.class) && !resultClass.equals(Array.class) && !resultClass.equals(Collection.class) && !resultClass.equals(HashMap.class) && !resultClass.equals(Map.class) && !resultClass.equals(ConcurrentMap.class) && !resultClass.equals(ConcurrentHashMap.class) && !HashMap.class.equals(superClass) && !Map.class.equals(superClass) && !LinkedHashMap.class.equals(superClass) && !ConcurrentHashMap.class.equals(superClass)) {
            fields = BeanUtil.matchSetMethodNames(resultClass);
        }
    }
    if (sqlToyContext.isDebug()) {
        if (logger.isDebugEnabled()) {
            logger.debug("execute elastic eql=" + jsonQuery.toJSONString());
        } else {
            System.out.println("execute elastic eql=" + jsonQuery.toJSONString());
        }
    }
    // 执行请求
    JSONObject json = HttpClientUtils.doPost(sqlToyContext, noSqlModel, esConfig, jsonQuery);
    if (json == null || json.isEmpty()) {
        return new DataSetResult();
    }
    DataSetResult resultSet = ElasticSearchUtils.extractFieldValue(sqlToyContext, sqlToyConfig, json, fields);
    MongoElasticUtils.processTranslate(sqlToyContext, sqlToyConfig, resultSet.getRows(), resultSet.getLabelNames());
    // 不支持指定查询集合的行列转换
    boolean changedCols = ResultUtils.calculate(sqlToyContext.getDesensitizeProvider(), sqlToyConfig, resultSet, null, null);
    // 将结果数据映射到具体对象类型中
    resultSet.setRows(ResultUtils.wrapQueryResult(sqlToyContext, resultSet.getRows(), StringUtil.humpFieldNames(resultSet.getLabelNames()), resultClass, changedCols, humpMapLabel, false, null, null));
    return resultSet;
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JSONArray(com.alibaba.fastjson.JSONArray) ConcurrentMap(java.util.concurrent.ConcurrentMap) ElasticEndpoint(org.sagacity.sqltoy.config.model.ElasticEndpoint) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) ElasticEndpoint(org.sagacity.sqltoy.config.model.ElasticEndpoint) LinkedHashMap(java.util.LinkedHashMap) Array(java.lang.reflect.Array) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject)

Example 14 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.

the class SqlXMLConfigParse method parseNoSql.

/**
 * @todo 解析nosql的相关配置
 * @param sqlToyConfig
 * @param sqlElt
 * @param local
 */
private static void parseNoSql(SqlToyConfig sqlToyConfig, Element sqlElt, String local) {
    NoSqlConfigModel noSqlConfig = new NoSqlConfigModel();
    NodeList nodeList;
    // mongo 的collection
    if (sqlElt.hasAttribute("collection")) {
        noSqlConfig.setCollection(sqlElt.getAttribute("collection"));
    }
    // url应该是一个变量如:${es_url}
    if (sqlElt.hasAttribute("url")) {
        noSqlConfig.setEndpoint(SqlToyConstants.replaceParams(sqlElt.getAttribute("url")));
    } else if (sqlElt.hasAttribute("end-point")) {
        noSqlConfig.setEndpoint(SqlToyConstants.replaceParams(sqlElt.getAttribute("end-point")));
    } else if (sqlElt.hasAttribute("endpoint")) {
        noSqlConfig.setEndpoint(SqlToyConstants.replaceParams(sqlElt.getAttribute("endpoint")));
    }
    // 索引
    if (sqlElt.hasAttribute("index")) {
        noSqlConfig.setIndex(sqlElt.getAttribute("index"));
    }
    // es 索引类型
    if (sqlElt.hasAttribute("type")) {
        noSqlConfig.setType(sqlElt.getAttribute("type"));
    }
    // 请求超时时间(单位毫秒)
    if (sqlElt.hasAttribute("request-timeout")) {
        noSqlConfig.setRequestTimeout(Integer.parseInt(sqlElt.getAttribute("request-timeout")));
    }
    // 连接超时时间(单位毫秒)
    if (sqlElt.hasAttribute("connection-timeout")) {
        noSqlConfig.setConnectTimeout(Integer.parseInt(sqlElt.getAttribute("connection-timeout")));
    }
    // 整个请求超时时长(毫秒)
    if (sqlElt.hasAttribute("socket-timeout")) {
        noSqlConfig.setSocketTimeout(Integer.parseInt(sqlElt.getAttribute("socket-timeout")));
    }
    // url请求字符集类型
    if (sqlElt.hasAttribute("charset")) {
        noSqlConfig.setCharset(sqlElt.getAttribute("charset"));
    }
    // fields
    if (sqlElt.hasAttribute("fields")) {
        if (StringUtil.isNotBlank(sqlElt.getAttribute("fields"))) {
            noSqlConfig.setFields(splitFields(sqlElt.getAttribute("fields")));
        }
    } else {
        nodeList = sqlElt.getElementsByTagName(local.concat("fields"));
        if (nodeList.getLength() > 0) {
            noSqlConfig.setFields(splitFields(nodeList.item(0).getTextContent()));
        }
    }
    // valueRoot
    if (sqlElt.hasAttribute("value-root")) {
        noSqlConfig.setValueRoot(trimParams(sqlElt.getAttribute("value-root").split("\\,")));
    } else if (sqlElt.hasAttribute("value-path")) {
        noSqlConfig.setValueRoot(trimParams(sqlElt.getAttribute("value-path").split("\\,")));
    }
    String nodeName = sqlElt.getNodeName().toLowerCase();
    // 是否有聚合查询
    if (nodeName.equals("eql")) {
        if (sqlElt.hasAttribute("aggregate")) {
            noSqlConfig.setHasAggs(Boolean.parseBoolean(sqlElt.getAttribute("aggregate")));
        } else if (sqlElt.hasAttribute("is-aggregate")) {
            noSqlConfig.setHasAggs(Boolean.parseBoolean(sqlElt.getAttribute("is-aggregate")));
        } else {
            noSqlConfig.setHasAggs(StringUtil.matches(sqlToyConfig.getSql(null), ES_AGGS_PATTERN));
        }
        // 判断查询语句的模式是否sql模式
        if (StringUtil.matches(sqlToyConfig.getSql(null), "(?i)\\s*select\\s*") && sqlToyConfig.getSql(null).toLowerCase().indexOf("from") > 0) {
            noSqlConfig.setSqlMode(true);
            // sql模式下存在group by 则判定为聚合查询
            if (StringUtil.matches(sqlToyConfig.getSql(null), GROUP_BY_PATTERN)) {
                noSqlConfig.setHasAggs(true);
            }
        }
    } else if (nodeName.equals("mql")) {
        if (sqlElt.hasAttribute("aggregate")) {
            noSqlConfig.setHasAggs(Boolean.parseBoolean(sqlElt.getAttribute("aggregate")));
        } else if (sqlElt.hasAttribute("is-aggregate")) {
            noSqlConfig.setHasAggs(Boolean.parseBoolean(sqlElt.getAttribute("is-aggregate")));
        } else {
            noSqlConfig.setHasAggs(StringUtil.matches(sqlToyConfig.getSql(null), MONGO_AGGS_PATTERN));
        }
    }
    sqlToyConfig.setNoSqlConfigModel(noSqlConfig);
    // nosql参数解析模式不同于sql
    if (!noSqlConfig.isSqlMode()) {
        sqlToyConfig.setParamsName(SqlConfigParseUtils.getNoSqlParamsName(sqlToyConfig.getSql(null), true));
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) NodeList(org.w3c.dom.NodeList)

Aggregations

NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)14 JSONObject (com.alibaba.fastjson.JSONObject)7 JSONArray (com.alibaba.fastjson.JSONArray)5 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ElasticEndpoint (org.sagacity.sqltoy.config.model.ElasticEndpoint)4 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)4 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)4 DataSetResult (org.sagacity.sqltoy.model.DataSetResult)3 DataSetResult (org.sagacity.sqltoy.model.inner.DataSetResult)3 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 File (java.io.File)1 Array (java.lang.reflect.Array)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 NoSqlFieldsModel (org.sagacity.sqltoy.config.model.NoSqlFieldsModel)1