Search in sources :

Example 1 with SelectRequest

use of com.alibaba.nacos.config.server.service.sql.SelectRequest in project nacos by alibaba.

the class DistributedDatabaseOperateImpl method onRequest.

@SuppressWarnings("all")
@Override
public Response onRequest(final ReadRequest request) {
    final SelectRequest selectRequest = serializer.deserialize(request.getData().toByteArray(), SelectRequest.class);
    LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "getData info : selectRequest : {}", selectRequest);
    final RowMapper<Object> mapper = RowMapperManager.getRowMapper(selectRequest.getClassName());
    final byte type = selectRequest.getQueryType();
    readLock.lock();
    Object data;
    try {
        switch(type) {
            case QueryType.QUERY_ONE_WITH_MAPPER_WITH_ARGS:
                data = queryOne(jdbcTemplate, selectRequest.getSql(), selectRequest.getArgs(), mapper);
                break;
            case QueryType.QUERY_ONE_NO_MAPPER_NO_ARGS:
                data = queryOne(jdbcTemplate, selectRequest.getSql(), ClassUtils.findClassByName(selectRequest.getClassName()));
                break;
            case QueryType.QUERY_ONE_NO_MAPPER_WITH_ARGS:
                data = queryOne(jdbcTemplate, selectRequest.getSql(), selectRequest.getArgs(), ClassUtils.findClassByName(selectRequest.getClassName()));
                break;
            case QueryType.QUERY_MANY_WITH_MAPPER_WITH_ARGS:
                data = queryMany(jdbcTemplate, selectRequest.getSql(), selectRequest.getArgs(), mapper);
                break;
            case QueryType.QUERY_MANY_WITH_LIST_WITH_ARGS:
                data = queryMany(jdbcTemplate, selectRequest.getSql(), selectRequest.getArgs());
                break;
            case QueryType.QUERY_MANY_NO_MAPPER_WITH_ARGS:
                data = queryMany(jdbcTemplate, selectRequest.getSql(), selectRequest.getArgs(), ClassUtils.findClassByName(selectRequest.getClassName()));
                break;
            default:
                throw new IllegalArgumentException("Unsupported data query categories");
        }
        ByteString bytes = data == null ? ByteString.EMPTY : ByteString.copyFrom(serializer.serialize(data));
        return Response.newBuilder().setSuccess(true).setData(bytes).build();
    } catch (Exception e) {
        LogUtil.FATAL_LOG.error("There was an error querying the data, request : {}, error : {}", selectRequest, e.toString());
        return Response.newBuilder().setSuccess(false).setErrMsg(ClassUtils.getSimplaName(e) + ":" + ExceptionUtil.getCause(e).getMessage()).build();
    } finally {
        readLock.unlock();
    }
}
Also used : ByteString(com.google.protobuf.ByteString) SelectRequest(com.alibaba.nacos.config.server.service.sql.SelectRequest) TimeoutException(java.util.concurrent.TimeoutException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DataAccessException(org.springframework.dao.DataAccessException) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)1 NacosRuntimeException (com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)1 NJdbcException (com.alibaba.nacos.config.server.exception.NJdbcException)1 SelectRequest (com.alibaba.nacos.config.server.service.sql.SelectRequest)1 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 ByteString (com.google.protobuf.ByteString)1 TimeoutException (java.util.concurrent.TimeoutException)1 DataAccessException (org.springframework.dao.DataAccessException)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)1