Search in sources :

Example 6 with DataSetResult

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

the class ElasticSearchPlugin method findTop.

/**
 * @todo 提取符合条件的前多少条记录
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param queryExecutor
 * @param topSize
 * @return
 * @throws Exception
 */
public static List findTop(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Integer topSize) throws Exception {
    String realMql = MongoElasticUtils.wrapES(sqlToyConfig, queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig)).trim();
    JSONObject jsonQuery = JSON.parseObject(realMql);
    if (topSize != null) {
        jsonQuery.fluentRemove("from");
        jsonQuery.fluentRemove("FROM");
        jsonQuery.fluentRemove("size");
        jsonQuery.fluentRemove("SIZE");
        jsonQuery.fluentPut("from", 0);
        jsonQuery.fluentPut("size", topSize);
    }
    if (sqlToyContext.isDebug()) {
        out.println("execute eql={" + jsonQuery.toJSONString() + "}");
    }
    DataSetResult result = executeQuery(sqlToyContext, sqlToyConfig, jsonQuery, queryExecutor.getResultTypeName());
    return result.getRows();
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) DataSetResult(org.sagacity.sqltoy.model.DataSetResult)

Example 7 with DataSetResult

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

the class ElasticSearchPlugin method findPage.

/**
 * @todo 基于es的分页查询
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param pageModel
 * @param queryExecutor
 * @return
 * @throws Exception
 */
public static PaginationModel findPage(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, PaginationModel pageModel, QueryExecutor queryExecutor) throws Exception {
    String realMql = MongoElasticUtils.wrapES(sqlToyConfig, queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig)).trim();
    JSONObject jsonQuery = JSON.parseObject(realMql);
    jsonQuery.fluentRemove("from");
    jsonQuery.fluentRemove("FROM");
    jsonQuery.fluentRemove("size");
    jsonQuery.fluentRemove("SIZE");
    jsonQuery.fluentPut("from", (pageModel.getPageNo() - 1) * pageModel.getPageSize());
    jsonQuery.fluentPut("size", pageModel.getPageSize());
    if (sqlToyContext.isDebug()) {
        out.println("execute eql={" + jsonQuery.toJSONString() + "}");
    }
    PaginationModel page = new PaginationModel();
    page.setPageNo(pageModel.getPageNo());
    page.setPageSize(pageModel.getPageSize());
    DataSetResult result = executeQuery(sqlToyContext, sqlToyConfig, jsonQuery, queryExecutor.getResultTypeName());
    page.setRows(result.getRows());
    page.setRecordCount(result.getTotalCount());
    return page;
}
Also used : PaginationModel(org.sagacity.sqltoy.model.PaginationModel) JSONObject(com.alibaba.fastjson.JSONObject) DataSetResult(org.sagacity.sqltoy.model.DataSetResult)

Example 8 with DataSetResult

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

the class ElasticSearchUtils method executeQuery.

/**
 * @todo 执行实际查询处理
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param noSqlModel
 * @param jsonQuery
 * @param resultClass
 * @return
 * @throws Exception
 */
public static DataSetResult executeQuery(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, String sql, String resultClass) throws Exception {
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    // 执行请求并返回json结果
    JSONObject json = HttpClientUtils.doPost(sqlToyContext, noSqlModel, sql);
    if (json == null || json.isEmpty()) {
        return new DataSetResult();
    }
    String[] fields = noSqlModel.getFields();
    if (fields == null && resultClass != null) {
        if (!CollectionUtil.any(resultClass.toLowerCase(), new String[] { "map", "hashmap", "linkedhashmap", "linkedmap" }, false)) {
            fields = BeanUtil.matchSetMethodNames(Class.forName(resultClass));
        }
    }
    DataSetResult resultSet = extractFieldValue(sqlToyContext, sqlToyConfig, json, fields);
    MongoElasticUtils.processTranslate(sqlToyContext, sqlToyConfig, resultSet.getRows(), resultSet.getLabelNames());
    // 不支持指定查询集合的行列转换
    ResultUtils.calculate(sqlToyConfig, resultSet, null, sqlToyContext.isDebug());
    // 将结果数据映射到具体对象类型中
    resultSet.setRows(MongoElasticUtils.wrapResultClass(resultSet.getRows(), resultSet.getLabelNames(), resultClass));
    return resultSet;
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) JSONObject(com.alibaba.fastjson.JSONObject) DataSetResult(org.sagacity.sqltoy.model.DataSetResult)

Example 9 with DataSetResult

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

the class Mongo method extractFieldValues.

private List extractFieldValues(SqlToyConfig sqlToyConfig, Iterator<Document> iter, String resultClass) throws Exception {
    List resultSet = new ArrayList();
    Document row;
    String[] fields = sqlToyConfig.getNoSqlConfigModel().getFields();
    // 解决field采用name:aliasName形式
    String[] realFields = new String[fields.length];
    String[] translateFields = new String[fields.length];
    System.arraycopy(fields, 0, realFields, 0, fields.length);
    System.arraycopy(fields, 0, translateFields, 0, fields.length);
    int aliasIndex = 0;
    for (int i = 0; i < realFields.length; i++) {
        aliasIndex = realFields[i].indexOf(":");
        if (aliasIndex != -1) {
            realFields[i] = realFields[i].substring(0, aliasIndex).trim();
            translateFields[i] = translateFields[i].substring(aliasIndex + 1).trim();
        }
    }
    while (iter.hasNext()) {
        row = iter.next();
        List rowData = new ArrayList();
        for (String name : realFields) {
            rowData.add(row.get(name));
        }
        resultSet.add(rowData);
    }
    MongoElasticUtils.processTranslate(sqlToyContext, sqlToyConfig, resultSet, translateFields);
    DataSetResult dataSetResult = new DataSetResult();
    dataSetResult.setRows(resultSet);
    dataSetResult.setLabelNames(translateFields);
    // 不支持指定查询集合的行列转换,对集合进行汇总、行列转换等
    ResultUtils.calculate(sqlToyConfig, dataSetResult, null, sqlToyContext.isDebug());
    MongoElasticUtils.wrapResultClass(resultSet, translateFields, resultClass);
    return resultSet;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DataSetResult(org.sagacity.sqltoy.model.DataSetResult) Document(org.bson.Document)

Aggregations

DataSetResult (org.sagacity.sqltoy.model.DataSetResult)9 JSONObject (com.alibaba.fastjson.JSONObject)6 JSONArray (com.alibaba.fastjson.JSONArray)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)3 PaginationModel (org.sagacity.sqltoy.model.PaginationModel)2 Document (org.bson.Document)1