Search in sources :

Example 1 with ResultSetHandler

use of com.varsql.core.sql.resultset.handler.ResultSetHandler in project varsql by varsqlinfo.

the class SQLResultSetUtils method resultSetHandler.

public static SqlSourceResultVO resultSetHandler(ResultSet rs, SqlSourceResultVO ssrv, SqlStatementInfo sqlExecuteInfo, DatabaseInfo dbInfo, int maxRow, boolean gridKeyAlias) throws SQLException {
    if (rs == null) {
        return ssrv;
    }
    ResultSetMetaData rsmd = rs.getMetaData();
    int count = rsmd.getColumnCount();
    String[] columnNameArr = new String[count];
    String[] columnGridKeyArr = GridUtils.getAliasKeyArr(count);
    List<GridColumnInfo> columnInfoList = new ArrayList<GridColumnInfo>();
    String columnName = "";
    String viewColumnInfo = sqlExecuteInfo.getColumnInfo();
    Set<String> viewColumnCheck = Collections.emptySet();
    boolean columnChkFlag = false;
    if (viewColumnInfo != null && !"".equals(viewColumnInfo)) {
        columnChkFlag = true;
        viewColumnCheck = new HashSet<String>(Arrays.asList(viewColumnInfo.toUpperCase().split(",")));
    }
    Map<String, Integer> columnKeyCheck = new HashMap<String, Integer>();
    int idx = 0;
    int columnType = -1;
    String columnTypeName = "";
    GridColumnInfo columnInfo = null;
    int columnWidth = count > 10 ? 70 : 0;
    boolean useColumnLabel = dbInfo.isUseColumnLabel();
    for (int i = 0; i < count; i++) {
        idx = i + 1;
        columnName = useColumnLabel ? rsmd.getColumnLabel(idx) : rsmd.getColumnName(idx);
        if (columnChkFlag && !viewColumnCheck.contains(columnName.toUpperCase())) {
            continue;
        }
        columnType = rsmd.getColumnType(idx);
        columnTypeName = rsmd.getColumnTypeName(idx);
        if (columnKeyCheck.containsKey(columnName)) {
            int idxVal = columnKeyCheck.get(columnName) + 1;
            columnKeyCheck.put(columnName, idxVal);
            columnName = columnName + "_" + idxVal;
        } else {
            columnKeyCheck.put(columnName, 0);
        }
        columnNameArr[i] = columnName;
        columnInfo = new GridColumnInfo();
        setColumnTypeInfo(columnType, columnInfo);
        columnInfo.setNo(idx);
        columnInfo.setLabel(columnName);
        if (gridKeyAlias) {
            columnInfo.setKey(columnGridKeyArr[i]);
        } else {
            columnInfo.setKey(columnName);
            columnGridKeyArr[i] = columnName;
        }
        columnInfo.setDbType(columnTypeName);
        columnInfo.setWidth(columnWidth);
        columnInfoList.add(columnInfo);
    }
    columnKeyCheck = null;
    int first = 0, last = maxRow;
    ssrv.setColumn(columnInfoList);
    Map row = null;
    ArrayList rows = new ArrayList();
    int totalCnt = 0;
    try {
        ResultSetHandler resultsetHandler = MetaControlFactory.getDbInstanceFactory(DBType.getDBType(sqlExecuteInfo.getDbType())).getResultsetHandler();
        while (rs.next()) {
            row = new LinkedHashMap(count);
            for (int colIdx = 0; colIdx < count; colIdx++) {
                if (columnNameArr[colIdx] != null) {
                    row = resultsetHandler.getDataValue(rs, row, columnInfoList.get(colIdx));
                // row = resultsetHandler.getDataValue(row, columnGridKeyArr[colIdx], columnNameArr[colIdx], rs, colIdx+1, columnTypeArr[colIdx], columnTypeNameArr[colIdx]);
                }
            }
            rows.add(row);
            ++first;
            totalCnt++;
            if (first >= last)
                break;
        }
    } catch (SQLException e) {
        ssrv.setData(rows);
        ssrv.setResultCnt(totalCnt);
        ssrv.setResultMessage(e.getMessage());
        throw new ResultSetConvertException(VarsqlAppCode.EC_SQL_RESULT_CONVERT, e, ssrv);
    }
    ssrv.setData(rows);
    ssrv.setResultCnt(totalCnt);
    return ssrv;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) GridColumnInfo(com.varsql.core.sql.beans.GridColumnInfo) ResultSetConvertException(com.varsql.core.exception.ResultSetConvertException) ResultSetHandler(com.varsql.core.sql.resultset.handler.ResultSetHandler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with ResultSetHandler

use of com.varsql.core.sql.resultset.handler.ResultSetHandler in project varsql by varsqlinfo.

the class SQLResultSetUtils method resultSetHandler.

public static void resultSetHandler(ResultSet rs, SqlStatementInfo sqlExecuteInfo, AbstractSQLExecutorHandler baseExecutorHandler, boolean gridKeyAlias) throws SQLException {
    if (rs == null) {
        return;
    }
    ResultSetMetaData rsmd = rs.getMetaData();
    int count = rsmd.getColumnCount();
    String[] columnNameArr = new String[count];
    String[] columnGridKeyArr = GridUtils.getAliasKeyArr(count);
    int columnType = -1;
    String columnTypeName = "";
    GridColumnInfo columnInfo = null;
    List<GridColumnInfo> columnInfoList = new ArrayList<GridColumnInfo>();
    String columnName = "";
    String viewColumnInfo = sqlExecuteInfo.getColumnInfo();
    Set<String> viewColumnCheck = Collections.emptySet();
    boolean columnChkFlag = false;
    if (viewColumnInfo != null && !"".equals(viewColumnInfo)) {
        columnChkFlag = true;
        viewColumnCheck = new HashSet<String>(Arrays.asList(viewColumnInfo.toUpperCase().split(",")));
    }
    Map<String, Integer> columnKeyCheck = new HashMap<String, Integer>();
    int idx = 0;
    int columnWidth = count > 10 ? 70 : 0;
    for (int i = 0; i < count; i++) {
        idx = i + 1;
        columnName = rsmd.getColumnName(idx);
        if (columnChkFlag && !viewColumnCheck.contains(columnName.toUpperCase())) {
            continue;
        }
        columnType = rsmd.getColumnType(idx);
        columnTypeName = rsmd.getColumnTypeName(idx);
        if (columnKeyCheck.containsKey(columnName)) {
            int idxVal = columnKeyCheck.get(columnName) + 1;
            columnKeyCheck.put(columnName, idxVal);
            columnName = columnName + "_" + idxVal;
        } else {
            columnKeyCheck.put(columnName, 0);
        }
        columnNameArr[i] = columnName;
        columnInfo = new GridColumnInfo();
        setColumnTypeInfo(columnType, columnInfo);
        if (gridKeyAlias) {
            columnInfo.setKey(columnGridKeyArr[i]);
        } else {
            columnInfo.setKey(columnName);
            columnGridKeyArr[i] = columnName;
        }
        columnInfo.setNo(idx);
        columnInfo.setLabel(columnName);
        columnInfo.setDbType(columnTypeName);
        columnInfo.setWidth(columnWidth);
        columnInfoList.add(columnInfo);
    }
    columnKeyCheck = null;
    try {
        ResultSetHandler resultsetHandler = MetaControlFactory.getDbInstanceFactory(DBType.getDBType(sqlExecuteInfo.getDbType())).getResultsetHandler();
        Map row = null;
        while (rs.next()) {
            row = new LinkedHashMap(count);
            for (int colIdx = 0; colIdx < count; colIdx++) {
                if (columnNameArr[colIdx] != null) {
                    row = resultsetHandler.getDataValue(rs, row, columnInfoList.get(colIdx));
                }
            }
            boolean addFlag = baseExecutorHandler.handle(SQLHandlerParameter.builder().rowObject(row).columnInfoList(columnInfoList).build());
            if (addFlag) {
                baseExecutorHandler.addTotalCount();
            } else {
                baseExecutorHandler.addFailCount();
            }
        }
    } catch (SQLException e) {
        throw new ResultSetConvertException(VarsqlAppCode.EC_SQL_RESULT_CONVERT, e);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) GridColumnInfo(com.varsql.core.sql.beans.GridColumnInfo) ResultSetConvertException(com.varsql.core.exception.ResultSetConvertException) ResultSetHandler(com.varsql.core.sql.resultset.handler.ResultSetHandler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ResultSetConvertException (com.varsql.core.exception.ResultSetConvertException)2 GridColumnInfo (com.varsql.core.sql.beans.GridColumnInfo)2 ResultSetHandler (com.varsql.core.sql.resultset.handler.ResultSetHandler)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2