Search in sources :

Example 21 with SqlToyConfig

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

the class OracleDialectUtils method load.

/**
 * @todo 加载单个对象
 * @param sqlToyContext
 * @param entity
 * @param cascadeTypes
 * @param lockMode
 * @param conn
 * @param dbType
 * @param dialect
 * @param tableName
 * @return
 * @throws Exception
 */
public static Serializable load(final SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final Integer dbType, final String dialect, String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    // 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search, dialect);
    String loadSql = sqlToyConfig.getSql(dialect);
    loadSql = loadSql.concat(getLockSql(loadSql, dbType, lockMode));
    return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn, dbType);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) Serializable(java.io.Serializable) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 22 with SqlToyConfig

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

the class SqlToyContext method getSqlToyConfig.

public SqlToyConfig getSqlToyConfig(QueryExecutor queryExecutor, SqlType sqlType, String dialect) {
    String sqlKey = queryExecutor.getInnerModel().sql;
    if (StringUtil.isBlank(sqlKey)) {
        throw new IllegalArgumentException("sql or sqlId is null!");
    }
    // 查询语句补全select * from table,避免一些sql直接从from 开始
    if (SqlType.search.equals(sqlType)) {
        if (queryExecutor.getInnerModel().resultType != null) {
            sqlKey = SqlUtil.completionSql(this, (Class) queryExecutor.getInnerModel().resultType, sqlKey);
        } else // update 2021-12-7 sql 类似 from table where xxxx 形式,补全select *
        if (!SqlConfigParseUtils.isNamedQuery(sqlKey) && StringUtil.matches(sqlKey.toLowerCase().trim(), "^from\\W")) {
            sqlKey = "select * ".concat(sqlKey);
        }
    }
    SqlToyConfig result = scriptLoader.getSqlConfig(sqlKey, sqlType, dialect);
    // 剔除空白转null的默认设置
    if (!queryExecutor.getInnerModel().blankToNull && StringUtil.isBlank(result.getId())) {
        if (result.getFilters().size() == 1) {
            result.getFilters().remove(0);
        }
    }
    return result;
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 23 with SqlToyConfig

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

the class SqlToyDaoSupport method findEntityBase.

/**
 * @TODO 提供findEntity的基础实现,供对外接口包装,额外开放了resultClass的自定义功能
 * @param entityClass
 * @param page        如分页查询则需指定,非分页则传null
 * @param entityQuery
 * @param resultClass 指定返回结果类型
 * @param isCount
 * @return
 */
private Object findEntityBase(Class entityClass, Page page, EntityQuery entityQuery, Class resultClass, boolean isCount) {
    EntityMeta entityMeta = getEntityMeta(entityClass);
    EntityQueryExtend innerModel = entityQuery.getInnerModel();
    String translateFields = "";
    // 将缓存翻译对应的查询补充到select column 上,形成select keyColumn as viewColumn 模式
    if (!innerModel.translates.isEmpty()) {
        Iterator<Translate> iter = innerModel.translates.values().iterator();
        String keyColumn;
        TranslateExtend extend;
        while (iter.hasNext()) {
            extend = iter.next().getExtend();
            // 将java模式的字段名称转化为数据库字段名称
            keyColumn = entityMeta.getColumnName(extend.keyColumn);
            if (keyColumn == null) {
                keyColumn = extend.keyColumn;
            }
            // 保留字处理
            keyColumn = ReservedWordsUtil.convertWord(keyColumn, null);
            translateFields = translateFields.concat(",").concat(keyColumn).concat(" as ").concat(extend.column);
        }
    }
    // 将notSelect构造成select,形成统一处理机制
    String[] selectFieldAry = null;
    Set<String> notSelect = innerModel.notSelectFields;
    if (notSelect != null) {
        List<String> selectFields = new ArrayList<String>();
        for (String field : entityMeta.getFieldsArray()) {
            if (!notSelect.contains(field.toLowerCase())) {
                selectFields.add(field);
            }
        }
        if (selectFields.size() > 0) {
            selectFieldAry = new String[selectFields.size()];
            selectFields.toArray(selectFieldAry);
        }
    } else {
        selectFieldAry = innerModel.fields;
    }
    // 指定的查询字段
    String fields = "";
    if (selectFieldAry != null && selectFieldAry.length > 0) {
        int index = 0;
        String colName;
        HashSet<String> cols = new HashSet<String>();
        boolean notAllPureField = false;
        for (String field : selectFieldAry) {
            // 去除重复字段
            if (!cols.contains(field)) {
                colName = entityMeta.getColumnName(field);
                // 非表字段对应pojo的属性名称
                if (colName == null) {
                    colName = field;
                    // 非字段名称
                    if (!entityMeta.getColumnFieldMap().containsKey(colName.toLowerCase())) {
                        notAllPureField = true;
                    } else {
                        // 保留字处理
                        colName = ReservedWordsUtil.convertWord(colName, null);
                    }
                } else {
                    // 保留字处理
                    colName = ReservedWordsUtil.convertWord(colName, null);
                }
                if (index > 0) {
                    fields = fields.concat(",");
                }
                fields = fields.concat(colName);
                index++;
                cols.add(field);
            }
        }
        // select 字段中可能存在max(field)或field as xxx等非字段形式
        if (notAllPureField) {
            fields = SqlUtil.convertFieldsToColumns(entityMeta, fields);
        }
    } else {
        fields = entityMeta.getAllColumnNames();
    }
    String sql = "select ".concat((innerModel.distinct) ? " distinct " : "").concat(fields).concat(translateFields).concat(" from ").concat(entityMeta.getSchemaTable(null, null));
    // where条件
    String where = "";
    // 动态组织where 后面的条件语句,此功能并不建议使用,where 一般需要指定明确条件
    if (StringUtil.isBlank(innerModel.where)) {
        if (innerModel.values != null && innerModel.values.length > 0) {
            where = SqlUtil.wrapWhere(entityMeta);
        }
    } else {
        where = SqlUtil.convertFieldsToColumns(entityMeta, innerModel.where);
    }
    if (StringUtil.isNotBlank(where)) {
        sql = sql.concat(" where ").concat(where);
    }
    // 分组和having
    if (StringUtil.isNotBlank(innerModel.groupBy)) {
        sql = sql.concat(" group by ").concat(SqlUtil.convertFieldsToColumns(entityMeta, innerModel.groupBy));
        if (StringUtil.isNotBlank(innerModel.having)) {
            sql = sql.concat(" having ").concat(SqlUtil.convertFieldsToColumns(entityMeta, innerModel.having));
        }
    }
    // 处理order by 排序
    if (!innerModel.orderBy.isEmpty()) {
        sql = sql.concat(" order by ");
        Iterator<Entry<String, String>> iter = innerModel.orderBy.entrySet().iterator();
        Entry<String, String> entry;
        String columnName;
        int index = 0;
        while (iter.hasNext()) {
            entry = iter.next();
            columnName = entityMeta.getColumnName(entry.getKey());
            if (columnName == null) {
                columnName = entry.getKey();
            }
            // 保留字处理
            columnName = ReservedWordsUtil.convertWord(columnName, null);
            if (index > 0) {
                sql = sql.concat(",");
            }
            // entry.getValue() is order way,like: desc or " "
            sql = sql.concat(columnName).concat(entry.getValue());
            index++;
        }
    }
    QueryExecutor queryExecutor;
    Class resultType = (resultClass == null) ? entityClass : resultClass;
    // :named 模式(named模式参数值必须存在)
    if (SqlConfigParseUtils.hasNamedParam(where) && StringUtil.isBlank(innerModel.names)) {
        queryExecutor = new QueryExecutor(sql, (innerModel.values == null || innerModel.values.length == 0) ? null : (Serializable) innerModel.values[0]).resultType(resultType).dataSource(getDataSource(innerModel.dataSource)).fetchSize(innerModel.fetchSize).maxRows(innerModel.maxRows);
    } else {
        queryExecutor = new QueryExecutor(sql).names(innerModel.names).values(innerModel.values).resultType(resultType).dataSource(getDataSource(innerModel.dataSource)).fetchSize(innerModel.fetchSize).maxRows(innerModel.maxRows);
    }
    // 设置是否空白转null
    queryExecutor.getInnerModel().blankToNull = innerModel.blankToNull;
    // 设置额外的缓存翻译
    if (!innerModel.translates.isEmpty()) {
        queryExecutor.getInnerModel().translates.putAll(innerModel.translates);
    }
    // 设置额外的参数条件过滤
    if (!innerModel.paramFilters.isEmpty()) {
        queryExecutor.getInnerModel().paramFilters.addAll(innerModel.paramFilters);
    }
    // 设置安全脱敏
    if (!innerModel.secureMask.isEmpty()) {
        queryExecutor.getInnerModel().secureMask.putAll(innerModel.secureMask);
    }
    // 设置分页优化
    queryExecutor.getInnerModel().pageOptimize = innerModel.pageOptimize;
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor, SqlType.search, getDialect(queryExecutor.getInnerModel().dataSource));
    // 加密字段,查询时解密
    if (entityMeta.getSecureColumns() != null) {
        sqlToyConfig.setDecryptColumns(entityMeta.getSecureColumns());
    }
    // 分库分表策略
    setEntitySharding(queryExecutor, entityMeta);
    if (innerModel.dbSharding != null) {
        queryExecutor.getInnerModel().dbSharding = innerModel.dbSharding;
    }
    if (innerModel.tableSharding != null) {
        ShardingStrategyConfig shardingConfig = innerModel.tableSharding;
        // 补充表名称
        shardingConfig.setTables(new String[] { entityMeta.getTableName() });
        List<ShardingStrategyConfig> tableShardings = new ArrayList<ShardingStrategyConfig>();
        tableShardings.add(shardingConfig);
        queryExecutor.getInnerModel().tableShardings = tableShardings;
    }
    DataSource realDataSource = getDataSource(queryExecutor.getInnerModel().dataSource, sqlToyConfig);
    // 取count数量
    if (isCount) {
        return dialectFactory.getCountBySql(sqlToyContext, queryExecutor, sqlToyConfig, realDataSource);
    }
    // 非分页
    if (page == null) {
        // 取top
        if (innerModel.pickType == 0) {
            return dialectFactory.findTop(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.pickSize, realDataSource).getRows();
        } else // 取随机记录
        if (innerModel.pickType == 1) {
            return dialectFactory.getRandomResult(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.pickSize, realDataSource).getRows();
        } else {
            return dialectFactory.findByQuery(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.lockMode, realDataSource).getRows();
        }
    }
    // 跳过总记录数形式的分页
    if (page.getSkipQueryCount()) {
        return dialectFactory.findSkipTotalCountPage(sqlToyContext, queryExecutor, sqlToyConfig, page.getPageNo(), page.getPageSize(), realDataSource).getPageResult();
    }
    return dialectFactory.findPage(sqlToyContext, queryExecutor, sqlToyConfig, page.getPageNo(), page.getPageSize(), realDataSource).getPageResult();
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) Entry(java.util.Map.Entry) ParallQueryExecutor(org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) ShardingStrategyConfig(org.sagacity.sqltoy.config.model.ShardingStrategyConfig) TranslateExtend(org.sagacity.sqltoy.model.inner.TranslateExtend) EntityQueryExtend(org.sagacity.sqltoy.model.inner.EntityQueryExtend) Translate(org.sagacity.sqltoy.config.model.Translate) HashSet(java.util.HashSet)

Example 24 with SqlToyConfig

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

the class SqlToyDaoSupport method parallQuery.

/**
 * @TODO 并行查询并返回一维List,有几个查询List中就包含几个结果对象,paramNames和paramValues是全部sql的条件参数的合集
 * @param parallQueryList
 * @param paramNames
 * @param paramValues
 * @param parallelConfig
 * @return
 */
protected <T> List<QueryResult<T>> parallQuery(List<ParallQuery> parallQueryList, String[] paramNames, Object[] paramValues, ParallelConfig parallelConfig) {
    if (parallQueryList == null || parallQueryList.isEmpty()) {
        return null;
    }
    ParallelConfig parallConfig = parallelConfig;
    if (parallConfig == null) {
        parallConfig = new ParallelConfig();
    }
    // 并行线程数量(默认最大十个)
    if (parallConfig.getMaxThreads() == null) {
        parallConfig.maxThreads(10);
    }
    int thread = parallConfig.getMaxThreads();
    if (parallQueryList.size() < thread) {
        thread = parallQueryList.size();
    }
    List<QueryResult<T>> results = new ArrayList<QueryResult<T>>();
    ExecutorService pool = null;
    try {
        pool = Executors.newFixedThreadPool(thread);
        List<Future<ParallQueryResult>> futureResult = new ArrayList<Future<ParallQueryResult>>();
        SqlToyConfig sqlToyConfig;
        Future<ParallQueryResult> future;
        for (ParallQuery query : parallQueryList) {
            sqlToyConfig = sqlToyContext.getSqlToyConfig(new QueryExecutor(query.getExtend().sql).resultType(query.getExtend().resultType), SqlType.search, getDialect(query.getExtend().dataSource));
            // 自定义条件参数
            if (query.getExtend().selfCondition) {
                future = pool.submit(new ParallQueryExecutor(sqlToyContext, dialectFactory, sqlToyConfig, query, query.getExtend().names, query.getExtend().values, getDataSource(query.getExtend().dataSource, sqlToyConfig)));
            } else {
                future = pool.submit(new ParallQueryExecutor(sqlToyContext, dialectFactory, sqlToyConfig, query, paramNames, paramValues, getDataSource(query.getExtend().dataSource, sqlToyConfig)));
            }
            futureResult.add(future);
        }
        pool.shutdown();
        // 设置最大等待时长
        if (parallConfig.getMaxWaitSeconds() != null) {
            pool.awaitTermination(parallConfig.getMaxWaitSeconds(), TimeUnit.SECONDS);
        } else {
            pool.awaitTermination(SqlToyConstants.PARALLEL_MAXWAIT_SECONDS, TimeUnit.SECONDS);
        }
        ParallQueryResult item;
        int index = 0;
        for (Future<ParallQueryResult> result : futureResult) {
            index++;
            item = result.get();
            // 存在执行异常则整体抛出
            if (item != null && !item.isSuccess()) {
                throw new DataAccessException("第:{} 个sql执行异常:{}!", index, item.getMessage());
            }
            results.add(item.getResult());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException("并行查询执行错误:" + e.getMessage(), e);
    } finally {
        if (pool != null) {
            pool.shutdownNow();
        }
    }
    return results;
}
Also used : ParallQueryExecutor(org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ParallelConfig(org.sagacity.sqltoy.model.ParallelConfig) ParallQuery(org.sagacity.sqltoy.model.ParallQuery) ArrayList(java.util.ArrayList) ParallQueryResult(org.sagacity.sqltoy.model.ParallQueryResult) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) QueryResult(org.sagacity.sqltoy.model.QueryResult) ParallQueryResult(org.sagacity.sqltoy.model.ParallQueryResult) ParallQueryExecutor(org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 25 with SqlToyConfig

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

the class SqlToyDaoSupport method deleteByQuery.

/**
 * @TODO 提供单表简易查询进行删除操作(删除操作filters过滤无效)
 * @param entityClass
 * @param entityQuery
 * @return
 */
protected Long deleteByQuery(Class entityClass, EntityQuery entityQuery) {
    EntityQueryExtend innerModel = entityQuery.getInnerModel();
    if (null == entityClass || null == entityQuery || StringUtil.isBlank(innerModel.where) || StringUtil.isBlank(innerModel.values)) {
        throw new IllegalArgumentException("deleteByQuery entityClass、where、value 值不能为空!");
    }
    // 做一个必要提示
    if (!innerModel.paramFilters.isEmpty()) {
        logger.warn("删除操作设置动态条件过滤是无效的,数据删除查询条件必须是精准的!");
    }
    EntityMeta entityMeta = getEntityMeta(entityClass);
    String where = SqlUtil.convertFieldsToColumns(entityMeta, innerModel.where);
    String sql = "delete from ".concat(entityMeta.getSchemaTable(null, null)).concat(" where ").concat(where);
    SqlToyConfig sqlToyConfig = getSqlToyConfig(sql, SqlType.update);
    QueryExecutor queryExecutor = null;
    // :named 模式
    if (SqlConfigParseUtils.hasNamedParam(where) && StringUtil.isBlank(innerModel.names)) {
        queryExecutor = new QueryExecutor(sql, (Serializable) innerModel.values[0]);
    } else {
        queryExecutor = new QueryExecutor(sql).names(innerModel.names).values(innerModel.values);
    }
    if (innerModel.paramFilters != null && innerModel.paramFilters.size() > 0) {
        queryExecutor.getInnerModel().paramFilters.addAll(innerModel.paramFilters);
    }
    // 分库分表策略
    setEntitySharding(queryExecutor, entityMeta);
    return dialectFactory.executeSql(sqlToyContext, sqlToyConfig, queryExecutor, null, null, getDataSource(innerModel.dataSource));
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) Serializable(java.io.Serializable) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ParallQueryExecutor(org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) EntityQueryExtend(org.sagacity.sqltoy.model.inner.EntityQueryExtend)

Aggregations

SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)82 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)22 Serializable (java.io.Serializable)20 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)20 QueryResult (org.sagacity.sqltoy.model.QueryResult)20 Connection (java.sql.Connection)19 ArrayList (java.util.ArrayList)19 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)19 List (java.util.List)16 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)16 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)15 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)12 BaseException (org.sagacity.sqltoy.exception.BaseException)10 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)5 DataSource (javax.sql.DataSource)4 Test (org.junit.jupiter.api.Test)4 ParallQueryExecutor (org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor)4 QueryExecutor (org.sagacity.sqltoy.executor.QueryExecutor)4 InputStream (java.io.InputStream)3 Document (org.w3c.dom.Document)3