| | |
| | | package com.product.server.report.service; |
| | | |
| | | import java.sql.Connection; |
| | | import java.sql.PreparedStatement; |
| | | import java.sql.ResultSetMetaData; |
| | | import java.sql.SQLException; |
| | | import java.sql.*; |
| | | import java.util.*; |
| | | import java.util.Date; |
| | | 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.server.report.util.QuerySqlParseUtil; |
| | | import com.product.util.BaseUtil; |
| | | import com.product.util.CallBack; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | /** |
| | | * sql解析与验证 |
| | | * |
| | | * @param sqlText |
| | | * @param fse |
| | | * @return |
| | | */ |
| | | public DataTableEntity sqlVerify(FieldSetEntity fse) throws SQLException { |
| | |
| | | sql = sql.replace(ternaryOperator, ""); |
| | | } |
| | | } |
| | | 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; |
| | | } |
| | | |
| | | /** |