Search in sources :

Example 6 with SqlSource

use of com.varsql.core.sql.builder.SqlSource in project varsql by varsqlinfo.

the class SelectExecutor method execute.

@Override
public SQLExecuteResult execute(SqlStatementInfo statementInfo, AbstractSQLExecutorHandler resultHandler) throws SQLException {
    SQLExecuteResult result = new SQLExecuteResult();
    Map sqlParamMap = VartechUtils.jsonStringToObject(statementInfo.getSqlParam(), HashMap.class);
    ResponseResult parseInfo = SqlSourceBuilder.parseResponseResult(statementInfo.getSql(), sqlParamMap, DBType.getDBType(statementInfo.getDbType()));
    List<SqlSource> sqlList = parseInfo.getItems();
    result.setStartTime(System.currentTimeMillis());
    SqlSource tmpSqlSource = sqlList.get(0);
    if (resultHandler == null) {
        resultHandler = getDefaultSelectHandler();
    }
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        conn = ConnectionFactory.getInstance().getConnection(statementInfo.getVconnid());
        logger.debug("execute query: {} ", tmpSqlSource.getQuery());
        pstmt = conn.prepareStatement(tmpSqlSource.getQuery());
        pstmt.setMaxRows(statementInfo.getLimit());
        SQLParamUtils.setSqlParameter(pstmt, tmpSqlSource);
        rs = pstmt.executeQuery();
        SQLResultSetUtils.resultSetHandler(rs, statementInfo, resultHandler, true);
    } catch (Throwable e) {
        result.setResultCode(VarsqlAppCode.EC_SQL_EXECUTOR);
        result.setMessage(" error message :  " + e.getMessage());
        logger.error("select : {} ", e.getMessage(), e);
    } finally {
        JdbcUtils.close(conn, pstmt, rs);
    }
    result.setTotalCount(resultHandler.getTotalCount());
    result.setEndTime(System.currentTimeMillis());
    result.setResult(resultHandler.getResult());
    return result;
}
Also used : SqlSource(com.varsql.core.sql.builder.SqlSource) ResponseResult(com.vartech.common.app.beans.ResponseResult) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SqlSource (com.varsql.core.sql.builder.SqlSource)6 ResponseResult (com.vartech.common.app.beans.ResponseResult)3 Connection (java.sql.Connection)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Map (java.util.Map)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLAlterStatement (com.alibaba.druid.sql.ast.statement.SQLAlterStatement)1 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)1 SQLDropStatement (com.alibaba.druid.sql.ast.statement.SQLDropStatement)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 SQLTruncateStatement (com.alibaba.druid.sql.ast.statement.SQLTruncateStatement)1 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)1 DatabaseInfo (com.varsql.core.db.valueobject.DatabaseInfo)1 ConnectionFactoryException (com.varsql.core.exception.ConnectionFactoryException)1 ResultSetConvertException (com.varsql.core.exception.ResultSetConvertException)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