/// <summary>
        /// Builds a main query to return an Any or All result
        /// </summary>
        /// <returns>Query string</returns>
        private string BuildMainAnyAllQuery()
        {
            var sb = new StringBuilder();

            sb.AppendFormat("SELECT {0} as result",
                            QueryType == N1QlQueryType.MainQueryAny ? "true" : "false");

            if (Extents.Any())
            {
                var mainFrom = Extents.First();
                mainFrom.AppendToStringBuilder(sb);

                if (!string.IsNullOrEmpty(UseKeysPart))
                {
                    sb.AppendFormat(" USE KEYS {0}", UseKeysPart);
                }

                foreach (var joinPart in Extents.Skip(1))
                {
                    joinPart.AppendToStringBuilder(sb);
                }
            }

            ApplyLetParts(sb);

            bool hasWhereClause = false;

            if (WhereParts.Any())
            {
                sb.AppendFormat(" WHERE {0}", String.Join(" AND ", WhereParts));

                hasWhereClause = true;
            }

            if (QueryType == N1QlQueryType.MainQueryAll)
            {
                sb.AppendFormat(" {0} NOT ({1})",
                                hasWhereClause ? "AND" : "WHERE",
                                WhereAllPart);
            }

            sb.Append(" LIMIT 1");

            return(sb.ToString());
        }
        /// <summary>
        /// Builds a primary select query
        /// </summary>
        /// <returns>Query string</returns>
        private string BuildSelectQuery()
        {
            var sb = new StringBuilder();

            if (QueryType == N1QlQueryType.Subquery)
            {
                if (!string.IsNullOrEmpty(PropertyExtractionPart))
                {
                    // Subqueries will always return a list of objects
                    // But we need to use an ARRAY statement to convert it into an array of a particular property of that object

                    sb.AppendFormat("ARRAY `ArrayExtent`.{0} FOR `ArrayExtent` IN (", PropertyExtractionPart);
                }
                else
                {
                    sb.Append('(');
                }
            }
            else if (QueryType == N1QlQueryType.SubqueryAny)
            {
                sb.AppendFormat("ANY {0} IN (", PropertyExtractionPart);
            }
            else if (QueryType == N1QlQueryType.SubqueryAll)
            {
                sb.AppendFormat("EVERY {0} IN (", PropertyExtractionPart);
            }

            if (!string.IsNullOrWhiteSpace(ExplainPart))
            {
                sb.Append(ExplainPart);
            }

            sb.Append("SELECT ");
            if (RawSelection)
            {
                sb.Append("RAW ");
            }

            if (!string.IsNullOrEmpty(AggregateFunction))
            {
                sb.AppendFormat("{0}({1}{2})",
                                AggregateFunction,
                                !string.IsNullOrWhiteSpace(DistinctPart) ? DistinctPart : string.Empty,
                                SelectPart);
            }
            else
            {
                sb.AppendFormat("{0}{1}",
                                !string.IsNullOrWhiteSpace(DistinctPart) ? DistinctPart : string.Empty,
                                SelectPart);
                //TODO support multiple select parts: http://localhost:8093/tutorial/content/#5
            }

            if (!IsBucketSubquery && !string.IsNullOrEmpty(PropertyExtractionPart))
            {
                sb.AppendFormat(" as {0}", PropertyExtractionPart);
            }

            if (Extents.Any())
            {
                var mainFrom = Extents.First();
                mainFrom.AppendToStringBuilder(sb);

                if (!string.IsNullOrEmpty(UseKeysPart))
                {
                    sb.AppendFormat(" USE KEYS {0}", UseKeysPart);
                }

                foreach (var joinPart in Extents.Skip(1))
                {
                    joinPart.AppendToStringBuilder(sb);
                }
            }

            ApplyLetParts(sb);

            if (WhereParts.Any())
            {
                sb.AppendFormat(" WHERE {0}", String.Join(" AND ", WhereParts));
            }
            if ((GroupByParts != null) && GroupByParts.Any())
            {
                sb.AppendFormat(" GROUP BY {0}", string.Join(", ", GroupByParts));
            }
            if ((HavingParts != null) && HavingParts.Any())
            {
                sb.AppendFormat(" HAVING {0}", string.Join(" AND ", HavingParts));
            }

            if (UnionParts != null)
            {
                sb.Append(string.Join("", UnionParts));
            }

            if (OrderByParts.Any())
            {
                sb.AppendFormat(" ORDER BY {0}", String.Join(", ", OrderByParts));
            }
            if (LimitPart != null)
            {
                sb.Append(LimitPart);
            }
            if (LimitPart != null && OffsetPart != null)
            {
                sb.Append(OffsetPart);
            }

            if (QueryType == N1QlQueryType.Subquery)
            {
                if (!string.IsNullOrEmpty(PropertyExtractionPart))
                {
                    sb.Append(") END");
                }
                else
                {
                    sb.Append(')');
                }
            }
            else if (QueryType == N1QlQueryType.SubqueryAny)
            {
                sb.Append(") SATISFIES true END");
            }
            else if (QueryType == N1QlQueryType.SubqueryAll)
            {
                sb.AppendFormat(") SATISFIES {0} END", WhereAllPart);
            }

            return(sb.ToString());
        }