| | |
| | | package com.product.server.report.service; |
| | | |
| | | import java.sql.Connection; |
| | | import java.sql.PreparedStatement; |
| | | import java.sql.ResultSetMetaData; |
| | | import java.sql.SQLException; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | import com.google.common.collect.Lists; |
| | | import com.google.common.collect.Maps; |
| | | import com.google.common.collect.Sets; |
| | | import com.product.core.entity.RequestParameterEntity; |
| | | import com.product.core.service.support.QueryFilterService; |
| | | import com.product.util.BaseUtil; |
| | | import com.product.util.CallBack; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import com.product.common.lang.StringUtils; |
| | | import com.product.core.cache.DataPoolCacheImpl; |
| | | import com.product.core.connection.ConnectionManager; |
| | |
| | | import com.product.core.entity.FieldSetEntity; |
| | | import com.product.core.exception.BaseException; |
| | | import com.product.core.service.support.AbstractBaseService; |
| | | import com.product.core.service.support.QueryFilterService; |
| | | import com.product.core.spring.context.SpringMVCContextHolder; |
| | | import com.product.core.transfer.Transactional; |
| | | import com.product.core.util.ReflectUtil; |
| | | import com.product.server.report.config.CmnConst; |
| | | import com.product.server.report.config.ReportCode; |
| | | import com.product.server.report.service.idel.IReportDatasourceService; |
| | | import com.product.server.report.util.QuerySqlParseUtil; |
| | | import com.product.util.BaseUtil; |
| | | import com.product.util.CallBack; |
| | | import com.product.util.SystemParamReplace; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.sql.*; |
| | | import java.util.Date; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | @Component |
| | | public class ReportDatasourceService extends AbstractBaseService implements IReportDatasourceService { |
| | |
| | | /** |
| | | * sql解析与验证 |
| | | * |
| | | * @param sqlText |
| | | * @param fse |
| | | * @return |
| | | */ |
| | | public DataTableEntity sqlVerify(FieldSetEntity fse) throws SQLException { |
| | |
| | | String executeContent; |
| | | String resultValue; |
| | | for (String ternaryOperator : ternaryOperatorSuitList) { |
| | | executeContent = ternaryOperator.substring(2, ternaryOperator.length() - 2); |
| | | executeContent = ternaryOperator.substring(2, ternaryOperator.length() - 2).trim(); |
| | | try { |
| | | resultValue = BaseUtil.executeExpression(executeContent).toString(); |
| | | String reg = "\\w+\\.\\w+\\((\\w+)?((,\\w+)*)?\\)"; |
| | | if (executeContent.matches(reg)) { |
| | | // bean调用方法处理 |
| | | int split1 = executeContent.indexOf("."); |
| | | int split2 = executeContent.indexOf("("); |
| | | String beanName = executeContent.substring(0, split1); |
| | | String methodName = executeContent.substring(split1 + 1, split2); |
| | | String paramStr = executeContent.substring(split2 + 1, executeContent.length() - 1); |
| | | if (StringUtils.isEmpty(paramStr)) { |
| | | resultValue = ReflectUtil.invoke(beanName, methodName, new Object[]{}).toString(); |
| | | } else { |
| | | resultValue = ReflectUtil.invoke(beanName, methodName, Arrays.asList(paramStr.split(",")).toArray()).toString(); |
| | | } |
| | | } else { |
| | | resultValue = BaseUtil.executeExpression(executeContent).toString(); |
| | | } |
| | | sql = sql.replace(ternaryOperator, resultValue); |
| | | } catch (Exception e) { |
| | | SpringMVCContextHolder.getSystemLogger().error(e); |
| | | sql = sql.replace(ternaryOperator, ""); |
| | | throw new BaseException(ReportCode.APPLY_SQL_FAIL.getValue(), ReportCode.APPLY_SQL_FAIL.getText() + e.getMessage()); |
| | | } |
| | | } |
| | | sql = sql.replaceAll("\\[\\[((?!\\[).)+\\]\\]", ""); |
| | | sql = sql.replaceAll("\\[\\[((?!\\[).)+\\]\\]", "") |
| | | .replaceAll("and\\s+1\\s*=\\s*1", "") |
| | | .replaceAll("\\{=.+=\\}", "") |
| | | .replaceAll("(\\t*\\n){2,}", "\n"); |
| | | sql = QuerySqlParseUtil.parseSplitTableSql(baseDao, sql, true); |
| | | SpringMVCContextHolder.getSystemLogger().info(sql); |
| | | //与数据库的连接 |
| | | PreparedStatement ps = null; |
| | | Connection con = null; |
| | | con = ConnectionManager.getConnection(); |
| | | sql = sql.replaceAll("and\\s+1\\s*=\\s*1", ""); |
| | | ps = con.prepareStatement(sql); |
| | | //结果集元数据 |
| | | ResultSetMetaData rsmd = ps.getMetaData(); |
| | | //表列数 |
| | | int size = rsmd.getColumnCount(); |
| | | Set<String> fieldNameSet = Sets.newHashSet(); |
| | | for (int i = 1; i <= size; i++) { |
| | | FieldSetEntity fieldSetEntity = new FieldSetEntity(); |
| | | FieldMetaEntity f = new FieldMetaEntity(); |
| | | f.setTableName(new Object[]{CmnConst.PRODUCT_SYS_DATAMODEL_FIELD}); |
| | | fieldSetEntity.setMeta(f); |
| | | fieldSetEntity.setValue(CmnConst.TABLE_NAME, rsmd.getTableName(i));//获取表名 |
| | | //字段名或别名 |
| | | fieldSetEntity.setValue(CmnConst.FIELD_NAME, rsmd.getColumnLabel(i)); |
| | | //字段类型 |
| | | fieldSetEntity.setValue(CmnConst.FIELD_FORMAT, "string"); |
| | | // 查询类型 |
| | | fieldSetEntity.setValue(CmnConst.SEARCH_TYPE, -1); |
| | | //获取指定列的指定列大小 |
| | | try (Connection con = ConnectionManager.getConnection(); |
| | | PreparedStatement ps = con.prepareStatement(sql); |
| | | ResultSet rs = ps.executeQuery()) { |
| | | //结果集元数据 |
| | | ResultSetMetaData rsmd = rs.getMetaData(); |
| | | //表列数 |
| | | int size = rsmd.getColumnCount(); |
| | | Set<String> fieldNameSet = Sets.newHashSet(); |
| | | for (int i = 1; i <= size; i++) { |
| | | FieldSetEntity fieldSetEntity = new FieldSetEntity(); |
| | | FieldMetaEntity f = new FieldMetaEntity(); |
| | | f.setTableName(new Object[]{CmnConst.PRODUCT_SYS_DATAMODEL_FIELD}); |
| | | fieldSetEntity.setMeta(f); |
| | | fieldSetEntity.setValue(CmnConst.TABLE_NAME, rsmd.getTableName(i));//获取表名 |
| | | //字段名或别名 |
| | | fieldSetEntity.setValue(CmnConst.FIELD_NAME, rsmd.getColumnLabel(i)); |
| | | //字段类型 |
| | | fieldSetEntity.setValue(CmnConst.FIELD_FORMAT, "string"); |
| | | // 查询类型 |
| | | fieldSetEntity.setValue(CmnConst.SEARCH_TYPE, -1); |
| | | //获取指定列的指定列大小 |
| | | // fieldSetEntity.setValue(CmnConst.FIELD_LENGTH, rsmd.getPrecision(i)); |
| | | dataTableEntity.addFieldSetEntity(fieldSetEntity); |
| | | fieldNameSet.add(fieldSetEntity.getString(CmnConst.FIELD_NAME)); |
| | | } |
| | | for (String columnName : customColumnName) { |
| | | if (fieldNameSet.contains(columnName)) { |
| | | continue; |
| | | dataTableEntity.addFieldSetEntity(fieldSetEntity); |
| | | fieldNameSet.add(fieldSetEntity.getString(CmnConst.FIELD_NAME)); |
| | | } |
| | | FieldSetEntity fs = new FieldSetEntity(); |
| | | fs.setTableName(CmnConst.PRODUCT_SYS_DATAMODEL_FIELD); |
| | | fs.setValue(CmnConst.FIELD_NAME, columnName); |
| | | fs.setValue(CmnConst.FIELD_FORMAT, "string"); |
| | | fs.setValue(CmnConst.SEARCH_TYPE, -1); |
| | | dataTableEntity.addFieldSetEntity(fs); |
| | | for (String columnName : customColumnName) { |
| | | if (fieldNameSet.contains(columnName)) { |
| | | continue; |
| | | } |
| | | FieldSetEntity fs = new FieldSetEntity(); |
| | | fs.setTableName(CmnConst.PRODUCT_SYS_DATAMODEL_FIELD); |
| | | fs.setValue(CmnConst.FIELD_NAME, columnName); |
| | | fs.setValue(CmnConst.FIELD_FORMAT, "string"); |
| | | fs.setValue(CmnConst.SEARCH_TYPE, -1); |
| | | dataTableEntity.addFieldSetEntity(fs); |
| | | } |
| | | return dataTableEntity; |
| | | } |
| | | return dataTableEntity; |
| | | } |
| | | |
| | | /** |