Search in sources :

Example 6 with QueryExecutor

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

the class Execute method submit.

/**
 * @todo 执行并返回修改的记录数量
 * @return
 */
public Long submit() {
    if (StringUtil.isBlank(sql)) {
        throw new IllegalArgumentException("execute operate sql is null!");
    }
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.update, super.getDialect());
    QueryExecutor queryExecute = build();
    return dialectFactory.executeSql(sqlToyContext, sqlToyConfig, queryExecute, null, autoCommit, getDataSource(sqlToyConfig));
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor)

Example 7 with QueryExecutor

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

the class Mongo method find.

/**
 * @todo 集合记录查询
 * @return
 */
public List<?> find() {
    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));
        // 聚合查询
        if (noSqlModel.isHasAggs()) {
            return aggregate(getMongoTemplate(), sqlToyConfig, realMql, (Class) extend.resultType, extend.humpMapLabel);
        }
        return findTop(getMongoTemplate(), sqlToyConfig, null, 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 8 with QueryExecutor

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

the class Query method build.

private QueryExecutor build() {
    QueryExecutor queryExecutor = null;
    if (entity != null) {
        queryExecutor = new QueryExecutor(sql, entity);
    } else {
        queryExecutor = new QueryExecutor(sql).names(names).values(values);
    }
    if (resultType != null) {
        queryExecutor.resultType(resultType);
    }
    if (handler != null) {
        queryExecutor.rowCallbackHandler(handler);
    }
    queryExecutor.humpMapLabel(humpMapLabel);
    queryExecutor.maxRows(maxRows);
    queryExecutor.fetchSize(fetchSize);
    return queryExecutor;
}
Also used : QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor)

Example 9 with QueryExecutor

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

the class Query method findRandom.

/**
 * @todo 随机取记录
 * @param randomSize
 * @return
 */
public List<?> findRandom(final double randomSize) {
    QueryExecutor queryExecute = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecute, SqlType.search, getDialect());
    QueryResult result = dialectFactory.getRandomResult(sqlToyContext, queryExecute, sqlToyConfig, new Double(randomSize), getDataSource(sqlToyConfig));
    return result.getRows();
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 10 with QueryExecutor

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

the class Query method getOne.

/**
 * @todo 获取一条记录
 * @return
 */
public Object getOne() {
    QueryExecutor queryExecute = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecute, SqlType.search, getDialect());
    QueryResult result = dialectFactory.findByQuery(sqlToyContext, queryExecute, sqlToyConfig, lockMode, getDataSource(sqlToyConfig));
    List rows = result.getRows();
    if (rows != null && rows.size() > 0) {
        return rows.get(0);
    }
    return null;
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) List(java.util.List)

Aggregations

QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)25 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)20 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)8 QueryResult (org.sagacity.sqltoy.model.QueryResult)6 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)4 ParallQueryExecutor (org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DataSource (javax.sql.DataSource)3 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)3 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)3 Entry (java.util.Map.Entry)2 Page (org.sagacity.sqltoy.model.Page)2 ParallQueryResult (org.sagacity.sqltoy.model.ParallQueryResult)2 EntityQueryExtend (org.sagacity.sqltoy.model.inner.EntityQueryExtend)2 DataSourceSelector (org.sagacity.sqltoy.plugins.datasource.DataSourceSelector)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Serializable (java.io.Serializable)1 Connection (java.sql.Connection)1 Date (java.util.Date)1