String createQuery(CouchDBQueryInterpreter interpreter, EntityMetadata m, StringBuilder q, String _id)
            throws URISyntaxException, UnsupportedEncodingException, IOException, ClientProtocolException
    {
        if (interpreter.isAggregation())
        {
            _id = CouchDBConstants.URL_SEPARATOR + m.getSchema().toLowerCase() + CouchDBConstants.URL_SEPARATOR
                    + CouchDBConstants.DESIGN + CouchDBConstants.AGGREGATIONS + CouchDBConstants.VIEW
                    + interpreter.getAggregationType();
            if (interpreter.getAggregationColumn() != null)
            {
                q.append("key=");
                q.append("\"" + interpreter.getAggregationColumn() + "_" + m.getTableName() + "\"");

            }
            else
            {
                q.append("key=" + "\"" + CouchDBConstants.ALL + "_" + m.getTableName() + "\"");
            }
            q.append("&group=true");
        }
        else if (!interpreter.isRangeQuery() && interpreter.getOperator() != null
                && interpreter.getOperator().equalsIgnoreCase("AND"))
        {
            StringBuilder viewName = new StringBuilder();
            List<String> columns = new ArrayList<String>();
            q.append("key=[");
            for (String columnName : interpreter.getKeyValues().keySet())
            {
                viewName.append(columnName + "AND");
                q.append(CouchDBUtils.appendQuotes(interpreter.getKeyValues().get(columnName)));
                q.append(",");
                columns.add(columnName);
            }
            q.deleteCharAt(q.toString().lastIndexOf(","));
            q.append("]");
            viewName.delete(viewName.toString().lastIndexOf("AND"), viewName.toString().lastIndexOf("AND") + 3);
            _id = _id + viewName.toString();
            CouchDBUtils.createDesignDocumentIfNotExist(httpClient, httpHost, gson, m.getTableName(), m.getSchema(),
                    viewName.toString(), columns);
        }
        else if (interpreter.getKeyValues() != null)
        {
            if (interpreter.getStartKeyValue() != null || interpreter.getEndKeyValue() != null)
            {
                String queryString = null;
                if (interpreter.getStartKeyValue() != null)
                {
                    queryString = "startkey=" + CouchDBUtils.appendQuotes(interpreter.getStartKeyValue());
                    q.append(queryString);
                }
                if (interpreter.getEndKeyValue() != null)
                {
                    if (interpreter.getStartKeyValue() != null)
                    {
                        q.append("&");
                    }
                    queryString = "endkey=" + CouchDBUtils.appendQuotes(interpreter.getEndKeyValue());
                    q.append(queryString);
                    if (interpreter.isIncludeLastKey())
                    {
                        q.append("&inclusive_end=");
                        q.append(true);
                    }
                }
            }
            else if (interpreter.getKeyValue() != null)
            {
                q.append("key=" + CouchDBUtils.appendQuotes(interpreter.getKeyValue()));
            }
            _id = _id + interpreter.getKeyName();
        }
        else if (interpreter.getColumns() != null && interpreter.getColumns().length != 0)
        {
            _id += CouchDBConstants.FIELDS;
            q.append("keys=[");
            for (String column : interpreter.getColumns())
            {
                q.append("\"" + column + "\",");
            }
            q.setCharAt(q.length() - 1, ']');
        }
        else
        {
            _id = _id + "all";
        }

        return _id;
    }
String createQuery ( CouchDBQueryInterpreter interpreter , EntityMetadata m , StringBuilder q , String _id ) throws URISyntaxException , UnsupportedEncodingException , IOException , ClientProtocolException { if ( interpreter . isAggregation ( ) ) { _id = CouchDBConstants . URL_SEPARATOR + m . getSchema ( ) . toLowerCase ( ) + CouchDBConstants . URL_SEPARATOR + CouchDBConstants . DESIGN + CouchDBConstants . AGGREGATIONS + CouchDBConstants . VIEW + interpreter . getAggregationType ( ) ; if ( interpreter . getAggregationColumn ( ) != null ) { q . append ( "key=" ) ; q . append ( "\"" + interpreter . getAggregationColumn ( ) + "_" + m . getTableName ( ) + "\"" ) ; } else { q . append ( "key=" + "\"" + CouchDBConstants . ALL + "_" + m . getTableName ( ) + "\"" ) ; } q . append ( "&group=true" ) ; } else if ( ! interpreter . isRangeQuery ( ) && interpreter . getOperator ( ) != null && interpreter . getOperator ( ) . equalsIgnoreCase ( "AND" ) ) { StringBuilder viewName = new StringBuilder ( ) ; List < String > columns = new ArrayList < String > ( ) ; q . append ( "key=[" ) ; for ( String columnName : interpreter . getKeyValues ( ) . keySet ( ) ) { viewName . append ( columnName + "AND" ) ; q . append ( CouchDBUtils . appendQuotes ( interpreter . getKeyValues ( ) . get ( columnName ) ) ) ; q . append ( "," ) ; columns . add ( columnName ) ; } q . deleteCharAt ( q . toString ( ) . lastIndexOf ( "," ) ) ; q . append ( "]" ) ; viewName . delete ( viewName . toString ( ) . lastIndexOf ( "AND" ) , viewName . toString ( ) . lastIndexOf ( "AND" ) + 3 ) ; _id = _id + viewName . toString ( ) ; CouchDBUtils . createDesignDocumentIfNotExist ( httpClient , httpHost , gson , m . getTableName ( ) , m . getSchema ( ) , viewName . toString ( ) , columns ) ; } else if ( interpreter . getKeyValues ( ) != null ) { if ( interpreter . getStartKeyValue ( ) != null || interpreter . getEndKeyValue ( ) != null ) { String queryString = null ; if ( interpreter . getStartKeyValue ( ) != null ) { queryString = "startkey=" + CouchDBUtils . appendQuotes ( interpreter . getStartKeyValue ( ) ) ; q . append ( queryString ) ; } if ( interpreter . getEndKeyValue ( ) != null ) { if ( interpreter . getStartKeyValue ( ) != null ) { q . append ( "&" ) ; } queryString = "endkey=" + CouchDBUtils . appendQuotes ( interpreter . getEndKeyValue ( ) ) ; q . append ( queryString ) ; if ( interpreter . isIncludeLastKey ( ) ) { q . append ( "&inclusive_end=" ) ; q . append ( true ) ; } } } else if ( interpreter . getKeyValue ( ) != null ) { q . append ( "key=" + CouchDBUtils . appendQuotes ( interpreter . getKeyValue ( ) ) ) ; } _id = _id + interpreter . getKeyName ( ) ; } else if ( interpreter . getColumns ( ) != null && interpreter . getColumns ( ) . length != 0 ) { _id += CouchDBConstants . FIELDS ; q . append ( "keys=[" ) ; for ( String column : interpreter . getColumns ( ) ) { q . append ( "\"" + column + "\"," ) ; } q . setCharAt ( q . length ( ) - 1 , ']' ) ; } else { _id = _id + "all" ; } return _id ; }
Creates the query.

@param interpreter
the interpreter
@param m
the m
@param q
the q
@param _id
the _id
@return the string
@throws URISyntaxException
the URI syntax exception
@throws UnsupportedEncodingException
the unsupported encoding exception
@throws IOException
Signals that an I/O exception has occurred.
@throws ClientProtocolException
the client protocol exception