use of org.sagacity.sqltoy.config.model.SqlToyConfig 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();
}
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method updateFetchTop.
@Deprecated
public QueryResult updateFetchTop(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Integer topSize, final UpdateRowHandler updateRowHandler, final DataSource dataSource) throws Exception {
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
try {
SqlExecuteStat.start(sqlToyConfig.getId(), "updateFetchTop", sqlToyConfig.isShowSql());
return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
// 处理sql中的?为统一的:named形式
SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
QueryResult queryResult = getDialectSqlWrapper(dbType).updateFetchTop(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), topSize, updateRowHandler, conn);
if (queryExecutor.getResultType() != null) {
queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
}
this.setResult(queryResult);
}
});
} catch (Exception e) {
SqlExecuteStat.error(e);
throw e;
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method getCountBySql.
/**
* @todo 查询符合条件的记录数量
* @param sqlToyContext
* @param queryExecutor
* @param dataSource
* @return
* @throws Exception
*/
public Long getCountBySql(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final DataSource dataSource) throws Exception {
if (queryExecutor.getSql() == null)
throw new Exception("getCountBySql operate sql is null!");
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
try {
SqlExecuteStat.start(sqlToyConfig.getId(), "count", sqlToyConfig.isShowSql());
return (Long) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
// 处理sql中的?为统一的:named形式
SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
this.setResult(getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect));
}
});
} catch (Exception e) {
SqlExecuteStat.error(e);
throw e;
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class SqliteDialect method load.
/*
* sqlite 只支持表锁,将操作放于事务中即可实现锁操作
*
* @see org.sagacity.sqltoy.dialect.Dialect#load(java.io.Serializable,
* java.util.List, java.sql.Connection)
*/
@Override
public Serializable load(final SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
// 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search);
String loadSql = sqlToyConfig.getSql();
// }
return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn);
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class SqlServerDialect method load.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#load(java.io.Serializable,
* java.util.List, java.sql.Connection)
*/
@Override
public Serializable load(final SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
// 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search);
String loadSql = sqlToyConfig.getSql();
if (lockMode != null) {
loadSql = SqlServerDialectUtils.lockSql(loadSql, entityMeta.getTableName(), lockMode);
}
return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn);
}
Aggregations