use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class SqlConfigParseUtils method parseSqlToyConfig.
/**
* @todo 将动态的sql解析组合成一个SqlToyConfig模型,以便统一处理
* @param querySql
* @param dialect
* @param sqlType
* @param functionConverts
* @return
*/
public static SqlToyConfig parseSqlToyConfig(String querySql, String dialect, SqlType sqlType, List<IFunction> functionConverts) {
SqlToyConfig sqlToyConfig = new SqlToyConfig();
// debug模式下面关闭sql打印
sqlToyConfig.setShowSql(!StringUtil.matches(querySql, SqlToyConstants.NOT_PRINT_REGEX));
// 清理sql中的一些注释、以及特殊的符号
String originalSql = StringUtil.clearMistyChars(SqlUtil.clearMark(querySql), BLANK).concat(BLANK);
// 对sql中的函数进行特定数据库方言转换
originalSql = convertFunctions(functionConverts, dialect, originalSql);
// 判定是否有with查询模式
sqlToyConfig.setHasWith(SqlConfigParseUtils.hasWith(originalSql));
/**
* 只有在查询模式前提下才支持fastPage机制
*/
if (sqlType.equals(SqlType.search)) {
// sqlToyConfig.setHasFast(hasFast(originalSql));
// 判断是否有快速分页@fast 宏
Matcher matcher = FAST_PATTERN.matcher(originalSql);
if (matcher.find()) {
int start = matcher.start();
String preSql = originalSql.substring(0, start);
String matchedFastSql = matcher.group();
int endMarkIndex = StringUtil.getSymMarkIndex("(", ")", matchedFastSql, 0);
// 得到分页宏处理器中的sql
String fastSql = matchedFastSql.substring(matchedFastSql.indexOf("(") + 1, endMarkIndex);
String tailSql = originalSql.substring(start + endMarkIndex + 1);
// sql剔除掉快速分页宏,在分页查询时再根据presql和tailsql、fastsql自行组装,从而保障正常的非分页查询直接提取sql
sqlToyConfig.setSql(preSql.concat(" (").concat(fastSql).concat(") ").concat(tailSql));
sqlToyConfig.setFastSql(fastSql);
sqlToyConfig.setFastPreSql(preSql);
sqlToyConfig.setFastTailSql(tailSql);
// 判断是否有快速分页
sqlToyConfig.setHasFast(true);
} else
sqlToyConfig.setSql(originalSql);
} else
sqlToyConfig.setSql(originalSql);
// 提取with fast查询语句
processFastWith(sqlToyConfig);
// 提取sql中的参数名称
sqlToyConfig.setParamsName(getSqlParamsName(sqlToyConfig.getSql(), true));
return sqlToyConfig;
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig 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());
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class ResultUtils method getPivotCategory.
/**
* @todo 提取数据旋转对应的sql查询结果
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param conn
* @param dialect
* @return
* @throws Exception
*/
public static List getPivotCategory(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Connection conn, String dialect) throws Exception {
if (sqlToyConfig.getResultProcessor() != null && !sqlToyConfig.getResultProcessor().isEmpty()) {
List resultProcessors = sqlToyConfig.getResultProcessor();
Object processor;
for (int i = 0; i < resultProcessors.size(); i++) {
processor = resultProcessors.get(i);
// 数据旋转只能存在一个
if (processor instanceof PivotModel) {
PivotModel pivotModel = (PivotModel) processor;
if (pivotModel.getCategorySql() != null) {
SqlToyConfig pivotSqlConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyContext.getSqlToyConfig(pivotModel.getCategorySql(), SqlType.search), queryExecutor, dialect, false);
SqlToyResult pivotSqlToyResult = SqlConfigParseUtils.processSql(pivotSqlConfig.getSql(), queryExecutor.getParamsName(pivotSqlConfig), queryExecutor.getParamsValue(pivotSqlConfig));
List pivotCategory = SqlUtil.findByJdbcQuery(pivotSqlToyResult.getSql(), pivotSqlToyResult.getParamsValue(), null, null, conn);
// 行转列返回
return CollectionUtil.convertColToRow(pivotCategory, null);
}
}
}
}
return null;
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class ResultUtils method getPivotCategory.
/**
* @todo 提取数据旋转对应的sql查询结果
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param conn
* @param dbType
* @param dialect
* @return
* @throws Exception
*/
public static List getPivotCategory(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Connection conn, final Integer dbType, String dialect) throws Exception {
List resultProcessors = new ArrayList();
QueryExecutorExtend extend = queryExecutor.getInnerModel();
if (!sqlToyConfig.getResultProcessor().isEmpty()) {
resultProcessors.addAll(sqlToyConfig.getResultProcessor());
}
// QueryExecutor中扩展的计算
if (extend != null && !extend.calculators.isEmpty()) {
resultProcessors.addAll(extend.calculators);
}
Object processor;
for (int i = 0; i < resultProcessors.size(); i++) {
processor = resultProcessors.get(i);
// 数据旋转只能存在一个
if (processor instanceof PivotModel) {
PivotModel pivotModel = (PivotModel) processor;
if (pivotModel.getCategorySql() != null) {
SqlToyConfig pivotSqlConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyContext.getSqlToyConfig(pivotModel.getCategorySql(), SqlType.search, ""), queryExecutor, dialect, false);
SqlToyResult pivotSqlToyResult = SqlConfigParseUtils.processSql(pivotSqlConfig.getSql(dialect), extend.getParamsName(pivotSqlConfig), extend.getParamsValue(sqlToyContext, pivotSqlConfig), dialect);
List pivotCategory = SqlUtil.findByJdbcQuery(sqlToyContext.getTypeHandler(), pivotSqlToyResult.getSql(), pivotSqlToyResult.getParamsValue(), null, null, null, conn, dbType, sqlToyConfig.isIgnoreEmpty(), null, SqlToyConstants.FETCH_SIZE, -1);
// 行转列返回
return CollectionUtil.convertColToRow(pivotCategory, null);
}
}
}
return null;
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class SqlConfigParseUtilsTest method testParseSql.
@Test
public void testParseSql() throws Exception {
String sqlFile = "classpath:scripts/showcase-sql.sql.xml";
List<SqlToyConfig> result = new ArrayList<SqlToyConfig>();
InputStream fileIS = FileUtil.getFileInputStream(sqlFile);
domFactory.setFeature(SqlToyConstants.XML_FETURE, false);
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document doc = domBuilder.parse(fileIS);
NodeList sqlElts = doc.getDocumentElement().getChildNodes();
if (sqlElts == null || sqlElts.getLength() == 0)
return;
// 解析单个sql
Element sqlElt;
Node obj;
for (int i = 0; i < sqlElts.getLength(); i++) {
obj = sqlElts.item(i);
if (obj.getNodeType() == Node.ELEMENT_NODE) {
sqlElt = (Element) obj;
result.add(SqlXMLConfigParse.parseSingleSql(sqlElt, "mysql"));
}
}
for (SqlToyConfig config : result) {
System.err.println(config.getSql());
}
}
Aggregations