Search in sources :

Example 1 with QueryExecutor

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

the class DialectFactory method executeSql.

/**
 * @todo 执行sql修改性质的操作语句
 * @param sqlToyContext
 * @param sqlOrNamedSql
 * @param paramsNamed
 * @param paramsValue
 * @param autoCommit
 * @param dataSource
 * @return
 * @throws Exception
 */
public Long executeSql(final SqlToyContext sqlToyContext, final String sqlOrNamedSql, final String[] paramsNamed, final Object[] paramsValue, final Boolean autoCommit, final DataSource dataSource) throws Exception {
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sqlOrNamedSql, SqlType.update);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "update", sqlToyConfig.isShowSql());
        return (Long) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, new QueryExecutor(sqlOrNamedSql, paramsNamed, paramsValue), dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(sqlToyContext.convertFunctions(sqlToyConfig.getSql(), dialect), paramsNamed, paramsValue);
                String executeSql = queryParam.getSql();
                // 替换sharding table
                executeSql = ShardingUtils.replaceShardingTables(sqlToyContext, executeSql, sqlToyConfig, paramsNamed, paramsValue);
                // debug 显示sql
                SqlExecuteStat.showSql(executeSql, queryParam.getParamsValue());
                this.setResult(DialectUtils.executeSql(executeSql, queryParam.getParamsValue(), null, conn, autoCommit));
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) Connection(java.sql.Connection) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) BaseException(org.sagacity.sqltoy.exception.BaseException) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 2 with QueryExecutor

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

the class SqlToyDaoSupport method findByCriteria.

/**
 * @todo 根据链式条件构造查询语句(不建议使用,并没有写sql直观)
 * @param criteria
 * @return
 * @throws Exception
 */
protected List findByCriteria(final Criteria criteria) throws Exception {
    EntityMeta entityMeta = this.getEntityMeta(criteria.getEntity().getClass());
    // 构造条件查询
    StringBuilder queryStr = new StringBuilder(entityMeta.getLoadAllSql());
    // 增加空白增强容错性
    queryStr.append(" ").append(criteria.wrapSqlFragments(entityMeta));
    return findByQuery(new QueryExecutor(queryStr.toString(), null, criteria.getParamValues().toArray()).resultType(criteria.getEntity().getClass())).getRows();
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor)

Example 3 with QueryExecutor

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

the class SqlToyDaoSupport method findPageByCriteria.

protected PaginationModel findPageByCriteria(PaginationModel pageModel, Criteria criteria) throws Exception {
    EntityMeta entityMeta = this.getEntityMeta(criteria.getEntity().getClass());
    // 构造条件查询
    StringBuilder queryStr = new StringBuilder(entityMeta.getLoadAllSql());
    // 增加一个空格增强容错
    queryStr.append(" ").append(criteria.wrapSqlFragments(entityMeta));
    return findPageByQuery(pageModel, new QueryExecutor(queryStr.toString(), null, criteria.getParamValues().toArray()).resultType(criteria.getEntity().getClass())).getPageResult();
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor)

Example 4 with QueryExecutor

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

the class Mongo method findPage.

/**
 * @todo 分页查询
 * @param pageModel
 * @return
 * @throws Exception
 */
public PaginationModel findPage(PaginationModel pageModel) throws Exception {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql);
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null)
        throw new Exception(ERROR_MESSAGE);
    // 最后的执行语句
    String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig));
    return findPage(new MongoTemplate(getMongoDbFactory(noSqlModel.getMongoFactory())), sqlToyConfig, pageModel, realMql, queryExecutor.getResultTypeName());
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate)

Example 5 with QueryExecutor

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

the class DemeServiceImpl method updateFetch.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.demo.service.DemoService#updateFetch()
	 */
@Override
public List updateFetch() throws Exception {
    StringBuilder query = new StringBuilder("SELECT STAFF_ID, STAFF_CODE, ORGAN_ID, STAFF_NAME, ENGLISH_NAME, SEX_TYPE,");
    query.append("LINK_PHONE, BIRTHDAY, DUTY_DATE, OUT_DUTY_DATE, POST, PHOTO, EMAIL, IS_VIRTUAL, OPERATOR, OPERATE_DATE, ENABLED ");
    query.append(" FROM SYS_STAFF_INFO where SEX_TYPE=:sexType");
    // String sql =
    // "select t.TRIGGER_ID, t.MANUAL_END, t.STATUS from cron_trigger t where t.STATUS in (:status) ";
    QueryExecutor queryExecutor = new QueryExecutor(query.toString(), new String[] { "sexType" }, new Object[] { "F" });
    List result = sqlToyLazyDao.updateFetch(queryExecutor, new UpdateRowHandler() {

        @Override
        public void updateRow(ResultSet rs, int index) throws SQLException {
            rs.updateString("LINK_PHONE", "13918799460");
            rs.updateString("ENGLISH_NAME", rs.getString("STAFF_NAME") + "EN");
        }
    });
    for (int i = 0; i < result.size(); i++) {
        System.err.println(((List) result.get(i)).get(0));
        System.err.println(((List) result.get(i)).get(3));
    }
    // );
    return result;
}
Also used : SQLException(java.sql.SQLException) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) ResultSet(java.sql.ResultSet) UpdateRowHandler(org.sagacity.sqltoy.callback.UpdateRowHandler) List(java.util.List)

Aggregations

QueryExecutor (org.sagacity.sqltoy.executor.QueryExecutor)7 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)4 Connection (java.sql.Connection)2 List (java.util.List)2 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)2 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1 DocumentException (org.dom4j.DocumentException)1 TranslateCacheManager (org.sagacity.sqltoy.cache.TranslateCacheManager)1 UpdateRowHandler (org.sagacity.sqltoy.callback.UpdateRowHandler)1 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)1 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)1 TranslateCacheModel (org.sagacity.sqltoy.config.model.TranslateCacheModel)1 BaseException (org.sagacity.sqltoy.exception.BaseException)1 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)1