Search in sources :

Example 1 with ResultSetConvertException

use of com.varsql.core.exception.ResultSetConvertException in project varsql by varsqlinfo.

the class SQLServiceImpl method sqlData.

/**
 * @Method Name  : sqlData
 * @Method 설명 : 쿼리 데이터 보기.
 * @작성자   : ytkim
 * @작성일   : 2015. 4. 9.
 * @변경이력  :
 * @param sqlExecuteInfo
 * @param req
 * @return
 * @throws Exception
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public ResponseResult sqlData(SqlExecuteDTO sqlExecuteInfo, HttpServletRequest req) throws Exception {
    Map sqlParamMap = VartechUtils.jsonStringToObject(sqlExecuteInfo.getSqlParam(), HashMap.class);
    DatabaseInfo dbinfo = SecurityUtil.userDBInfo(sqlExecuteInfo.getConuid());
    ResponseResult parseInfo = SqlSourceBuilder.parseResponseResult(sqlExecuteInfo.getSql(), sqlParamMap, DBType.getDBType(sqlExecuteInfo.getDbType()));
    List<SqlSource> sqlList = parseInfo.getItems();
    int limit = sqlExecuteInfo.getLimit();
    if (!SecurityUtil.isAdmin()) {
        sqlExecuteInfo.setLimit(limit > dbinfo.getMaxSelectCount() ? dbinfo.getMaxSelectCount() : limit);
    }
    ArrayList<SqlSourceResultVO> reLst = new ArrayList<SqlSourceResultVO>();
    ResponseResult result = new ResponseResult();
    Connection conn = null;
    SqlSourceResultVO ssrv = null;
    long stddt = System.currentTimeMillis();
    String[] mmddHH = DateUtils.dateformat("MM-dd-HH", stddt).split("-");
    SqlLogInfoDTO sqlLogInfo = new SqlLogInfoDTO();
    sqlLogInfo.setVconnid(sqlExecuteInfo.getVconnid());
    sqlLogInfo.setViewid(sqlExecuteInfo.getViewid());
    sqlLogInfo.setStartTime(stddt);
    sqlLogInfo.setSMm(Integer.valueOf(mmddHH[0]));
    sqlLogInfo.setSDd(Integer.valueOf(mmddHH[1]));
    sqlLogInfo.setSHh(Integer.valueOf(mmddHH[2]));
    sqlLogInfo.setUsrIp(VarsqlUtils.getClientIp(req));
    SqlSource tmpSqlSource = null;
    int sqldx = 0, sqlSize = sqlList.size();
    String errorMsg = "";
    try {
        conn = ConnectionFactory.getInstance().getConnection(sqlExecuteInfo.getVconnid());
        if (!StringUtils.isBlank(sqlExecuteInfo.get_requid_())) {
            SqlExecuteManager.getInstance().setStatementInfo(sqlExecuteInfo.get_requid_(), null);
        }
        conn.setAutoCommit(false);
        List<SqlStatisticsEntity> allSqlStatistics = new LinkedList<SqlStatisticsEntity>();
        for (sqldx = 0; sqldx < sqlSize; sqldx++) {
            tmpSqlSource = sqlList.get(sqldx);
            ssrv = new SqlSourceResultVO();
            reLst.add(ssrv);
            tmpSqlSource.setResult(ssrv);
            ssrv.setStarttime(System.currentTimeMillis());
            getRequestSqlData(sqlExecuteInfo, conn, tmpSqlSource, dbinfo, true);
            ssrv.setEndtime(System.currentTimeMillis());
            ssrv.setDelay((ssrv.getEndtime() - ssrv.getStarttime()) / 1000);
            ssrv.setResultMessage((ssrv.getDelay()) + " SECOND : " + StringUtils.escape(ssrv.getResultMessage(), EscapeType.html));
            sqlLogInfo.setStartTime(ssrv.getStarttime());
            sqlLogInfo.setCommandType(tmpSqlSource.getCommandType());
            sqlLogInfo.setEndTime(ssrv.getEndtime());
            allSqlStatistics.add(SqlStatisticsEntity.builder().vconnid(sqlLogInfo.getVconnid()).viewid(sqlLogInfo.getViewid()).startTime(ConvertUtils.longToLocalDateTime(sqlLogInfo.getStartTime())).endTime(ConvertUtils.longToLocalDateTime(sqlLogInfo.getEndTime())).delayTime(sqlLogInfo.getDelayTime()).sMm(sqlLogInfo.getSMm()).sDd(sqlLogInfo.getSDd()).sHh(sqlLogInfo.getSHh()).resultCount(sqlLogInfo.getResultCount()).commandType(sqlLogInfo.getCommandType()).build());
            if (SqlDataConstants.VIEWTYPE.GRID.val().equals(ssrv.getViewType())) {
                break;
            }
        }
        commonServiceImpl.sqlLogInsert(allSqlStatistics);
        result.setItemList(reLst);
        conn.commit();
    } catch (Throwable e) {
        if (conn != null && !conn.isClosed())
            conn.rollback();
        errorMsg = e.getMessage();
        if (e instanceof ResultSetConvertException) {
            result.setResultCode(VarsqlAppCode.EC_SQL_RESULT_CONVERT);
            ssrv = ((ResultSetConvertException) e).getSsrv();
            if (ssrv != null) {
                ssrv = new SqlSourceResultVO();
            }
            ssrv.setViewType(SqlDataConstants.VIEWTYPE.GRID.val());
        } else {
            boolean ssrvNullFlag = false;
            if (ssrv == null) {
                ssrvNullFlag = true;
                ssrv = new SqlSourceResultVO();
            }
            ssrv.setEndtime(System.currentTimeMillis());
            String tmpMsg = parseInfo.getMessage();
            tmpMsg = (tmpMsg == null || "".equals(tmpMsg) ? "" : StringUtils.escape(parseInfo.getMessage(), EscapeType.html) + "<br/>");
            if (e instanceof ConnectionFactoryException) {
                if (((ConnectionFactoryException) e).getErrorCode() == VarsqlAppCode.EC_DB_POOL_CLOSE) {
                    result.setResultCode(VarsqlAppCode.EC_DB_POOL_CLOSE);
                } else {
                    result.setResultCode(VarsqlAppCode.EC_DB_POOL);
                }
            } else {
                result.setResultCode(VarsqlAppCode.EC_SQL);
            }
            result.setMessage(tmpMsg + StringUtils.escape(ssrv.getResultMessage(), EscapeType.html));
            if (ssrvNullFlag) {
                result.setMessage(errorMsg);
            }
        }
        result.addCustoms("errorLine", sqldx);
        result.setItemOne(tmpSqlSource == null ? sqlList.get(0) : tmpSqlSource);
        LoggerFactory.getLogger("sqlErrorLog").error("sqlData errorLine : {}", sqldx, e);
    } finally {
        if (conn != null && !conn.isClosed()) {
            conn.setAutoCommit(true);
            JdbcUtils.close(conn);
        }
    }
    if (!StringUtils.isBlank(sqlExecuteInfo.get_requid_())) {
        SqlExecuteManager.getInstance().removeStatementInfo(sqlExecuteInfo.get_requid_());
    }
    long enddt = System.currentTimeMillis();
    commonServiceImpl.saveSqlHistory(SqlHistoryEntity.builder().vconnid(sqlLogInfo.getVconnid()).viewid(sqlLogInfo.getViewid()).startTime(ConvertUtils.longToTimestamp(stddt)).endTime(ConvertUtils.longToTimestamp(enddt)).delayTime((int) ((enddt - stddt) / 1000)).logSql(sqlExecuteInfo.getSql()).usrIp(sqlLogInfo.getUsrIp()).errorLog(errorMsg).build());
    return result;
}
Also used : SqlSource(com.varsql.core.sql.builder.SqlSource) DatabaseInfo(com.varsql.core.db.valueobject.DatabaseInfo) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) LinkedList(java.util.LinkedList) ConnectionFactoryException(com.varsql.core.exception.ConnectionFactoryException) SqlStatisticsEntity(com.varsql.web.model.entity.sql.SqlStatisticsEntity) ResultSetConvertException(com.varsql.core.exception.ResultSetConvertException) ResponseResult(com.vartech.common.app.beans.ResponseResult) SqlSourceResultVO(com.varsql.core.sql.builder.SqlSourceResultVO) SqlLogInfoDTO(com.varsql.web.dto.sql.SqlLogInfoDTO) ParamMap(com.vartech.common.app.beans.ParamMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ResultSetConvertException

use of com.varsql.core.exception.ResultSetConvertException 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 3 with ResultSetConvertException

use of com.varsql.core.exception.ResultSetConvertException 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)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 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 DatabaseInfo (com.varsql.core.db.valueobject.DatabaseInfo)1 ConnectionFactoryException (com.varsql.core.exception.ConnectionFactoryException)1 SqlSource (com.varsql.core.sql.builder.SqlSource)1 SqlSourceResultVO (com.varsql.core.sql.builder.SqlSourceResultVO)1 SqlLogInfoDTO (com.varsql.web.dto.sql.SqlLogInfoDTO)1 SqlStatisticsEntity (com.varsql.web.model.entity.sql.SqlStatisticsEntity)1 ParamMap (com.vartech.common.app.beans.ParamMap)1 ResponseResult (com.vartech.common.app.beans.ResponseResult)1 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1