Example usage for java.sql SQLWarning SQLWarning

List of usage examples for java.sql SQLWarning SQLWarning

Introduction

In this page you can find the example usage for java.sql SQLWarning SQLWarning.

Prototype

public SQLWarning(String reason, String SQLState, Throwable cause) 

Source Link

Document

Constructs a SQLWarning object with a given reason, SQLState and cause.

Usage

From source file:com.couchbase.jdbc.core.ProtocolImpl.java

public CouchResponse handleResponse(String sql, CloseableHttpResponse response)
        throws SQLException, IOException {
    int status = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();

    ObjectMapper mapper = JsonFactory.create();

    CouchResponse couchResponse = new CouchResponse();

    String strResponse = EntityUtils.toString(entity);
    //        logger.trace( "Response to query {} {}", sql, strResponse );

    Object foo = mapper.readValue(strResponse, Map.class);
    Map<String, Object> rootAsMap = null;
    if (foo instanceof Map) {
        //noinspection unchecked
        rootAsMap = (Map<String, Object>) foo;
    } else {//  w w w .j av a 2  s . co m
        logger.debug("error");
    }
    couchResponse.status = (String) rootAsMap.get("status");
    couchResponse.requestId = (String) rootAsMap.get("requestID");
    Object signature = rootAsMap.get("signature");

    if (signature instanceof Map) {
        //noinspection unchecked
        couchResponse.signature = (Map) signature;
        //noinspection unchecked
        couchResponse.results = (List) rootAsMap.get("results");
    } else if (signature instanceof String) {
        couchResponse.signature = new HashMap<String, String>();
        couchResponse.signature.put("$1", (String) signature);

        Iterator iterator = ((List) rootAsMap.get("results")).iterator();

        couchResponse.results = new ArrayList<>();
        while (iterator.hasNext()) {
            Object object = iterator.next();

            HashMap entry = new HashMap();
            //noinspection unchecked
            entry.put("$1", object);
            //noinspection unchecked
            couchResponse.results.add(entry);
        }

    } else if (signature != null) {
        throw new SQLException("Error reading signature" + signature);
    }
    //noinspection unchecked
    couchResponse.metrics = MapObjectConversion.fromMap((Map) rootAsMap.get("metrics"), CouchMetrics.class);
    List errorList = (List) rootAsMap.get("errors");
    if (errorList != null) {
        //noinspection unchecked,unchecked
        couchResponse.errors = MapObjectConversion.convertListOfMapsToObjects(CouchError.class, errorList);
    }
    List warningList = (List) rootAsMap.get("warnings");
    if (warningList != null) {
        //noinspection unchecked,unchecked
        couchResponse.warnings = MapObjectConversion.convertListOfMapsToObjects(CouchError.class, warningList);

        for (CouchError warning : couchResponse.warnings) {
            if (sqlWarning != null) {
                sqlWarning = new SQLWarning(warning.msg, null, warning.code);
            } else {
                sqlWarning.setNextWarning(new SQLWarning(warning.msg, null, warning.code));
            }
        }
    }

    //JsonObject jsonObject = jsonReader.readObject();
    //logger.trace( "response from query {} {}", sql, jsonObject.toString());

    //String statusString = (String)jsonObject.get("status");

    Integer iStatus = statusStrings.get(couchResponse.status);
    String message;

    switch (status) {
    case 200:
        switch (iStatus.intValue()) {
        case N1QL_ERROR:
            List<CouchError> errors = couchResponse.errors;
            throw new SQLException(errors.get(0).msg);

        case N1QL_SUCCESS:
            return couchResponse;

        case N1QL_COMPLETED:
        case N1QL_FATAL:
        case N1QL_RUNNING:
        case N1QL_STOPPED:
        case N1QL_TIMEOUT:
            message = "Invalid Status";
            fillSQLException(message, couchResponse);

        default:
            logger.error("Unexpected status string {} for query {}", couchResponse.status, sql);
            throw new SQLException("Unexpected status: " + couchResponse.status);

        }
    case 400:
        message = "Bad Request";
        fillSQLException(message, couchResponse);
    case 401:
        message = "Unauthorized Request credentials are missing or invalid";
        fillSQLException(message, couchResponse);
    case 403:
        message = "Forbidden Request: read only violation or client unauthorized to modify";
        fillSQLException(message, couchResponse);
    case 404:
        message = "Not found: Request references an invalid keyspace or there is no primary key";
        fillSQLException(message, couchResponse);
    case 405:
        message = "Method not allowed: The REST method type in request is supported";
        fillSQLException(message, couchResponse);
    case 409:
        message = "Conflict: attempt to create a keyspace or index that already exists";
        fillSQLException(message, couchResponse);
    case 410:
        message = "Gone: The server is doing a graceful shutdown";
        fillSQLException(message, couchResponse);
    case 500:
        message = "Internal server error: unforeseen problem processing the request";
        fillSQLException(message, couchResponse);
    case 503:
        message = "Service Unavailable: there is an issue preventing the request from being serviced";
        logger.debug("Error with the request {}", message);

        CouchError errors, warnings;

        if (couchResponse.metrics.errorCount > 0) {
            errors = couchResponse.errors.get(0);
            logger.error("Error Code: {} Message: {} for query {} ", errors.code, errors.msg, sql);
        }
        if (couchResponse.metrics.warningCount > 0) {
            warnings = couchResponse.warnings.get(0);
            logger.error("Warning Code: {} Message: {} for query {}", warnings.code, warnings.msg, sql);
        }

        fillSQLException(message, couchResponse);

    default:
        throw new ClientProtocolException("Unexpected response status: " + status);

    }
}