Sfoglia il codice sorgente

获取列信息BUG修复

chenw 7 anni fa
parent
commit
1a9fd88643

+ 9 - 0
bi-server/src/main/java/com/usoftchina/bi/server/model/vo/dataVo/ColumnTypeInfo.java

@@ -20,6 +20,15 @@ public class ColumnTypeInfo {
         this.columnType = columnType;
     }
 
+    public ColumnTypeInfo() {
+    }
+
+    public ColumnTypeInfo(String columnName, String columnType) {
+
+        this.columnName = columnName;
+        this.columnType = columnType;
+    }
+
     @Override
     public String toString() {
         return "ColumnTypeInfo{" +

+ 15 - 16
bi-server/src/main/java/com/usoftchina/bi/server/service/dataSource/ImplementSqlService.java

@@ -8,6 +8,9 @@ import com.usoftchina.bi.core.base.RepCode;
 import com.usoftchina.bi.core.base.RepEntity;
 import com.usoftchina.bi.server.model.vo.dataVo.ColumnTypeInfo;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.rowset.SqlRowSet;
+import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
 import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
@@ -21,12 +24,14 @@ public class ImplementSqlService {
     DataColumnMapper dataColumnMapper;
     @Autowired
     DataConnectorMapper dataConnectorMapper;
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
 
     /*
     执行数据源
      */
     public RepEntity implementSql(ToSql toSql) {
-        List<String> checkSql = java.util.Arrays.asList(toSql.getStrSql().toLowerCase().split(" "));
+        List<String> checkSql = Arrays.asList(toSql.getStrSql().toLowerCase().split(" "));
         if (checkSql.contains("update") || checkSql.contains("delete") || checkSql.contains("insert") ||
                 checkSql.contains("drop") || checkSql.contains("create") || checkSql.contains("comment")){
             return new RepEntity(RepCode.SqlWarn);
@@ -36,22 +41,16 @@ public class ImplementSqlService {
             return new RepEntity(RepCode.ChartsNameNull);
         }
 
-        LinkedHashMap<String, Object> columnData = dataColumnMapper.getColumn(sqlStr);
-        LinkedHashMap<String, String> tarValue = getColumnType(columnData);
-
-        List<ColumnTypeInfo> isList = new ArrayList<>();
-
-        Iterator<String> iter = columnData.keySet().iterator();
-        while (iter.hasNext()){
-            String key = iter.next();
-            String vaul = tarValue.get(key);
-            ColumnTypeInfo columnTypeInfo = new ColumnTypeInfo();
-            columnTypeInfo.setColumnName(key);
-            columnTypeInfo.setColumnType(vaul);
-            isList.add(columnTypeInfo);
+        SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet(sqlStr);
+        SqlRowSetMetaData metaData = sqlRowSet.getMetaData();
+        int length = metaData.getColumnCount();
+        List<ColumnTypeInfo> columnTypeInfoList = new ArrayList<>();
+        for (int i = 1; i <= length; i++) {
+            String typeName = metaData.getColumnClassName(i);
+            typeName = metaData.getColumnClassName(i).substring(typeName.lastIndexOf(".") + 1);
+            columnTypeInfoList.add(new ColumnTypeInfo(metaData.getColumnName(i), typeName));
         }
-
-        return new RepEntity(RepCode.success,isList);
+        return new RepEntity(RepCode.success,columnTypeInfoList);
     }
 
     /*

+ 47 - 0
bi-server/src/test/java/com/usoftchina/bi/test/server/GetJavaTypeTest.java

@@ -0,0 +1,47 @@
+package com.usoftchina.bi.test.server;
+
+import com.usoftchina.bi.server.Application;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.rowset.SqlRowSet;
+import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import java.sql.SQLException;
+
+/**
+ * @Author chenwei
+ * @Date 2019-04-09
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest( classes = Application.class)
+public class GetJavaTypeTest {
+
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
+
+    @Test
+    public void getJavaType() throws SQLException {
+        //String sql = "select * from employee where em_id = -111009";
+        String sql = "select * from (select * from employee where em_id = -111009) where rownum <= 1";
+        //String sql = "select 'a' 今天 from dual";
+        SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
+        SqlRowSetMetaData metaData = rowSet.getMetaData();
+        int length = metaData.getColumnCount();
+        for (int i = 1; i <= length; i++) {
+            System.out.println(metaData.getColumnName(i) + "=" + metaData.getColumnClassName(i).substring(metaData.getColumnClassName(i).lastIndexOf(".") + 1));
+        }
+
+
+        /*System.out.println("==========================分割==========================");
+        DatabaseMetaData databaseMetaData = jdbcTemplate.getDataSource().getConnection().getMetaData();
+        ResultSet resultSet = databaseMetaData.getColumns(null, databaseMetaData.getUserName(), "EMPLOYEE", "%");
+        while (resultSet.next()){
+            System.out.println(resultSet.getString("COLUMN_NAME") + "==" + resultSet.getString("TYPE_NAME") + "==" + resultSet.getString("REMARKS"));
+        }*/
+    }
+
+}