All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.couchbase.jdbc.core.CouchResponse Maven / Gradle / Ivy

Go to download

A JDBC driver for the Couchbase database, based on the N1QL query language.

The newest version!
/*
 * //  Copyright (c) 2015 Couchbase, Inc.
 * //  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * //  except in compliance with the License. You may obtain a copy of the License at
 * //    http://www.apache.org/licenses/LICENSE-2.0
 * //  Unless required by applicable law or agreed to in writing, software distributed under the
 * //  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * //  either express or implied. See the License for the specific language governing permissions
 * //  and limitations under the License.
 */

package com.couchbase.jdbc.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by davec on 2015-06-23.
 */
public class CouchResponse
{
    AtomicBoolean fieldsInitialized = new AtomicBoolean(false);

    Map     signature=null;
    ArrayList fields;

    List errors;
    List warnings;
    CouchMetrics metrics;
    String requestId;
    String status;
    List > results;


    public CouchMetrics getMetrics()
    {
        return metrics;
    }


    // we don't know which will get called first so set the fields in getFields()

    public void setSignature(Map  signature)
    {
        this.signature = signature;
    }

    public ArrayListgetFields()
    {
        // check to make sure we haven't set these yet
        if (!fieldsInitialized.getAndSet(true))
        {
            if (signature != null)
            {
                fields = new ArrayList(signature.size());
            }

            if (signature.containsKey("*")  )
            {
                if (metrics.getResultSize() > 0) {
                    Map firstRow = results.get(0);
                    Set keySet = firstRow.keySet();

                    for (String key : keySet)
                    {
                        Object object =  firstRow.get(key);
                        if (object == null )
                        {
                            fields.add( new Field(key, "null"));
                        }
                        else
                        {
                            Object type = firstRow.get(key);
                            String jsonType="json";

                            if (type instanceof Number)
                                jsonType = "number";
                            else if ( type instanceof Boolean)
                                jsonType = "boolean";
                            else if ( type instanceof String)
                                jsonType = "string";
                            else if (type instanceof Map )
                                jsonType = "json";
                            else if ( type instanceof List )
                                jsonType = "json";

                            fields.add(new Field(key, jsonType));
                        }
                    }
                }
            }
            else
            {
                for (String key : signature.keySet())
                {
                    fields.add(new Field(key, signature.get(key)));
                }
            }
        }
        return fields;
    }
    public List > getResults()
    {
        return results;
    }
    public void setResults(List results)
    {
        //noinspection unchecked
        this.results = results;
    }
    public void setMetrics(CouchMetrics metrics)
    {
        this.metrics = metrics;
    }
    public List  getWarnings() {return warnings;}
    public List  getErrors(){return errors;}

    public Map getFirstResult()
    {
        return (Map)results.get(0);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy